Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Machine specific setup for xen |
| 3 | * |
| 4 | * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007 |
| 5 | */ |
| 6 | |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/pm.h> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 11 | #include <linux/memblock.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 12 | |
| 13 | #include <asm/elf.h> |
Roland McGrath | 6c3652e | 2008-01-30 13:30:42 +0100 | [diff] [blame] | 14 | #include <asm/vdso.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 15 | #include <asm/e820.h> |
| 16 | #include <asm/setup.h> |
Jeremy Fitzhardinge | b792c75 | 2008-06-16 14:54:49 -0700 | [diff] [blame] | 17 | #include <asm/acpi.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 18 | #include <asm/xen/hypervisor.h> |
| 19 | #include <asm/xen/hypercall.h> |
| 20 | |
Ian Campbell | 45263cb | 2010-10-25 16:32:29 -0700 | [diff] [blame] | 21 | #include <xen/xen.h> |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 22 | #include <xen/page.h> |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 23 | #include <xen/interface/callback.h> |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 24 | #include <xen/interface/memory.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 25 | #include <xen/interface/physdev.h> |
| 26 | #include <xen/features.h> |
| 27 | |
| 28 | #include "xen-ops.h" |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 29 | #include "vdso.h" |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 30 | |
| 31 | /* These are code, but not functions. Defined in entry.S */ |
| 32 | extern const char xen_hypervisor_callback[]; |
| 33 | extern const char xen_failsafe_callback[]; |
Tej | f63c2f2 | 2008-12-16 11:56:06 -0800 | [diff] [blame] | 34 | extern void xen_sysenter_target(void); |
| 35 | extern void xen_syscall_target(void); |
| 36 | extern void xen_syscall32_target(void); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 37 | |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 38 | /* Amount of extra memory space we add to the e820 ranges */ |
| 39 | phys_addr_t xen_extra_mem_start, xen_extra_mem_size; |
| 40 | |
Jeremy Fitzhardinge | 698bb8d | 2010-09-14 10:19:14 -0700 | [diff] [blame] | 41 | /* |
| 42 | * The maximum amount of extra memory compared to the base size. The |
| 43 | * main scaling factor is the size of struct page. At extreme ratios |
| 44 | * of base:extra, all the base memory can be filled with page |
| 45 | * structures for the extra memory, leaving no space for anything |
| 46 | * else. |
| 47 | * |
| 48 | * 10x seems like a reasonable balance between scaling flexibility and |
| 49 | * leaving a practically usable system. |
| 50 | */ |
| 51 | #define EXTRA_MEM_RATIO (10) |
| 52 | |
Daniel Kiper | ae15a3b | 2011-05-04 20:17:21 +0200 | [diff] [blame^] | 53 | static void __init xen_add_extra_mem(unsigned long pages) |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 54 | { |
Konrad Rzeszutek Wilk | 6eaa412 | 2011-01-18 20:09:41 -0500 | [diff] [blame] | 55 | unsigned long pfn; |
| 56 | |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 57 | u64 size = (u64)pages * PAGE_SIZE; |
Jeremy Fitzhardinge | 3654581 | 2010-09-29 16:54:33 -0700 | [diff] [blame] | 58 | u64 extra_start = xen_extra_mem_start + xen_extra_mem_size; |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 59 | |
| 60 | if (!pages) |
| 61 | return; |
| 62 | |
Jeremy Fitzhardinge | 3654581 | 2010-09-29 16:54:33 -0700 | [diff] [blame] | 63 | e820_add_region(extra_start, size, E820_RAM); |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 64 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); |
| 65 | |
Linus Torvalds | 520045d | 2010-10-26 18:20:19 -0700 | [diff] [blame] | 66 | memblock_x86_reserve_range(extra_start, extra_start + size, "XEN EXTRA"); |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 67 | |
| 68 | xen_extra_mem_size += size; |
Jeremy Fitzhardinge | 2f7acb2 | 2010-09-15 13:32:49 -0700 | [diff] [blame] | 69 | |
Jeremy Fitzhardinge | 3654581 | 2010-09-29 16:54:33 -0700 | [diff] [blame] | 70 | xen_max_p2m_pfn = PFN_DOWN(extra_start + size); |
Konrad Rzeszutek Wilk | 6eaa412 | 2011-01-18 20:09:41 -0500 | [diff] [blame] | 71 | |
| 72 | for (pfn = PFN_DOWN(extra_start); pfn <= xen_max_p2m_pfn; pfn++) |
| 73 | __set_phys_to_machine(pfn, INVALID_P2M_ENTRY); |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Jeremy Fitzhardinge | f89e048 | 2009-09-16 12:38:33 -0700 | [diff] [blame] | 76 | static unsigned long __init xen_release_chunk(phys_addr_t start_addr, |
| 77 | phys_addr_t end_addr) |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 78 | { |
| 79 | struct xen_memory_reservation reservation = { |
| 80 | .address_bits = 0, |
| 81 | .extent_order = 0, |
| 82 | .domid = DOMID_SELF |
| 83 | }; |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 84 | unsigned long start, end; |
Jeremy Fitzhardinge | f89e048 | 2009-09-16 12:38:33 -0700 | [diff] [blame] | 85 | unsigned long len = 0; |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 86 | unsigned long pfn; |
| 87 | int ret; |
| 88 | |
| 89 | start = PFN_UP(start_addr); |
Jeremy Fitzhardinge | f89e048 | 2009-09-16 12:38:33 -0700 | [diff] [blame] | 90 | end = PFN_DOWN(end_addr); |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 91 | |
| 92 | if (end <= start) |
| 93 | return 0; |
| 94 | |
Jeremy Fitzhardinge | f89e048 | 2009-09-16 12:38:33 -0700 | [diff] [blame] | 95 | printk(KERN_INFO "xen_release_chunk: looking at area pfn %lx-%lx: ", |
| 96 | start, end); |
| 97 | for(pfn = start; pfn < end; pfn++) { |
| 98 | unsigned long mfn = pfn_to_mfn(pfn); |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 99 | |
Jeremy Fitzhardinge | f89e048 | 2009-09-16 12:38:33 -0700 | [diff] [blame] | 100 | /* Make sure pfn exists to start with */ |
| 101 | if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn) |
| 102 | continue; |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 103 | |
Jeremy Fitzhardinge | f89e048 | 2009-09-16 12:38:33 -0700 | [diff] [blame] | 104 | set_xen_guest_handle(reservation.extent_start, &mfn); |
| 105 | reservation.nr_extents = 1; |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 106 | |
Jeremy Fitzhardinge | f89e048 | 2009-09-16 12:38:33 -0700 | [diff] [blame] | 107 | ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, |
| 108 | &reservation); |
| 109 | WARN(ret != 1, "Failed to release memory %lx-%lx err=%d\n", |
| 110 | start, end, ret); |
| 111 | if (ret == 1) { |
Konrad Rzeszutek Wilk | 6eaa412 | 2011-01-18 20:09:41 -0500 | [diff] [blame] | 112 | __set_phys_to_machine(pfn, INVALID_P2M_ENTRY); |
Jeremy Fitzhardinge | f89e048 | 2009-09-16 12:38:33 -0700 | [diff] [blame] | 113 | len++; |
| 114 | } |
| 115 | } |
| 116 | printk(KERN_CONT "%ld pages freed\n", len); |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 117 | |
| 118 | return len; |
| 119 | } |
| 120 | |
Jeremy Fitzhardinge | f89e048 | 2009-09-16 12:38:33 -0700 | [diff] [blame] | 121 | static unsigned long __init xen_return_unused_memory(unsigned long max_pfn, |
| 122 | const struct e820map *e820) |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 123 | { |
Jeremy Fitzhardinge | f89e048 | 2009-09-16 12:38:33 -0700 | [diff] [blame] | 124 | phys_addr_t max_addr = PFN_PHYS(max_pfn); |
Ian Campbell | 9ec23a7f | 2010-10-28 11:32:29 -0700 | [diff] [blame] | 125 | phys_addr_t last_end = ISA_END_ADDRESS; |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 126 | unsigned long released = 0; |
| 127 | int i; |
| 128 | |
Ian Campbell | 9ec23a7f | 2010-10-28 11:32:29 -0700 | [diff] [blame] | 129 | /* Free any unused memory above the low 1Mbyte. */ |
Jeremy Fitzhardinge | f89e048 | 2009-09-16 12:38:33 -0700 | [diff] [blame] | 130 | for (i = 0; i < e820->nr_map && last_end < max_addr; i++) { |
| 131 | phys_addr_t end = e820->map[i].addr; |
| 132 | end = min(max_addr, end); |
| 133 | |
Ian Campbell | 9ec23a7f | 2010-10-28 11:32:29 -0700 | [diff] [blame] | 134 | if (last_end < end) |
| 135 | released += xen_release_chunk(last_end, end); |
| 136 | last_end = max(last_end, e820->map[i].addr + e820->map[i].size); |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Jeremy Fitzhardinge | f89e048 | 2009-09-16 12:38:33 -0700 | [diff] [blame] | 139 | if (last_end < max_addr) |
| 140 | released += xen_release_chunk(last_end, max_addr); |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 141 | |
| 142 | printk(KERN_INFO "released %ld pages of unused memory\n", released); |
| 143 | return released; |
| 144 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 145 | |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 146 | static unsigned long __init xen_set_identity(const struct e820entry *list, |
| 147 | ssize_t map_size) |
| 148 | { |
| 149 | phys_addr_t last = xen_initial_domain() ? 0 : ISA_END_ADDRESS; |
| 150 | phys_addr_t start_pci = last; |
| 151 | const struct e820entry *entry; |
| 152 | unsigned long identity = 0; |
| 153 | int i; |
| 154 | |
| 155 | for (i = 0, entry = list; i < map_size; i++, entry++) { |
| 156 | phys_addr_t start = entry->addr; |
| 157 | phys_addr_t end = start + entry->size; |
| 158 | |
| 159 | if (start < last) |
| 160 | start = last; |
| 161 | |
| 162 | if (end <= start) |
| 163 | continue; |
| 164 | |
| 165 | /* Skip over the 1MB region. */ |
| 166 | if (last > end) |
| 167 | continue; |
| 168 | |
| 169 | if (entry->type == E820_RAM) { |
| 170 | if (start > start_pci) |
| 171 | identity += set_phys_range_identity( |
| 172 | PFN_UP(start_pci), PFN_DOWN(start)); |
| 173 | |
| 174 | /* Without saving 'last' we would gooble RAM too |
| 175 | * at the end of the loop. */ |
| 176 | last = end; |
| 177 | start_pci = end; |
| 178 | continue; |
| 179 | } |
| 180 | start_pci = min(start, start_pci); |
| 181 | last = end; |
| 182 | } |
| 183 | if (last > start_pci) |
| 184 | identity += set_phys_range_identity( |
| 185 | PFN_UP(start_pci), PFN_DOWN(last)); |
| 186 | return identity; |
| 187 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 188 | /** |
| 189 | * machine_specific_memory_setup - Hook for machine specific memory setup. |
| 190 | **/ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 191 | char * __init xen_memory_setup(void) |
| 192 | { |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 193 | static struct e820entry map[E820MAX] __initdata; |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 194 | static struct e820entry map_raw[E820MAX] __initdata; |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 195 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 196 | unsigned long max_pfn = xen_start_info->nr_pages; |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 197 | unsigned long long mem_end; |
| 198 | int rc; |
| 199 | struct xen_memory_map memmap; |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 200 | unsigned long extra_pages = 0; |
Jeremy Fitzhardinge | 698bb8d | 2010-09-14 10:19:14 -0700 | [diff] [blame] | 201 | unsigned long extra_limit; |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 202 | unsigned long identity_pages = 0; |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 203 | int i; |
Ian Campbell | 9e9a5fc | 2010-09-02 16:16:00 +0100 | [diff] [blame] | 204 | int op; |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 205 | |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 206 | max_pfn = min(MAX_DOMAIN_PAGES, max_pfn); |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 207 | mem_end = PFN_PHYS(max_pfn); |
| 208 | |
| 209 | memmap.nr_entries = E820MAX; |
| 210 | set_xen_guest_handle(memmap.buffer, map); |
| 211 | |
Ian Campbell | 9e9a5fc | 2010-09-02 16:16:00 +0100 | [diff] [blame] | 212 | op = xen_initial_domain() ? |
| 213 | XENMEM_machine_memory_map : |
| 214 | XENMEM_memory_map; |
| 215 | rc = HYPERVISOR_memory_op(op, &memmap); |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 216 | if (rc == -ENOSYS) { |
Ian Campbell | 9ec23a7f | 2010-10-28 11:32:29 -0700 | [diff] [blame] | 217 | BUG_ON(xen_initial_domain()); |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 218 | memmap.nr_entries = 1; |
| 219 | map[0].addr = 0ULL; |
| 220 | map[0].size = mem_end; |
| 221 | /* 8MB slack (to balance backend allocations). */ |
| 222 | map[0].size += 8ULL << 20; |
| 223 | map[0].type = E820_RAM; |
| 224 | rc = 0; |
| 225 | } |
| 226 | BUG_ON(rc); |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 227 | |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 228 | memcpy(map_raw, map, sizeof(map)); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 229 | e820.nr_map = 0; |
Stefano Stabellini | 24bdb0b | 2011-04-12 12:19:52 +0100 | [diff] [blame] | 230 | xen_extra_mem_start = max((1ULL << 32), mem_end); |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 231 | for (i = 0; i < memmap.nr_entries; i++) { |
Stefano Stabellini | 7cb31b7 | 2011-01-27 10:13:25 -0500 | [diff] [blame] | 232 | unsigned long long end; |
Jeremy Fitzhardinge | be5bf9f | 2008-06-16 14:54:46 -0700 | [diff] [blame] | 233 | |
Stefano Stabellini | 7cb31b7 | 2011-01-27 10:13:25 -0500 | [diff] [blame] | 234 | /* Guard against non-page aligned E820 entries. */ |
| 235 | if (map[i].type == E820_RAM) |
| 236 | map[i].size -= (map[i].size + map[i].addr) % PAGE_SIZE; |
| 237 | |
| 238 | end = map[i].addr + map[i].size; |
Jeremy Fitzhardinge | c2d0879 | 2010-11-22 16:31:35 -0800 | [diff] [blame] | 239 | if (map[i].type == E820_RAM && end > mem_end) { |
| 240 | /* RAM off the end - may be partially included */ |
| 241 | u64 delta = min(map[i].size, end - mem_end); |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 242 | |
Jeremy Fitzhardinge | c2d0879 | 2010-11-22 16:31:35 -0800 | [diff] [blame] | 243 | map[i].size -= delta; |
| 244 | end -= delta; |
Jeremy Fitzhardinge | 3654581 | 2010-09-29 16:54:33 -0700 | [diff] [blame] | 245 | |
Jeremy Fitzhardinge | c2d0879 | 2010-11-22 16:31:35 -0800 | [diff] [blame] | 246 | extra_pages += PFN_DOWN(delta); |
Zhang, Fengzhe | 2f14ddc | 2011-02-16 22:26:20 +0800 | [diff] [blame] | 247 | /* |
| 248 | * Set RAM below 4GB that is not for us to be unusable. |
| 249 | * This prevents "System RAM" address space from being |
| 250 | * used as potential resource for I/O address (happens |
| 251 | * when 'allocate_resource' is called). |
| 252 | */ |
Konrad Rzeszutek Wilk | 86b3212 | 2011-03-09 22:03:16 -0500 | [diff] [blame] | 253 | if (delta && |
| 254 | (xen_initial_domain() && end < 0x100000000ULL)) |
Zhang, Fengzhe | 2f14ddc | 2011-02-16 22:26:20 +0800 | [diff] [blame] | 255 | e820_add_region(end, delta, E820_UNUSABLE); |
Jeremy Fitzhardinge | 3654581 | 2010-09-29 16:54:33 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Jeremy Fitzhardinge | c2d0879 | 2010-11-22 16:31:35 -0800 | [diff] [blame] | 258 | if (map[i].size > 0 && end > xen_extra_mem_start) |
Jeremy Fitzhardinge | 36bc251 | 2010-09-02 17:07:03 -0700 | [diff] [blame] | 259 | xen_extra_mem_start = end; |
Jeremy Fitzhardinge | b5b43ce | 2010-09-02 17:10:12 -0700 | [diff] [blame] | 260 | |
Jeremy Fitzhardinge | c2d0879 | 2010-11-22 16:31:35 -0800 | [diff] [blame] | 261 | /* Add region if any remains */ |
| 262 | if (map[i].size > 0) |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 263 | e820_add_region(map[i].addr, map[i].size, map[i].type); |
| 264 | } |
Jeremy Fitzhardinge | b792c75 | 2008-06-16 14:54:49 -0700 | [diff] [blame] | 265 | |
| 266 | /* |
Ian Campbell | 9ec23a7f | 2010-10-28 11:32:29 -0700 | [diff] [blame] | 267 | * In domU, the ISA region is normal, usable memory, but we |
| 268 | * reserve ISA memory anyway because too many things poke |
Jeremy Fitzhardinge | b792c75 | 2008-06-16 14:54:49 -0700 | [diff] [blame] | 269 | * about in there. |
Juan Quintela | 4ec5387 | 2010-09-02 15:45:43 +0100 | [diff] [blame] | 270 | * |
Ian Campbell | 9ec23a7f | 2010-10-28 11:32:29 -0700 | [diff] [blame] | 271 | * In Dom0, the host E820 information can leave gaps in the |
| 272 | * ISA range, which would cause us to release those pages. To |
| 273 | * avoid this, we unconditionally reserve them here. |
Jeremy Fitzhardinge | b792c75 | 2008-06-16 14:54:49 -0700 | [diff] [blame] | 274 | */ |
| 275 | e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS, |
| 276 | E820_RESERVED); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 277 | |
Jeremy Fitzhardinge | be5bf9f | 2008-06-16 14:54:46 -0700 | [diff] [blame] | 278 | /* |
| 279 | * Reserve Xen bits: |
| 280 | * - mfn_list |
| 281 | * - xen_start_info |
| 282 | * See comment above "struct start_info" in <xen/interface/xen.h> |
| 283 | */ |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 284 | memblock_x86_reserve_range(__pa(xen_start_info->mfn_list), |
Jeremy Fitzhardinge | 6b2e852 | 2009-05-07 11:56:49 -0700 | [diff] [blame] | 285 | __pa(xen_start_info->pt_base), |
| 286 | "XEN START INFO"); |
Jeremy Fitzhardinge | be5bf9f | 2008-06-16 14:54:46 -0700 | [diff] [blame] | 287 | |
| 288 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); |
| 289 | |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 290 | extra_pages += xen_return_unused_memory(xen_start_info->nr_pages, &e820); |
| 291 | |
Jeremy Fitzhardinge | 698bb8d | 2010-09-14 10:19:14 -0700 | [diff] [blame] | 292 | /* |
| 293 | * Clamp the amount of extra memory to a EXTRA_MEM_RATIO |
| 294 | * factor the base size. On non-highmem systems, the base |
| 295 | * size is the full initial memory allocation; on highmem it |
| 296 | * is limited to the max size of lowmem, so that it doesn't |
| 297 | * get completely filled. |
| 298 | * |
| 299 | * In principle there could be a problem in lowmem systems if |
| 300 | * the initial memory is also very large with respect to |
| 301 | * lowmem, but we won't try to deal with that here. |
| 302 | */ |
| 303 | extra_limit = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)), |
| 304 | max_pfn + extra_pages); |
| 305 | |
| 306 | if (extra_limit >= max_pfn) |
| 307 | extra_pages = extra_limit - max_pfn; |
| 308 | else |
| 309 | extra_pages = 0; |
| 310 | |
Jeremy Fitzhardinge | d2a8171 | 2010-11-19 23:27:06 -0800 | [diff] [blame] | 311 | xen_add_extra_mem(extra_pages); |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 312 | |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 313 | /* |
| 314 | * Set P2M for all non-RAM pages and E820 gaps to be identity |
| 315 | * type PFNs. We supply it with the non-sanitized version |
| 316 | * of the E820. |
| 317 | */ |
| 318 | identity_pages = xen_set_identity(map_raw, memmap.nr_entries); |
| 319 | printk(KERN_INFO "Set %ld page(s) to 1-1 mapping.\n", identity_pages); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 320 | return "Xen"; |
| 321 | } |
| 322 | |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 323 | /* |
| 324 | * Set the bit indicating "nosegneg" library variants should be used. |
Jeremy Fitzhardinge | 6a52e4b | 2008-07-12 02:22:00 -0700 | [diff] [blame] | 325 | * We only need to bother in pure 32-bit mode; compat 32-bit processes |
| 326 | * can have un-truncated segments, so wrapping around is allowed. |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 327 | */ |
Sam Ravnborg | 08b6d29 | 2008-01-30 13:33:25 +0100 | [diff] [blame] | 328 | static void __init fiddle_vdso(void) |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 329 | { |
Jeremy Fitzhardinge | 6a52e4b | 2008-07-12 02:22:00 -0700 | [diff] [blame] | 330 | #ifdef CONFIG_X86_32 |
| 331 | u32 *mask; |
| 332 | mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK); |
| 333 | *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT; |
| 334 | mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK); |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 335 | *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT; |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 336 | #endif |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Daniel Kiper | ae15a3b | 2011-05-04 20:17:21 +0200 | [diff] [blame^] | 339 | static int __cpuinit register_callback(unsigned type, const void *func) |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 340 | { |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 341 | struct callback_register callback = { |
| 342 | .type = type, |
| 343 | .address = XEN_CALLBACK(__KERNEL_CS, func), |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 344 | .flags = CALLBACKF_mask_events, |
| 345 | }; |
| 346 | |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 347 | return HYPERVISOR_callback_op(CALLBACKOP_register, &callback); |
| 348 | } |
| 349 | |
| 350 | void __cpuinit xen_enable_sysenter(void) |
| 351 | { |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 352 | int ret; |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 353 | unsigned sysenter_feature; |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 354 | |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 355 | #ifdef CONFIG_X86_32 |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 356 | sysenter_feature = X86_FEATURE_SEP; |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 357 | #else |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 358 | sysenter_feature = X86_FEATURE_SYSENTER32; |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 359 | #endif |
| 360 | |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 361 | if (!boot_cpu_has(sysenter_feature)) |
| 362 | return; |
| 363 | |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 364 | ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target); |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 365 | if(ret != 0) |
| 366 | setup_clear_cpu_cap(sysenter_feature); |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 369 | void __cpuinit xen_enable_syscall(void) |
| 370 | { |
| 371 | #ifdef CONFIG_X86_64 |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 372 | int ret; |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 373 | |
| 374 | ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target); |
| 375 | if (ret != 0) { |
Jeremy Fitzhardinge | d5303b8 | 2008-07-12 02:22:06 -0700 | [diff] [blame] | 376 | printk(KERN_ERR "Failed to set syscall callback: %d\n", ret); |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 377 | /* Pretty fatal; 64-bit userspace has no other |
| 378 | mechanism for syscalls. */ |
| 379 | } |
| 380 | |
| 381 | if (boot_cpu_has(X86_FEATURE_SYSCALL32)) { |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 382 | ret = register_callback(CALLBACKTYPE_syscall32, |
| 383 | xen_syscall32_target); |
Jeremy Fitzhardinge | d5303b8 | 2008-07-12 02:22:06 -0700 | [diff] [blame] | 384 | if (ret != 0) |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 385 | setup_clear_cpu_cap(X86_FEATURE_SYSCALL32); |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 386 | } |
| 387 | #endif /* CONFIG_X86_64 */ |
| 388 | } |
| 389 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 390 | void __init xen_arch_setup(void) |
| 391 | { |
Donald Dutile | f09f6d1 | 2010-07-15 14:56:49 -0400 | [diff] [blame] | 392 | xen_panic_handler_init(); |
| 393 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 394 | HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments); |
| 395 | HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables); |
| 396 | |
| 397 | if (!xen_feature(XENFEAT_auto_translated_physmap)) |
Tej | f63c2f2 | 2008-12-16 11:56:06 -0800 | [diff] [blame] | 398 | HYPERVISOR_vm_assist(VMASST_CMD_enable, |
| 399 | VMASST_TYPE_pae_extended_cr3); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 400 | |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 401 | if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) || |
| 402 | register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback)) |
| 403 | BUG(); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 404 | |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 405 | xen_enable_sysenter(); |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 406 | xen_enable_syscall(); |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 407 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 408 | #ifdef CONFIG_ACPI |
| 409 | if (!(xen_start_info->flags & SIF_INITDOMAIN)) { |
| 410 | printk(KERN_INFO "ACPI in unprivileged domain disabled\n"); |
| 411 | disable_acpi(); |
| 412 | } |
| 413 | #endif |
| 414 | |
| 415 | memcpy(boot_command_line, xen_start_info->cmd_line, |
| 416 | MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ? |
| 417 | COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE); |
| 418 | |
Jeremy Fitzhardinge | bc15fde | 2010-11-22 17:17:50 -0800 | [diff] [blame] | 419 | /* Set up idle, making sure it calls safe_halt() pvop */ |
| 420 | #ifdef CONFIG_X86_32 |
| 421 | boot_cpu_data.hlt_works_ok = 1; |
| 422 | #endif |
| 423 | pm_idle = default_idle; |
Stefano Stabellini | 23febed | 2011-01-26 17:07:27 +0000 | [diff] [blame] | 424 | boot_option_idle_override = IDLE_HALT; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 425 | |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 426 | fiddle_vdso(); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 427 | } |