pvh.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2018 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/kernel.h>
  19. #include <grub/misc.h>
  20. #include <grub/memory.h>
  21. #include <grub/mm.h>
  22. #include <grub/i386/cpuid.h>
  23. #include <grub/i386/io.h>
  24. #include <grub/xen.h>
  25. #include <xen/hvm/start_info.h>
  26. #include <grub/i386/linux.h>
  27. #include <grub/machine/kernel.h>
  28. #include <grub/machine/memory.h>
  29. #include <xen/hvm/params.h>
  30. #include <xen/memory.h>
  31. #define XEN_MEMORY_MAP_SIZE 128
  32. grub_uint64_t grub_rsdp_addr;
  33. static char hypercall_page[GRUB_XEN_PAGE_SIZE]
  34. __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
  35. static grub_uint32_t xen_cpuid_base;
  36. static struct start_info grub_xen_start_page;
  37. static struct grub_e820_mmap_entry map[XEN_MEMORY_MAP_SIZE];
  38. static unsigned int nr_map_entries;
  39. static void
  40. grub_xen_cons_msg (const char *msg)
  41. {
  42. const char *c;
  43. for (c = msg; *c; c++)
  44. grub_outb (*c, XEN_HVM_DEBUGCONS_IOPORT);
  45. }
  46. static void
  47. grub_xen_panic (const char *msg)
  48. {
  49. grub_xen_cons_msg (msg);
  50. grub_xen_cons_msg ("System halted!\n");
  51. asm volatile ("cli");
  52. while (1)
  53. {
  54. asm volatile ("hlt");
  55. }
  56. }
  57. static void
  58. grub_xen_cpuid_base (void)
  59. {
  60. grub_uint32_t base, eax, signature[3];
  61. for (base = 0x40000000; base < 0x40010000; base += 0x100)
  62. {
  63. grub_cpuid (base, eax, signature[0], signature[1], signature[2]);
  64. if (!grub_memcmp ("XenVMMXenVMM", signature, 12) && (eax - base) >= 2)
  65. {
  66. xen_cpuid_base = base;
  67. return;
  68. }
  69. }
  70. grub_xen_panic ("Found no Xen signature!\n");
  71. }
  72. static void
  73. grub_xen_setup_hypercall_page (void)
  74. {
  75. grub_uint32_t msr, addr, eax, ebx, ecx, edx;
  76. /* Get base address of Xen-specific MSRs. */
  77. grub_cpuid (xen_cpuid_base + 2, eax, ebx, ecx, edx);
  78. msr = ebx;
  79. addr = (grub_uint32_t) (&hypercall_page);
  80. /* Specify hypercall page address for Xen. */
  81. asm volatile ("wrmsr" : : "c" (msr), "a" (addr), "d" (0) : "memory");
  82. }
  83. int
  84. grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
  85. grub_uint32_t a1, grub_uint32_t a2,
  86. grub_uint32_t a3, grub_uint32_t a4,
  87. grub_uint32_t a5 __attribute__ ((unused)))
  88. {
  89. grub_uint32_t res;
  90. asm volatile ("call *%[callno]"
  91. : "=a" (res), "+b" (a0), "+c" (a1), "+d" (a2),
  92. "+S" (a3), "+D" (a4)
  93. : [callno] "a" (&hypercall_page[callno * 32])
  94. : "memory");
  95. return res;
  96. }
  97. static grub_uint32_t
  98. grub_xen_get_param (int idx)
  99. {
  100. struct xen_hvm_param xhv;
  101. int r;
  102. xhv.domid = DOMID_SELF;
  103. xhv.index = idx;
  104. r = grub_xen_hypercall (__HYPERVISOR_hvm_op, HVMOP_get_param,
  105. (grub_uint32_t) (&xhv), 0, 0, 0, 0);
  106. if (r < 0)
  107. grub_xen_panic ("Could not get parameter from Xen!\n");
  108. return xhv.value;
  109. }
  110. static void *
  111. grub_xen_add_physmap (unsigned int space, void *addr)
  112. {
  113. struct xen_add_to_physmap xatp;
  114. xatp.domid = DOMID_SELF;
  115. xatp.idx = 0;
  116. xatp.space = space;
  117. xatp.gpfn = (grub_addr_t) addr >> GRUB_XEN_LOG_PAGE_SIZE;
  118. if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_add_to_physmap,
  119. (grub_uint32_t) (&xatp), 0, 0, 0, 0))
  120. grub_xen_panic ("Memory_op hypercall failed!\n");
  121. return addr;
  122. }
  123. static void
  124. grub_xen_sort_mmap (void)
  125. {
  126. grub_uint64_t from, to;
  127. unsigned int i;
  128. struct grub_e820_mmap_entry tmp;
  129. /* Align map entries to page boundaries. */
  130. for (i = 0; i < nr_map_entries; i++)
  131. {
  132. from = map[i].addr;
  133. to = from + map[i].len;
  134. if (map[i].type == GRUB_MEMORY_AVAILABLE)
  135. {
  136. from = ALIGN_UP (from, GRUB_XEN_PAGE_SIZE);
  137. to = ALIGN_DOWN (to, GRUB_XEN_PAGE_SIZE);
  138. }
  139. else
  140. {
  141. from = ALIGN_DOWN (from, GRUB_XEN_PAGE_SIZE);
  142. to = ALIGN_UP (to, GRUB_XEN_PAGE_SIZE);
  143. }
  144. map[i].addr = from;
  145. map[i].len = to - from;
  146. }
  147. again:
  148. /* Sort entries by start address. */
  149. for (i = 1; i < nr_map_entries; i++)
  150. {
  151. if (map[i].addr >= map[i - 1].addr)
  152. continue;
  153. tmp = map[i];
  154. map[i] = map[i - 1];
  155. map[i - 1] = tmp;
  156. i = 0;
  157. }
  158. /* Detect overlapping areas. */
  159. for (i = 1; i < nr_map_entries; i++)
  160. {
  161. if (map[i].addr >= map[i - 1].addr + map[i - 1].len)
  162. continue;
  163. tmp = map[i - 1];
  164. map[i - 1].len = map[i].addr - map[i - 1].addr;
  165. if (map[i].addr + map[i].len >= tmp.addr + tmp.len)
  166. continue;
  167. if (nr_map_entries < ARRAY_SIZE (map))
  168. {
  169. map[nr_map_entries].addr = map[i].addr + map[i].len;
  170. map[nr_map_entries].len = tmp.addr + tmp.len - map[nr_map_entries].addr;
  171. map[nr_map_entries].type = tmp.type;
  172. nr_map_entries++;
  173. goto again;
  174. }
  175. }
  176. /* Merge adjacent entries. */
  177. for (i = 1; i < nr_map_entries; i++)
  178. {
  179. if (map[i].type == map[i - 1].type &&
  180. map[i].addr == map[i - 1].addr + map[i - 1].len)
  181. {
  182. map[i - 1].len += map[i].len;
  183. map[i] = map[nr_map_entries - 1];
  184. nr_map_entries--;
  185. goto again;
  186. }
  187. }
  188. }
  189. static void
  190. grub_xen_get_mmap (void)
  191. {
  192. struct xen_memory_map memmap;
  193. memmap.nr_entries = ARRAY_SIZE (map);
  194. set_xen_guest_handle (memmap.buffer, map);
  195. if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_memory_map,
  196. (grub_uint32_t) (&memmap), 0, 0, 0, 0))
  197. grub_xen_panic ("Could not get memory map from Xen!\n");
  198. nr_map_entries = memmap.nr_entries;
  199. grub_xen_sort_mmap ();
  200. }
  201. static void
  202. grub_xen_set_mmap (void)
  203. {
  204. struct xen_foreign_memory_map memmap;
  205. memmap.domid = DOMID_SELF;
  206. memmap.map.nr_entries = nr_map_entries;
  207. set_xen_guest_handle (memmap.map.buffer, map);
  208. grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_set_memory_map,
  209. (grub_uint32_t) (&memmap), 0, 0, 0, 0);
  210. }
  211. static void
  212. grub_xen_mm_init_regions (void)
  213. {
  214. grub_uint64_t modend, from, to;
  215. unsigned int i;
  216. modend = grub_modules_get_end ();
  217. for (i = 0; i < nr_map_entries; i++)
  218. {
  219. if (map[i].type != GRUB_MEMORY_AVAILABLE)
  220. continue;
  221. from = map[i].addr;
  222. to = from + map[i].len;
  223. if (from < modend)
  224. from = modend;
  225. if (from >= to || from >= (1ULL << 32))
  226. continue;
  227. if (to > (1ULL << 32))
  228. to = 1ULL << 32;
  229. grub_mm_init_region ((void *) (grub_addr_t) from, to - from);
  230. }
  231. }
  232. static grub_uint64_t
  233. grub_xen_find_page (grub_uint64_t start)
  234. {
  235. unsigned int i, j;
  236. grub_uint64_t last = start;
  237. /*
  238. * Try to find a e820 map hole below 4G.
  239. * Relies on page-aligned entries (addr and len) and input (start).
  240. */
  241. for (i = 0; i < nr_map_entries; i++)
  242. {
  243. if (last > map[i].addr + map[i].len)
  244. continue;
  245. if (last < map[i].addr)
  246. return last;
  247. if ((map[i].addr >> 32) || ((map[i].addr + map[i].len) >> 32))
  248. break;
  249. last = map[i].addr + map[i].len;
  250. }
  251. if (i == nr_map_entries)
  252. return last;
  253. /* No hole found, use the highest RAM page below 4G and reserve it. */
  254. if (nr_map_entries == ARRAY_SIZE (map))
  255. grub_xen_panic ("Memory map size limit reached!\n");
  256. for (i = 0, j = 0; i < nr_map_entries; i++)
  257. {
  258. if (map[i].type != GRUB_MEMORY_AVAILABLE)
  259. continue;
  260. if (map[i].addr >> 32)
  261. break;
  262. j = i;
  263. if ((map[i].addr + map[i].len) >> 32)
  264. break;
  265. }
  266. if (map[j].type != GRUB_MEMORY_AVAILABLE)
  267. grub_xen_panic ("No free memory page found!\n");
  268. if ((map[j].addr + map[j].len) >> 32)
  269. last = (1ULL << 32) - GRUB_XEN_PAGE_SIZE;
  270. else
  271. last = map[j].addr + map[j].len - GRUB_XEN_PAGE_SIZE;
  272. map[nr_map_entries].addr = last;
  273. map[nr_map_entries].len = GRUB_XEN_PAGE_SIZE;
  274. map[nr_map_entries].type = GRUB_MEMORY_RESERVED;
  275. nr_map_entries++;
  276. grub_xen_sort_mmap ();
  277. return last;
  278. }
  279. void
  280. grub_xen_setup_pvh (void)
  281. {
  282. grub_addr_t par;
  283. grub_xen_cpuid_base ();
  284. grub_xen_setup_hypercall_page ();
  285. grub_xen_get_mmap ();
  286. /* Setup Xen data. */
  287. grub_xen_start_page_addr = &grub_xen_start_page;
  288. par = grub_xen_get_param (HVM_PARAM_CONSOLE_PFN);
  289. grub_xen_start_page_addr->console.domU.mfn = par;
  290. grub_xen_xcons = (void *) (grub_addr_t) (par << GRUB_XEN_LOG_PAGE_SIZE);
  291. par = grub_xen_get_param (HVM_PARAM_CONSOLE_EVTCHN);
  292. grub_xen_start_page_addr->console.domU.evtchn = par;
  293. par = grub_xen_get_param (HVM_PARAM_STORE_PFN);
  294. grub_xen_start_page_addr->store_mfn = par;
  295. grub_xen_xenstore = (void *) (grub_addr_t) (par << GRUB_XEN_LOG_PAGE_SIZE);
  296. par = grub_xen_get_param (HVM_PARAM_STORE_EVTCHN);
  297. grub_xen_start_page_addr->store_evtchn = par;
  298. par = grub_xen_find_page (0);
  299. grub_xen_grant_table = grub_xen_add_physmap (XENMAPSPACE_grant_table,
  300. (void *) par);
  301. par = grub_xen_find_page (par + GRUB_XEN_PAGE_SIZE);
  302. grub_xen_shared_info = grub_xen_add_physmap (XENMAPSPACE_shared_info,
  303. (void *) par);
  304. grub_xen_set_mmap ();
  305. grub_xen_mm_init_regions ();
  306. grub_rsdp_addr = pvh_start_info->rsdp_paddr;
  307. }
  308. grub_err_t
  309. grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
  310. {
  311. unsigned int i;
  312. for (i = 0; i < nr_map_entries; i++)
  313. {
  314. if (map[i].len && hook (map[i].addr, map[i].len, map[i].type, hook_data))
  315. break;
  316. }
  317. return GRUB_ERR_NONE;
  318. }