xen.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/loader.h>
  19. #include <grub/memory.h>
  20. #include <grub/normal.h>
  21. #include <grub/file.h>
  22. #include <grub/disk.h>
  23. #include <grub/err.h>
  24. #include <grub/misc.h>
  25. #include <grub/types.h>
  26. #include <grub/dl.h>
  27. #include <grub/mm.h>
  28. #include <grub/term.h>
  29. #include <grub/cpu/linux.h>
  30. #include <grub/video.h>
  31. #include <grub/video_fb.h>
  32. #include <grub/command.h>
  33. #include <grub/xen/relocator.h>
  34. #include <grub/i18n.h>
  35. #include <grub/elf.h>
  36. #include <grub/elfload.h>
  37. #include <grub/lib/cmdline.h>
  38. #include <grub/xen.h>
  39. #include <grub/xen_file.h>
  40. #include <grub/linux.h>
  41. #include <grub/i386/memory.h>
  42. GRUB_MOD_LICENSE ("GPLv3+");
  43. #ifdef __x86_64__
  44. #define NUMBER_OF_LEVELS 4
  45. #define INTERMEDIATE_OR (GRUB_PAGE_PRESENT | GRUB_PAGE_RW | GRUB_PAGE_USER)
  46. #define VIRT_MASK 0x0000ffffffffffffULL
  47. #else
  48. #define NUMBER_OF_LEVELS 3
  49. #define INTERMEDIATE_OR (GRUB_PAGE_PRESENT | GRUB_PAGE_RW)
  50. #define VIRT_MASK 0x00000000ffffffffULL
  51. #define HYPERVISOR_PUD_ADDRESS 0xc0000000ULL
  52. #endif
  53. struct grub_xen_mapping_lvl {
  54. grub_uint64_t virt_start;
  55. grub_uint64_t virt_end;
  56. grub_uint64_t pfn_start;
  57. grub_uint64_t n_pt_pages;
  58. };
  59. struct grub_xen_mapping {
  60. grub_uint64_t *where;
  61. struct grub_xen_mapping_lvl area;
  62. struct grub_xen_mapping_lvl lvls[NUMBER_OF_LEVELS];
  63. };
  64. struct xen_loader_state {
  65. struct grub_relocator *relocator;
  66. struct grub_relocator_xen_state state;
  67. struct start_info next_start;
  68. struct grub_xen_file_info xen_inf;
  69. grub_xen_mfn_t *virt_mfn_list;
  70. struct start_info *virt_start_info;
  71. grub_xen_mfn_t console_pfn;
  72. grub_uint64_t max_addr;
  73. grub_uint64_t pgtbl_end;
  74. struct xen_multiboot_mod_list *module_info_page;
  75. grub_uint64_t modules_target_start;
  76. grub_size_t n_modules;
  77. struct grub_xen_mapping *map_reloc;
  78. struct grub_xen_mapping mappings[XEN_MAX_MAPPINGS];
  79. int n_mappings;
  80. int loaded;
  81. };
  82. static struct xen_loader_state xen_state;
  83. static grub_dl_t my_mod;
  84. #define PAGE_SIZE (1UL << PAGE_SHIFT)
  85. #define MAX_MODULES (PAGE_SIZE / sizeof (struct xen_multiboot_mod_list))
  86. #define STACK_SIZE 1048576
  87. #define ADDITIONAL_SIZE (1 << 19)
  88. #define ALIGN_SIZE (1 << 22)
  89. #define LOG_POINTERS_PER_PAGE 9
  90. #define POINTERS_PER_PAGE (1 << LOG_POINTERS_PER_PAGE)
  91. static grub_uint64_t
  92. page2offset (grub_uint64_t page)
  93. {
  94. return page << PAGE_SHIFT;
  95. }
  96. static grub_err_t
  97. get_pgtable_size (grub_uint64_t from, grub_uint64_t to, grub_uint64_t pfn)
  98. {
  99. struct grub_xen_mapping *map, *map_cmp;
  100. grub_uint64_t mask, bits;
  101. int i, m;
  102. if (xen_state.n_mappings == XEN_MAX_MAPPINGS)
  103. return grub_error (GRUB_ERR_BUG, "too many mapped areas");
  104. grub_dprintf ("xen", "get_pgtable_size %d from=%llx, to=%llx, pfn=%llx\n",
  105. xen_state.n_mappings, (unsigned long long) from,
  106. (unsigned long long) to, (unsigned long long) pfn);
  107. map = xen_state.mappings + xen_state.n_mappings;
  108. grub_memset (map, 0, sizeof (*map));
  109. map->area.virt_start = from & VIRT_MASK;
  110. map->area.virt_end = (to - 1) & VIRT_MASK;
  111. map->area.n_pt_pages = 0;
  112. for (i = NUMBER_OF_LEVELS - 1; i >= 0; i--)
  113. {
  114. map->lvls[i].pfn_start = pfn + map->area.n_pt_pages;
  115. if (i == NUMBER_OF_LEVELS - 1)
  116. {
  117. if (xen_state.n_mappings == 0)
  118. {
  119. map->lvls[i].virt_start = 0;
  120. map->lvls[i].virt_end = VIRT_MASK;
  121. map->lvls[i].n_pt_pages = 1;
  122. map->area.n_pt_pages++;
  123. }
  124. continue;
  125. }
  126. bits = PAGE_SHIFT + (i + 1) * LOG_POINTERS_PER_PAGE;
  127. mask = (1ULL << bits) - 1;
  128. map->lvls[i].virt_start = map->area.virt_start & ~mask;
  129. map->lvls[i].virt_end = map->area.virt_end | mask;
  130. #ifdef __i386__
  131. /* PAE wants last root directory present. */
  132. if (i == 1 && to <= HYPERVISOR_PUD_ADDRESS && xen_state.n_mappings == 0)
  133. map->lvls[i].virt_end = VIRT_MASK;
  134. #endif
  135. for (m = 0; m < xen_state.n_mappings; m++)
  136. {
  137. map_cmp = xen_state.mappings + m;
  138. if (map_cmp->lvls[i].virt_start == map_cmp->lvls[i].virt_end)
  139. continue;
  140. if (map->lvls[i].virt_start >= map_cmp->lvls[i].virt_start &&
  141. map->lvls[i].virt_end <= map_cmp->lvls[i].virt_end)
  142. {
  143. map->lvls[i].virt_start = 0;
  144. map->lvls[i].virt_end = 0;
  145. break;
  146. }
  147. if (map->lvls[i].virt_start >= map_cmp->lvls[i].virt_start &&
  148. map->lvls[i].virt_start <= map_cmp->lvls[i].virt_end)
  149. map->lvls[i].virt_start = map_cmp->lvls[i].virt_end + 1;
  150. if (map->lvls[i].virt_end >= map_cmp->lvls[i].virt_start &&
  151. map->lvls[i].virt_end <= map_cmp->lvls[i].virt_end)
  152. map->lvls[i].virt_end = map_cmp->lvls[i].virt_start - 1;
  153. }
  154. if (map->lvls[i].virt_start < map->lvls[i].virt_end)
  155. map->lvls[i].n_pt_pages =
  156. ((map->lvls[i].virt_end - map->lvls[i].virt_start) >> bits) + 1;
  157. map->area.n_pt_pages += map->lvls[i].n_pt_pages;
  158. grub_dprintf ("xen", "get_pgtable_size level %d: virt %llx-%llx %d pts\n",
  159. i, (unsigned long long) map->lvls[i].virt_start,
  160. (unsigned long long) map->lvls[i].virt_end,
  161. (int) map->lvls[i].n_pt_pages);
  162. }
  163. grub_dprintf ("xen", "get_pgtable_size return: %d page tables\n",
  164. (int) map->area.n_pt_pages);
  165. xen_state.state.paging_start[xen_state.n_mappings] = pfn;
  166. xen_state.state.paging_size[xen_state.n_mappings] = map->area.n_pt_pages;
  167. return GRUB_ERR_NONE;
  168. }
  169. static grub_uint64_t *
  170. get_pg_table_virt (int mapping, int level)
  171. {
  172. grub_uint64_t pfn;
  173. struct grub_xen_mapping *map;
  174. map = xen_state.mappings + mapping;
  175. pfn = map->lvls[level].pfn_start - map->lvls[NUMBER_OF_LEVELS - 1].pfn_start;
  176. return map->where + pfn * POINTERS_PER_PAGE;
  177. }
  178. static grub_uint64_t
  179. get_pg_table_prot (int level, grub_uint64_t pfn)
  180. {
  181. int m;
  182. grub_uint64_t pfn_s, pfn_e;
  183. if (level > 0)
  184. return INTERMEDIATE_OR;
  185. for (m = 0; m < xen_state.n_mappings; m++)
  186. {
  187. pfn_s = xen_state.mappings[m].lvls[NUMBER_OF_LEVELS - 1].pfn_start;
  188. pfn_e = xen_state.mappings[m].area.n_pt_pages + pfn_s;
  189. if (pfn >= pfn_s && pfn < pfn_e)
  190. return GRUB_PAGE_PRESENT | GRUB_PAGE_USER;
  191. }
  192. return GRUB_PAGE_PRESENT | GRUB_PAGE_RW | GRUB_PAGE_USER;
  193. }
  194. static void
  195. generate_page_table (grub_xen_mfn_t *mfn_list)
  196. {
  197. int l, m1, m2;
  198. long p, p_s, p_e;
  199. grub_uint64_t start, end, pfn;
  200. grub_uint64_t *pg;
  201. struct grub_xen_mapping_lvl *lvl;
  202. for (m1 = 0; m1 < xen_state.n_mappings; m1++)
  203. grub_memset (xen_state.mappings[m1].where, 0,
  204. xen_state.mappings[m1].area.n_pt_pages * PAGE_SIZE);
  205. for (l = NUMBER_OF_LEVELS - 1; l >= 0; l--)
  206. {
  207. for (m1 = 0; m1 < xen_state.n_mappings; m1++)
  208. {
  209. start = xen_state.mappings[m1].lvls[l].virt_start;
  210. end = xen_state.mappings[m1].lvls[l].virt_end;
  211. pg = get_pg_table_virt(m1, l);
  212. for (m2 = 0; m2 < xen_state.n_mappings; m2++)
  213. {
  214. lvl = (l > 0) ? xen_state.mappings[m2].lvls + l - 1
  215. : &xen_state.mappings[m2].area;
  216. if (l > 0 && lvl->n_pt_pages == 0)
  217. continue;
  218. if (lvl->virt_start >= end || lvl->virt_end <= start)
  219. continue;
  220. p_s = (grub_max (start, lvl->virt_start) - start) >>
  221. (PAGE_SHIFT + l * LOG_POINTERS_PER_PAGE);
  222. p_e = (grub_min (end, lvl->virt_end) - start) >>
  223. (PAGE_SHIFT + l * LOG_POINTERS_PER_PAGE);
  224. pfn = ((grub_max (start, lvl->virt_start) - lvl->virt_start) >>
  225. (PAGE_SHIFT + l * LOG_POINTERS_PER_PAGE)) + lvl->pfn_start;
  226. grub_dprintf ("xen", "write page table entries level %d pg %p "
  227. "mapping %d/%d index %lx-%lx pfn %llx\n",
  228. l, pg, m1, m2, p_s, p_e, (unsigned long long) pfn);
  229. for (p = p_s; p <= p_e; p++)
  230. {
  231. pg[p] = page2offset (mfn_list[pfn]) |
  232. get_pg_table_prot (l, pfn);
  233. pfn++;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. static grub_err_t
  240. set_mfns (grub_xen_mfn_t pfn)
  241. {
  242. grub_xen_mfn_t i, t;
  243. grub_xen_mfn_t cn_pfn = -1, st_pfn = -1;
  244. struct mmu_update m2p_updates[4];
  245. for (i = 0; i < grub_xen_start_page_addr->nr_pages; i++)
  246. {
  247. if (xen_state.virt_mfn_list[i] ==
  248. grub_xen_start_page_addr->console.domU.mfn)
  249. cn_pfn = i;
  250. if (xen_state.virt_mfn_list[i] == grub_xen_start_page_addr->store_mfn)
  251. st_pfn = i;
  252. }
  253. if (cn_pfn == (grub_xen_mfn_t)-1)
  254. return grub_error (GRUB_ERR_BUG, "no console");
  255. if (st_pfn == (grub_xen_mfn_t)-1)
  256. return grub_error (GRUB_ERR_BUG, "no store");
  257. t = xen_state.virt_mfn_list[pfn];
  258. xen_state.virt_mfn_list[pfn] = xen_state.virt_mfn_list[cn_pfn];
  259. xen_state.virt_mfn_list[cn_pfn] = t;
  260. t = xen_state.virt_mfn_list[pfn + 1];
  261. xen_state.virt_mfn_list[pfn + 1] = xen_state.virt_mfn_list[st_pfn];
  262. xen_state.virt_mfn_list[st_pfn] = t;
  263. m2p_updates[0].ptr =
  264. page2offset (xen_state.virt_mfn_list[pfn]) | MMU_MACHPHYS_UPDATE;
  265. m2p_updates[0].val = pfn;
  266. m2p_updates[1].ptr =
  267. page2offset (xen_state.virt_mfn_list[pfn + 1]) | MMU_MACHPHYS_UPDATE;
  268. m2p_updates[1].val = pfn + 1;
  269. m2p_updates[2].ptr =
  270. page2offset (xen_state.virt_mfn_list[cn_pfn]) | MMU_MACHPHYS_UPDATE;
  271. m2p_updates[2].val = cn_pfn;
  272. m2p_updates[3].ptr =
  273. page2offset (xen_state.virt_mfn_list[st_pfn]) | MMU_MACHPHYS_UPDATE;
  274. m2p_updates[3].val = st_pfn;
  275. grub_xen_mmu_update (m2p_updates, 4, NULL, DOMID_SELF);
  276. return GRUB_ERR_NONE;
  277. }
  278. static grub_err_t
  279. grub_xen_p2m_alloc (void)
  280. {
  281. grub_relocator_chunk_t ch;
  282. grub_size_t p2msize, p2malloc;
  283. grub_err_t err;
  284. struct grub_xen_mapping *map;
  285. if (xen_state.virt_mfn_list)
  286. return GRUB_ERR_NONE;
  287. map = xen_state.mappings + xen_state.n_mappings;
  288. p2msize = ALIGN_UP (sizeof (grub_xen_mfn_t) *
  289. grub_xen_start_page_addr->nr_pages, PAGE_SIZE);
  290. if (xen_state.xen_inf.has_p2m_base)
  291. {
  292. err = get_pgtable_size (xen_state.xen_inf.p2m_base,
  293. xen_state.xen_inf.p2m_base + p2msize,
  294. (xen_state.max_addr + p2msize) >> PAGE_SHIFT);
  295. if (err)
  296. return err;
  297. map->area.pfn_start = xen_state.max_addr >> PAGE_SHIFT;
  298. p2malloc = p2msize + page2offset (map->area.n_pt_pages);
  299. xen_state.n_mappings++;
  300. xen_state.next_start.mfn_list = xen_state.xen_inf.p2m_base;
  301. xen_state.next_start.first_p2m_pfn = map->area.pfn_start;
  302. xen_state.next_start.nr_p2m_frames = p2malloc >> PAGE_SHIFT;
  303. }
  304. else
  305. {
  306. xen_state.next_start.mfn_list =
  307. xen_state.max_addr + xen_state.xen_inf.virt_base;
  308. p2malloc = p2msize;
  309. }
  310. xen_state.state.mfn_list = xen_state.max_addr;
  311. err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
  312. xen_state.max_addr, p2malloc);
  313. if (err)
  314. return err;
  315. xen_state.virt_mfn_list = get_virtual_current_address (ch);
  316. if (xen_state.xen_inf.has_p2m_base)
  317. map->where = (grub_uint64_t *) xen_state.virt_mfn_list +
  318. p2msize / sizeof (grub_uint64_t);
  319. grub_memcpy (xen_state.virt_mfn_list,
  320. (void *) grub_xen_start_page_addr->mfn_list, p2msize);
  321. xen_state.max_addr += p2malloc;
  322. return GRUB_ERR_NONE;
  323. }
  324. static grub_err_t
  325. grub_xen_special_alloc (void)
  326. {
  327. grub_relocator_chunk_t ch;
  328. grub_err_t err;
  329. if (xen_state.virt_start_info)
  330. return GRUB_ERR_NONE;
  331. err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
  332. xen_state.max_addr,
  333. sizeof (xen_state.next_start));
  334. if (err)
  335. return err;
  336. xen_state.state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
  337. xen_state.virt_start_info = get_virtual_current_address (ch);
  338. xen_state.max_addr =
  339. ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
  340. xen_state.console_pfn = xen_state.max_addr >> PAGE_SHIFT;
  341. xen_state.max_addr += 2 * PAGE_SIZE;
  342. xen_state.next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
  343. grub_memcpy (xen_state.next_start.magic, grub_xen_start_page_addr->magic,
  344. sizeof (xen_state.next_start.magic));
  345. xen_state.next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
  346. xen_state.next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
  347. xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
  348. xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
  349. return GRUB_ERR_NONE;
  350. }
  351. static grub_err_t
  352. grub_xen_pt_alloc (void)
  353. {
  354. grub_relocator_chunk_t ch;
  355. grub_err_t err;
  356. grub_uint64_t nr_info_pages;
  357. grub_uint64_t nr_need_pages;
  358. grub_uint64_t try_virt_end;
  359. struct grub_xen_mapping *map;
  360. if (xen_state.pgtbl_end)
  361. return GRUB_ERR_NONE;
  362. map = xen_state.mappings + xen_state.n_mappings;
  363. xen_state.map_reloc = map + 1;
  364. xen_state.next_start.pt_base =
  365. xen_state.max_addr + xen_state.xen_inf.virt_base;
  366. nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
  367. nr_need_pages = nr_info_pages;
  368. while (1)
  369. {
  370. try_virt_end = ALIGN_UP (xen_state.xen_inf.virt_base +
  371. page2offset (nr_need_pages) +
  372. ADDITIONAL_SIZE + STACK_SIZE, ALIGN_SIZE);
  373. err = get_pgtable_size (xen_state.xen_inf.virt_base, try_virt_end,
  374. nr_info_pages);
  375. if (err)
  376. return err;
  377. xen_state.n_mappings++;
  378. /* Map the relocator page either at virtual 0 or after end of area. */
  379. nr_need_pages = nr_info_pages + map->area.n_pt_pages;
  380. if (xen_state.xen_inf.virt_base)
  381. err = get_pgtable_size (0, PAGE_SIZE, nr_need_pages);
  382. else
  383. err = get_pgtable_size (try_virt_end, try_virt_end + PAGE_SIZE,
  384. nr_need_pages);
  385. if (err)
  386. return err;
  387. nr_need_pages += xen_state.map_reloc->area.n_pt_pages;
  388. if (xen_state.xen_inf.virt_base + page2offset (nr_need_pages) <=
  389. try_virt_end)
  390. break;
  391. xen_state.n_mappings--;
  392. }
  393. xen_state.n_mappings++;
  394. nr_need_pages = map->area.n_pt_pages + xen_state.map_reloc->area.n_pt_pages;
  395. err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
  396. xen_state.max_addr,
  397. page2offset (nr_need_pages));
  398. if (err)
  399. return err;
  400. map->where = get_virtual_current_address (ch);
  401. map->area.pfn_start = 0;
  402. xen_state.max_addr += page2offset (nr_need_pages);
  403. xen_state.state.stack =
  404. xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
  405. xen_state.next_start.nr_pt_frames = nr_need_pages;
  406. xen_state.max_addr = try_virt_end - xen_state.xen_inf.virt_base;
  407. xen_state.pgtbl_end = xen_state.max_addr >> PAGE_SHIFT;
  408. xen_state.map_reloc->where = (grub_uint64_t *) ((char *) map->where +
  409. page2offset (map->area.n_pt_pages));
  410. return GRUB_ERR_NONE;
  411. }
  412. /* Allocate all not yet allocated areas mapped by initial page tables. */
  413. static grub_err_t
  414. grub_xen_alloc_boot_data (void)
  415. {
  416. grub_err_t err;
  417. if (!xen_state.xen_inf.has_p2m_base)
  418. {
  419. err = grub_xen_p2m_alloc ();
  420. if (err)
  421. return err;
  422. }
  423. err = grub_xen_special_alloc ();
  424. if (err)
  425. return err;
  426. err = grub_xen_pt_alloc ();
  427. if (err)
  428. return err;
  429. return GRUB_ERR_NONE;
  430. }
  431. static grub_err_t
  432. grub_xen_boot (void)
  433. {
  434. grub_err_t err;
  435. grub_uint64_t nr_pages;
  436. struct gnttab_set_version gnttab_setver;
  437. grub_size_t i;
  438. if (grub_xen_n_allocated_shared_pages)
  439. return grub_error (GRUB_ERR_BUG, "active grants");
  440. err = grub_xen_alloc_boot_data ();
  441. if (err)
  442. return err;
  443. if (xen_state.xen_inf.has_p2m_base)
  444. {
  445. err = grub_xen_p2m_alloc ();
  446. if (err)
  447. return err;
  448. }
  449. err = set_mfns (xen_state.console_pfn);
  450. if (err)
  451. return err;
  452. nr_pages = xen_state.max_addr >> PAGE_SHIFT;
  453. grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
  454. (unsigned long long) xen_state.xen_inf.virt_base,
  455. (unsigned long long) page2offset (nr_pages));
  456. xen_state.map_reloc->area.pfn_start = nr_pages;
  457. generate_page_table (xen_state.virt_mfn_list);
  458. xen_state.state.entry_point = xen_state.xen_inf.entry_point;
  459. *xen_state.virt_start_info = xen_state.next_start;
  460. grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
  461. gnttab_setver.version = 1;
  462. grub_xen_grant_table_op (GNTTABOP_set_version, &gnttab_setver, 1);
  463. for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
  464. grub_xen_shared_info->evtchn_pending[i] = 0;
  465. return grub_relocator_xen_boot (xen_state.relocator, xen_state.state, nr_pages,
  466. xen_state.xen_inf.virt_base <
  467. PAGE_SIZE ? page2offset (nr_pages) : 0,
  468. xen_state.pgtbl_end - 1,
  469. page2offset (xen_state.pgtbl_end - 1) +
  470. xen_state.xen_inf.virt_base);
  471. }
  472. static void
  473. grub_xen_reset (void)
  474. {
  475. grub_relocator_unload (xen_state.relocator);
  476. grub_memset (&xen_state, 0, sizeof (xen_state));
  477. }
  478. static grub_err_t
  479. grub_xen_unload (void)
  480. {
  481. grub_xen_reset ();
  482. grub_dl_unref (my_mod);
  483. return GRUB_ERR_NONE;
  484. }
  485. #define HYPERCALL_INTERFACE_SIZE 32
  486. #ifdef __x86_64__
  487. static grub_uint8_t template[] =
  488. {
  489. 0x51, /* push %rcx */
  490. 0x41, 0x53, /* push %r11 */
  491. 0x48, 0xc7, 0xc0, 0xbb, 0xaa, 0x00, 0x00, /* mov $0xaabb,%rax */
  492. 0x0f, 0x05, /* syscall */
  493. 0x41, 0x5b, /* pop %r11 */
  494. 0x59, /* pop %rcx */
  495. 0xc3 /* ret */
  496. };
  497. static grub_uint8_t template_iret[] =
  498. {
  499. 0x51, /* push %rcx */
  500. 0x41, 0x53, /* push %r11 */
  501. 0x50, /* push %rax */
  502. 0x48, 0xc7, 0xc0, 0x17, 0x00, 0x00, 0x00, /* mov $0x17,%rax */
  503. 0x0f, 0x05 /* syscall */
  504. };
  505. #define CALLNO_OFFSET 6
  506. #else
  507. static grub_uint8_t template[] =
  508. {
  509. 0xb8, 0xbb, 0xaa, 0x00, 0x00, /* mov imm32, %eax */
  510. 0xcd, 0x82, /* int $0x82 */
  511. 0xc3 /* ret */
  512. };
  513. static grub_uint8_t template_iret[] =
  514. {
  515. 0x50, /* push %eax */
  516. 0xb8, 0x17, 0x00, 0x00, 0x00, /* mov $0x17,%eax */
  517. 0xcd, 0x82, /* int $0x82 */
  518. };
  519. #define CALLNO_OFFSET 1
  520. #endif
  521. static void
  522. set_hypercall_interface (grub_uint8_t *tgt, unsigned callno)
  523. {
  524. if (callno == 0x17)
  525. {
  526. grub_memcpy (tgt, template_iret, ARRAY_SIZE (template_iret));
  527. grub_memset (tgt + ARRAY_SIZE (template_iret), 0xcc,
  528. HYPERCALL_INTERFACE_SIZE - ARRAY_SIZE (template_iret));
  529. return;
  530. }
  531. grub_memcpy (tgt, template, ARRAY_SIZE (template));
  532. grub_memset (tgt + ARRAY_SIZE (template), 0xcc,
  533. HYPERCALL_INTERFACE_SIZE - ARRAY_SIZE (template));
  534. tgt[CALLNO_OFFSET] = callno & 0xff;
  535. tgt[CALLNO_OFFSET + 1] = callno >> 8;
  536. }
  537. #ifdef __x86_64__
  538. #define grub_elfXX_load grub_elf64_load
  539. #else
  540. #define grub_elfXX_load grub_elf32_load
  541. #endif
  542. static grub_err_t
  543. grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
  544. int argc, char *argv[])
  545. {
  546. grub_file_t file;
  547. grub_elf_t elf;
  548. grub_err_t err;
  549. void *kern_chunk_src;
  550. grub_relocator_chunk_t ch;
  551. grub_addr_t kern_start;
  552. grub_addr_t kern_end;
  553. if (argc == 0)
  554. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  555. /* Call grub_loader_unset early to avoid it being called by grub_loader_set */
  556. grub_loader_unset ();
  557. grub_xen_reset ();
  558. grub_create_loader_cmdline (argc - 1, argv + 1,
  559. (char *) xen_state.next_start.cmd_line,
  560. sizeof (xen_state.next_start.cmd_line) - 1);
  561. file = grub_file_open (argv[0]);
  562. if (!file)
  563. return grub_errno;
  564. elf = grub_xen_file (file);
  565. if (!elf)
  566. goto fail;
  567. err = grub_xen_get_info (elf, &xen_state.xen_inf);
  568. if (err)
  569. goto fail;
  570. #ifdef __x86_64__
  571. if (xen_state.xen_inf.arch != GRUB_XEN_FILE_X86_64)
  572. #else
  573. if (xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE
  574. && xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
  575. #endif
  576. {
  577. grub_error (GRUB_ERR_BAD_OS, "incompatible architecture: %d",
  578. xen_state.xen_inf.arch);
  579. goto fail;
  580. }
  581. if (xen_state.xen_inf.virt_base & (PAGE_SIZE - 1))
  582. {
  583. grub_error (GRUB_ERR_BAD_OS, "unaligned virt_base");
  584. goto fail;
  585. }
  586. grub_dprintf ("xen", "virt_base = %llx, entry = %llx\n",
  587. (unsigned long long) xen_state.xen_inf.virt_base,
  588. (unsigned long long) xen_state.xen_inf.entry_point);
  589. xen_state.relocator = grub_relocator_new ();
  590. if (!xen_state.relocator)
  591. goto fail;
  592. kern_start = xen_state.xen_inf.kern_start - xen_state.xen_inf.paddr_offset;
  593. kern_end = xen_state.xen_inf.kern_end - xen_state.xen_inf.paddr_offset;
  594. if (xen_state.xen_inf.has_hypercall_page)
  595. {
  596. grub_dprintf ("xen", "hypercall page at 0x%llx\n",
  597. (unsigned long long) xen_state.xen_inf.hypercall_page);
  598. kern_start = grub_min (kern_start, xen_state.xen_inf.hypercall_page -
  599. xen_state.xen_inf.virt_base);
  600. kern_end = grub_max (kern_end, xen_state.xen_inf.hypercall_page -
  601. xen_state.xen_inf.virt_base + PAGE_SIZE);
  602. }
  603. xen_state.max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
  604. err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start,
  605. kern_end - kern_start);
  606. if (err)
  607. goto fail;
  608. kern_chunk_src = get_virtual_current_address (ch);
  609. grub_dprintf ("xen", "paddr_offset = 0x%llx\n",
  610. (unsigned long long) xen_state.xen_inf.paddr_offset);
  611. grub_dprintf ("xen", "kern_start = 0x%llx, kern_end = 0x%llx\n",
  612. (unsigned long long) xen_state.xen_inf.kern_start,
  613. (unsigned long long) xen_state.xen_inf.kern_end);
  614. err = grub_elfXX_load (elf, argv[0],
  615. (grub_uint8_t *) kern_chunk_src - kern_start
  616. - xen_state.xen_inf.paddr_offset, 0, 0, 0);
  617. if (xen_state.xen_inf.has_hypercall_page)
  618. {
  619. unsigned i;
  620. for (i = 0; i < PAGE_SIZE / HYPERCALL_INTERFACE_SIZE; i++)
  621. set_hypercall_interface ((grub_uint8_t *) kern_chunk_src +
  622. i * HYPERCALL_INTERFACE_SIZE +
  623. xen_state.xen_inf.hypercall_page -
  624. xen_state.xen_inf.virt_base - kern_start, i);
  625. }
  626. if (err)
  627. goto fail;
  628. grub_dl_ref (my_mod);
  629. xen_state.loaded = 1;
  630. grub_loader_set (grub_xen_boot, grub_xen_unload, 0);
  631. goto fail;
  632. fail:
  633. /* grub_errno might be clobbered by further calls, save the error reason. */
  634. err = grub_errno;
  635. if (elf)
  636. grub_elf_close (elf);
  637. else if (file)
  638. grub_file_close (file);
  639. if (err != GRUB_ERR_NONE)
  640. grub_xen_reset ();
  641. return err;
  642. }
  643. static grub_err_t
  644. grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
  645. int argc, char *argv[])
  646. {
  647. grub_size_t size = 0;
  648. grub_err_t err;
  649. struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
  650. grub_relocator_chunk_t ch;
  651. if (argc == 0)
  652. {
  653. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  654. goto fail;
  655. }
  656. if (!xen_state.loaded)
  657. {
  658. grub_error (GRUB_ERR_BAD_ARGUMENT,
  659. N_("you need to load the kernel first"));
  660. goto fail;
  661. }
  662. if (xen_state.next_start.mod_start || xen_state.next_start.mod_len)
  663. {
  664. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
  665. goto fail;
  666. }
  667. if (xen_state.xen_inf.unmapped_initrd)
  668. {
  669. err = grub_xen_alloc_boot_data ();
  670. if (err)
  671. goto fail;
  672. }
  673. if (grub_initrd_init (argc, argv, &initrd_ctx))
  674. goto fail;
  675. size = grub_get_initrd_size (&initrd_ctx);
  676. if (size)
  677. {
  678. err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
  679. xen_state.max_addr, size);
  680. if (err)
  681. goto fail;
  682. if (grub_initrd_load (&initrd_ctx, argv,
  683. get_virtual_current_address (ch)))
  684. goto fail;
  685. }
  686. xen_state.next_start.mod_len = size;
  687. if (xen_state.xen_inf.unmapped_initrd)
  688. {
  689. xen_state.next_start.flags |= SIF_MOD_START_PFN;
  690. xen_state.next_start.mod_start = xen_state.max_addr >> PAGE_SHIFT;
  691. }
  692. else
  693. xen_state.next_start.mod_start =
  694. xen_state.max_addr + xen_state.xen_inf.virt_base;
  695. grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
  696. (unsigned) (xen_state.max_addr + xen_state.xen_inf.virt_base),
  697. (unsigned) size);
  698. xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
  699. fail:
  700. grub_initrd_close (&initrd_ctx);
  701. return grub_errno;
  702. }
  703. static grub_err_t
  704. grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
  705. int argc, char *argv[])
  706. {
  707. grub_size_t size = 0;
  708. grub_err_t err;
  709. grub_relocator_chunk_t ch;
  710. grub_size_t cmdline_len;
  711. int nounzip = 0;
  712. grub_file_t file;
  713. if (argc == 0)
  714. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  715. if (grub_strcmp (argv[0], "--nounzip") == 0)
  716. {
  717. argv++;
  718. argc--;
  719. nounzip = 1;
  720. }
  721. if (argc == 0)
  722. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  723. if (!xen_state.loaded)
  724. {
  725. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  726. N_("you need to load the kernel first"));
  727. }
  728. if ((xen_state.next_start.mod_start || xen_state.next_start.mod_len) &&
  729. !xen_state.module_info_page)
  730. {
  731. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
  732. }
  733. /* Leave one space for terminator. */
  734. if (xen_state.n_modules >= MAX_MODULES - 1)
  735. {
  736. return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many modules");
  737. }
  738. if (!xen_state.module_info_page)
  739. {
  740. xen_state.xen_inf.unmapped_initrd = 0;
  741. xen_state.n_modules = 0;
  742. xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
  743. xen_state.modules_target_start = xen_state.max_addr;
  744. xen_state.next_start.mod_start =
  745. xen_state.max_addr + xen_state.xen_inf.virt_base;
  746. xen_state.next_start.flags |= SIF_MULTIBOOT_MOD;
  747. err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
  748. xen_state.max_addr, MAX_MODULES
  749. *
  750. sizeof (xen_state.module_info_page
  751. [0]));
  752. if (err)
  753. return err;
  754. xen_state.module_info_page = get_virtual_current_address (ch);
  755. grub_memset (xen_state.module_info_page, 0, MAX_MODULES
  756. * sizeof (xen_state.module_info_page[0]));
  757. xen_state.max_addr +=
  758. MAX_MODULES * sizeof (xen_state.module_info_page[0]);
  759. }
  760. xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
  761. if (nounzip)
  762. grub_file_filter_disable_compression ();
  763. file = grub_file_open (argv[0]);
  764. if (!file)
  765. return grub_errno;
  766. size = grub_file_size (file);
  767. cmdline_len = grub_loader_cmdline_size (argc - 1, argv + 1);
  768. err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
  769. xen_state.max_addr, cmdline_len);
  770. if (err)
  771. goto fail;
  772. grub_create_loader_cmdline (argc - 1, argv + 1,
  773. get_virtual_current_address (ch), cmdline_len);
  774. xen_state.module_info_page[xen_state.n_modules].cmdline =
  775. xen_state.max_addr - xen_state.modules_target_start;
  776. xen_state.max_addr = ALIGN_UP (xen_state.max_addr + cmdline_len, PAGE_SIZE);
  777. if (size)
  778. {
  779. err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
  780. xen_state.max_addr, size);
  781. if (err)
  782. goto fail;
  783. if (grub_file_read (file, get_virtual_current_address (ch), size)
  784. != (grub_ssize_t) size)
  785. {
  786. if (!grub_errno)
  787. grub_error (GRUB_ERR_FILE_READ_ERROR,
  788. N_("premature end of file %s"), argv[0]);
  789. goto fail;
  790. }
  791. }
  792. xen_state.next_start.mod_len =
  793. xen_state.max_addr + size - xen_state.modules_target_start;
  794. xen_state.module_info_page[xen_state.n_modules].mod_start =
  795. xen_state.max_addr - xen_state.modules_target_start;
  796. xen_state.module_info_page[xen_state.n_modules].mod_end =
  797. xen_state.max_addr + size - xen_state.modules_target_start;
  798. xen_state.n_modules++;
  799. grub_dprintf ("xen", "module, addr=0x%x, size=0x%x\n",
  800. (unsigned) xen_state.max_addr, (unsigned) size);
  801. xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
  802. fail:
  803. grub_file_close (file);
  804. return grub_errno;
  805. }
  806. static grub_command_t cmd_xen, cmd_initrd, cmd_module, cmd_multiboot;
  807. GRUB_MOD_INIT (xen)
  808. {
  809. cmd_xen = grub_register_command ("linux", grub_cmd_xen,
  810. 0, N_("Load Linux."));
  811. cmd_multiboot = grub_register_command ("multiboot", grub_cmd_xen,
  812. 0, N_("Load Linux."));
  813. cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
  814. 0, N_("Load initrd."));
  815. cmd_module = grub_register_command ("module", grub_cmd_module,
  816. 0, N_("Load module."));
  817. my_mod = mod;
  818. }
  819. GRUB_MOD_FINI (xen)
  820. {
  821. grub_unregister_command (cmd_xen);
  822. grub_unregister_command (cmd_initrd);
  823. grub_unregister_command (cmd_multiboot);
  824. grub_unregister_command (cmd_module);
  825. }