xen: check for kernel memory conflicting with memory layout
[cascardo/linux.git] / arch / x86 / xen / setup.c
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>
11 #include <linux/memblock.h>
12 #include <linux/cpuidle.h>
13 #include <linux/cpufreq.h>
14
15 #include <asm/elf.h>
16 #include <asm/vdso.h>
17 #include <asm/e820.h>
18 #include <asm/setup.h>
19 #include <asm/acpi.h>
20 #include <asm/numa.h>
21 #include <asm/xen/hypervisor.h>
22 #include <asm/xen/hypercall.h>
23
24 #include <xen/xen.h>
25 #include <xen/page.h>
26 #include <xen/interface/callback.h>
27 #include <xen/interface/memory.h>
28 #include <xen/interface/physdev.h>
29 #include <xen/features.h>
30 #include <xen/hvc-console.h>
31 #include "xen-ops.h"
32 #include "vdso.h"
33 #include "p2m.h"
34 #include "mmu.h"
35
36 /* Amount of extra memory space we add to the e820 ranges */
37 struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
38
39 /* Number of pages released from the initial allocation. */
40 unsigned long xen_released_pages;
41
42 /* E820 map used during setting up memory. */
43 static struct e820entry xen_e820_map[E820MAX] __initdata;
44 static u32 xen_e820_map_entries __initdata;
45
46 /*
47  * Buffer used to remap identity mapped pages. We only need the virtual space.
48  * The physical page behind this address is remapped as needed to different
49  * buffer pages.
50  */
51 #define REMAP_SIZE      (P2M_PER_PAGE - 3)
52 static struct {
53         unsigned long   next_area_mfn;
54         unsigned long   target_pfn;
55         unsigned long   size;
56         unsigned long   mfns[REMAP_SIZE];
57 } xen_remap_buf __initdata __aligned(PAGE_SIZE);
58 static unsigned long xen_remap_mfn __initdata = INVALID_P2M_ENTRY;
59
60 /* 
61  * The maximum amount of extra memory compared to the base size.  The
62  * main scaling factor is the size of struct page.  At extreme ratios
63  * of base:extra, all the base memory can be filled with page
64  * structures for the extra memory, leaving no space for anything
65  * else.
66  * 
67  * 10x seems like a reasonable balance between scaling flexibility and
68  * leaving a practically usable system.
69  */
70 #define EXTRA_MEM_RATIO         (10)
71
72 static void __init xen_add_extra_mem(phys_addr_t start, phys_addr_t size)
73 {
74         int i;
75
76         for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
77                 /* Add new region. */
78                 if (xen_extra_mem[i].size == 0) {
79                         xen_extra_mem[i].start = start;
80                         xen_extra_mem[i].size  = size;
81                         break;
82                 }
83                 /* Append to existing region. */
84                 if (xen_extra_mem[i].start + xen_extra_mem[i].size == start) {
85                         xen_extra_mem[i].size += size;
86                         break;
87                 }
88         }
89         if (i == XEN_EXTRA_MEM_MAX_REGIONS)
90                 printk(KERN_WARNING "Warning: not enough extra memory regions\n");
91
92         memblock_reserve(start, size);
93 }
94
95 static void __init xen_del_extra_mem(phys_addr_t start, phys_addr_t size)
96 {
97         int i;
98         phys_addr_t start_r, size_r;
99
100         for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
101                 start_r = xen_extra_mem[i].start;
102                 size_r = xen_extra_mem[i].size;
103
104                 /* Start of region. */
105                 if (start_r == start) {
106                         BUG_ON(size > size_r);
107                         xen_extra_mem[i].start += size;
108                         xen_extra_mem[i].size -= size;
109                         break;
110                 }
111                 /* End of region. */
112                 if (start_r + size_r == start + size) {
113                         BUG_ON(size > size_r);
114                         xen_extra_mem[i].size -= size;
115                         break;
116                 }
117                 /* Mid of region. */
118                 if (start > start_r && start < start_r + size_r) {
119                         BUG_ON(start + size > start_r + size_r);
120                         xen_extra_mem[i].size = start - start_r;
121                         /* Calling memblock_reserve() again is okay. */
122                         xen_add_extra_mem(start + size, start_r + size_r -
123                                           (start + size));
124                         break;
125                 }
126         }
127         memblock_free(start, size);
128 }
129
130 /*
131  * Called during boot before the p2m list can take entries beyond the
132  * hypervisor supplied p2m list. Entries in extra mem are to be regarded as
133  * invalid.
134  */
135 unsigned long __ref xen_chk_extra_mem(unsigned long pfn)
136 {
137         int i;
138         phys_addr_t addr = PFN_PHYS(pfn);
139
140         for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
141                 if (addr >= xen_extra_mem[i].start &&
142                     addr < xen_extra_mem[i].start + xen_extra_mem[i].size)
143                         return INVALID_P2M_ENTRY;
144         }
145
146         return IDENTITY_FRAME(pfn);
147 }
148
149 /*
150  * Mark all pfns of extra mem as invalid in p2m list.
151  */
152 void __init xen_inv_extra_mem(void)
153 {
154         unsigned long pfn, pfn_s, pfn_e;
155         int i;
156
157         for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
158                 if (!xen_extra_mem[i].size)
159                         continue;
160                 pfn_s = PFN_DOWN(xen_extra_mem[i].start);
161                 pfn_e = PFN_UP(xen_extra_mem[i].start + xen_extra_mem[i].size);
162                 for (pfn = pfn_s; pfn < pfn_e; pfn++)
163                         set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
164         }
165 }
166
167 /*
168  * Finds the next RAM pfn available in the E820 map after min_pfn.
169  * This function updates min_pfn with the pfn found and returns
170  * the size of that range or zero if not found.
171  */
172 static unsigned long __init xen_find_pfn_range(unsigned long *min_pfn)
173 {
174         const struct e820entry *entry = xen_e820_map;
175         unsigned int i;
176         unsigned long done = 0;
177
178         for (i = 0; i < xen_e820_map_entries; i++, entry++) {
179                 unsigned long s_pfn;
180                 unsigned long e_pfn;
181
182                 if (entry->type != E820_RAM)
183                         continue;
184
185                 e_pfn = PFN_DOWN(entry->addr + entry->size);
186
187                 /* We only care about E820 after this */
188                 if (e_pfn < *min_pfn)
189                         continue;
190
191                 s_pfn = PFN_UP(entry->addr);
192
193                 /* If min_pfn falls within the E820 entry, we want to start
194                  * at the min_pfn PFN.
195                  */
196                 if (s_pfn <= *min_pfn) {
197                         done = e_pfn - *min_pfn;
198                 } else {
199                         done = e_pfn - s_pfn;
200                         *min_pfn = s_pfn;
201                 }
202                 break;
203         }
204
205         return done;
206 }
207
208 static int __init xen_free_mfn(unsigned long mfn)
209 {
210         struct xen_memory_reservation reservation = {
211                 .address_bits = 0,
212                 .extent_order = 0,
213                 .domid        = DOMID_SELF
214         };
215
216         set_xen_guest_handle(reservation.extent_start, &mfn);
217         reservation.nr_extents = 1;
218
219         return HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
220 }
221
222 /*
223  * This releases a chunk of memory and then does the identity map. It's used
224  * as a fallback if the remapping fails.
225  */
226 static void __init xen_set_identity_and_release_chunk(unsigned long start_pfn,
227                         unsigned long end_pfn, unsigned long nr_pages)
228 {
229         unsigned long pfn, end;
230         int ret;
231
232         WARN_ON(start_pfn > end_pfn);
233
234         /* Release pages first. */
235         end = min(end_pfn, nr_pages);
236         for (pfn = start_pfn; pfn < end; pfn++) {
237                 unsigned long mfn = pfn_to_mfn(pfn);
238
239                 /* Make sure pfn exists to start with */
240                 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
241                         continue;
242
243                 ret = xen_free_mfn(mfn);
244                 WARN(ret != 1, "Failed to release pfn %lx err=%d\n", pfn, ret);
245
246                 if (ret == 1) {
247                         xen_released_pages++;
248                         if (!__set_phys_to_machine(pfn, INVALID_P2M_ENTRY))
249                                 break;
250                 } else
251                         break;
252         }
253
254         set_phys_range_identity(start_pfn, end_pfn);
255 }
256
257 /*
258  * Helper function to update the p2m and m2p tables and kernel mapping.
259  */
260 static void __init xen_update_mem_tables(unsigned long pfn, unsigned long mfn)
261 {
262         struct mmu_update update = {
263                 .ptr = ((uint64_t)mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE,
264                 .val = pfn
265         };
266
267         /* Update p2m */
268         if (!set_phys_to_machine(pfn, mfn)) {
269                 WARN(1, "Failed to set p2m mapping for pfn=%ld mfn=%ld\n",
270                      pfn, mfn);
271                 BUG();
272         }
273
274         /* Update m2p */
275         if (HYPERVISOR_mmu_update(&update, 1, NULL, DOMID_SELF) < 0) {
276                 WARN(1, "Failed to set m2p mapping for mfn=%ld pfn=%ld\n",
277                      mfn, pfn);
278                 BUG();
279         }
280
281         /* Update kernel mapping, but not for highmem. */
282         if (pfn >= PFN_UP(__pa(high_memory - 1)))
283                 return;
284
285         if (HYPERVISOR_update_va_mapping((unsigned long)__va(pfn << PAGE_SHIFT),
286                                          mfn_pte(mfn, PAGE_KERNEL), 0)) {
287                 WARN(1, "Failed to update kernel mapping for mfn=%ld pfn=%ld\n",
288                       mfn, pfn);
289                 BUG();
290         }
291 }
292
293 /*
294  * This function updates the p2m and m2p tables with an identity map from
295  * start_pfn to start_pfn+size and prepares remapping the underlying RAM of the
296  * original allocation at remap_pfn. The information needed for remapping is
297  * saved in the memory itself to avoid the need for allocating buffers. The
298  * complete remap information is contained in a list of MFNs each containing
299  * up to REMAP_SIZE MFNs and the start target PFN for doing the remap.
300  * This enables us to preserve the original mfn sequence while doing the
301  * remapping at a time when the memory management is capable of allocating
302  * virtual and physical memory in arbitrary amounts, see 'xen_remap_memory' and
303  * its callers.
304  */
305 static void __init xen_do_set_identity_and_remap_chunk(
306         unsigned long start_pfn, unsigned long size, unsigned long remap_pfn)
307 {
308         unsigned long buf = (unsigned long)&xen_remap_buf;
309         unsigned long mfn_save, mfn;
310         unsigned long ident_pfn_iter, remap_pfn_iter;
311         unsigned long ident_end_pfn = start_pfn + size;
312         unsigned long left = size;
313         unsigned int i, chunk;
314
315         WARN_ON(size == 0);
316
317         BUG_ON(xen_feature(XENFEAT_auto_translated_physmap));
318
319         mfn_save = virt_to_mfn(buf);
320
321         for (ident_pfn_iter = start_pfn, remap_pfn_iter = remap_pfn;
322              ident_pfn_iter < ident_end_pfn;
323              ident_pfn_iter += REMAP_SIZE, remap_pfn_iter += REMAP_SIZE) {
324                 chunk = (left < REMAP_SIZE) ? left : REMAP_SIZE;
325
326                 /* Map first pfn to xen_remap_buf */
327                 mfn = pfn_to_mfn(ident_pfn_iter);
328                 set_pte_mfn(buf, mfn, PAGE_KERNEL);
329
330                 /* Save mapping information in page */
331                 xen_remap_buf.next_area_mfn = xen_remap_mfn;
332                 xen_remap_buf.target_pfn = remap_pfn_iter;
333                 xen_remap_buf.size = chunk;
334                 for (i = 0; i < chunk; i++)
335                         xen_remap_buf.mfns[i] = pfn_to_mfn(ident_pfn_iter + i);
336
337                 /* Put remap buf into list. */
338                 xen_remap_mfn = mfn;
339
340                 /* Set identity map */
341                 set_phys_range_identity(ident_pfn_iter, ident_pfn_iter + chunk);
342
343                 left -= chunk;
344         }
345
346         /* Restore old xen_remap_buf mapping */
347         set_pte_mfn(buf, mfn_save, PAGE_KERNEL);
348 }
349
350 /*
351  * This function takes a contiguous pfn range that needs to be identity mapped
352  * and:
353  *
354  *  1) Finds a new range of pfns to use to remap based on E820 and remap_pfn.
355  *  2) Calls the do_ function to actually do the mapping/remapping work.
356  *
357  * The goal is to not allocate additional memory but to remap the existing
358  * pages. In the case of an error the underlying memory is simply released back
359  * to Xen and not remapped.
360  */
361 static unsigned long __init xen_set_identity_and_remap_chunk(
362         unsigned long start_pfn, unsigned long end_pfn, unsigned long nr_pages,
363         unsigned long remap_pfn)
364 {
365         unsigned long pfn;
366         unsigned long i = 0;
367         unsigned long n = end_pfn - start_pfn;
368
369         while (i < n) {
370                 unsigned long cur_pfn = start_pfn + i;
371                 unsigned long left = n - i;
372                 unsigned long size = left;
373                 unsigned long remap_range_size;
374
375                 /* Do not remap pages beyond the current allocation */
376                 if (cur_pfn >= nr_pages) {
377                         /* Identity map remaining pages */
378                         set_phys_range_identity(cur_pfn, cur_pfn + size);
379                         break;
380                 }
381                 if (cur_pfn + size > nr_pages)
382                         size = nr_pages - cur_pfn;
383
384                 remap_range_size = xen_find_pfn_range(&remap_pfn);
385                 if (!remap_range_size) {
386                         pr_warning("Unable to find available pfn range, not remapping identity pages\n");
387                         xen_set_identity_and_release_chunk(cur_pfn,
388                                                 cur_pfn + left, nr_pages);
389                         break;
390                 }
391                 /* Adjust size to fit in current e820 RAM region */
392                 if (size > remap_range_size)
393                         size = remap_range_size;
394
395                 xen_do_set_identity_and_remap_chunk(cur_pfn, size, remap_pfn);
396
397                 /* Update variables to reflect new mappings. */
398                 i += size;
399                 remap_pfn += size;
400         }
401
402         /*
403          * If the PFNs are currently mapped, the VA mapping also needs
404          * to be updated to be 1:1.
405          */
406         for (pfn = start_pfn; pfn <= max_pfn_mapped && pfn < end_pfn; pfn++)
407                 (void)HYPERVISOR_update_va_mapping(
408                         (unsigned long)__va(pfn << PAGE_SHIFT),
409                         mfn_pte(pfn, PAGE_KERNEL_IO), 0);
410
411         return remap_pfn;
412 }
413
414 static void __init xen_set_identity_and_remap(unsigned long nr_pages)
415 {
416         phys_addr_t start = 0;
417         unsigned long last_pfn = nr_pages;
418         const struct e820entry *entry = xen_e820_map;
419         int i;
420
421         /*
422          * Combine non-RAM regions and gaps until a RAM region (or the
423          * end of the map) is reached, then set the 1:1 map and
424          * remap the memory in those non-RAM regions.
425          *
426          * The combined non-RAM regions are rounded to a whole number
427          * of pages so any partial pages are accessible via the 1:1
428          * mapping.  This is needed for some BIOSes that put (for
429          * example) the DMI tables in a reserved region that begins on
430          * a non-page boundary.
431          */
432         for (i = 0; i < xen_e820_map_entries; i++, entry++) {
433                 phys_addr_t end = entry->addr + entry->size;
434                 if (entry->type == E820_RAM || i == xen_e820_map_entries - 1) {
435                         unsigned long start_pfn = PFN_DOWN(start);
436                         unsigned long end_pfn = PFN_UP(end);
437
438                         if (entry->type == E820_RAM)
439                                 end_pfn = PFN_UP(entry->addr);
440
441                         if (start_pfn < end_pfn)
442                                 last_pfn = xen_set_identity_and_remap_chunk(
443                                                 start_pfn, end_pfn, nr_pages,
444                                                 last_pfn);
445                         start = end;
446                 }
447         }
448
449         pr_info("Released %ld page(s)\n", xen_released_pages);
450 }
451
452 /*
453  * Remap the memory prepared in xen_do_set_identity_and_remap_chunk().
454  * The remap information (which mfn remap to which pfn) is contained in the
455  * to be remapped memory itself in a linked list anchored at xen_remap_mfn.
456  * This scheme allows to remap the different chunks in arbitrary order while
457  * the resulting mapping will be independant from the order.
458  */
459 void __init xen_remap_memory(void)
460 {
461         unsigned long buf = (unsigned long)&xen_remap_buf;
462         unsigned long mfn_save, mfn, pfn;
463         unsigned long remapped = 0;
464         unsigned int i;
465         unsigned long pfn_s = ~0UL;
466         unsigned long len = 0;
467
468         mfn_save = virt_to_mfn(buf);
469
470         while (xen_remap_mfn != INVALID_P2M_ENTRY) {
471                 /* Map the remap information */
472                 set_pte_mfn(buf, xen_remap_mfn, PAGE_KERNEL);
473
474                 BUG_ON(xen_remap_mfn != xen_remap_buf.mfns[0]);
475
476                 pfn = xen_remap_buf.target_pfn;
477                 for (i = 0; i < xen_remap_buf.size; i++) {
478                         mfn = xen_remap_buf.mfns[i];
479                         xen_update_mem_tables(pfn, mfn);
480                         remapped++;
481                         pfn++;
482                 }
483                 if (pfn_s == ~0UL || pfn == pfn_s) {
484                         pfn_s = xen_remap_buf.target_pfn;
485                         len += xen_remap_buf.size;
486                 } else if (pfn_s + len == xen_remap_buf.target_pfn) {
487                         len += xen_remap_buf.size;
488                 } else {
489                         xen_del_extra_mem(PFN_PHYS(pfn_s), PFN_PHYS(len));
490                         pfn_s = xen_remap_buf.target_pfn;
491                         len = xen_remap_buf.size;
492                 }
493
494                 mfn = xen_remap_mfn;
495                 xen_remap_mfn = xen_remap_buf.next_area_mfn;
496         }
497
498         if (pfn_s != ~0UL && len)
499                 xen_del_extra_mem(PFN_PHYS(pfn_s), PFN_PHYS(len));
500
501         set_pte_mfn(buf, mfn_save, PAGE_KERNEL);
502
503         pr_info("Remapped %ld page(s)\n", remapped);
504 }
505
506 static unsigned long __init xen_get_max_pages(void)
507 {
508         unsigned long max_pages = MAX_DOMAIN_PAGES;
509         domid_t domid = DOMID_SELF;
510         int ret;
511
512         /*
513          * For the initial domain we use the maximum reservation as
514          * the maximum page.
515          *
516          * For guest domains the current maximum reservation reflects
517          * the current maximum rather than the static maximum. In this
518          * case the e820 map provided to us will cover the static
519          * maximum region.
520          */
521         if (xen_initial_domain()) {
522                 ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
523                 if (ret > 0)
524                         max_pages = ret;
525         }
526
527         return min(max_pages, MAX_DOMAIN_PAGES);
528 }
529
530 static void __init xen_align_and_add_e820_region(phys_addr_t start,
531                                                  phys_addr_t size, int type)
532 {
533         phys_addr_t end = start + size;
534
535         /* Align RAM regions to page boundaries. */
536         if (type == E820_RAM) {
537                 start = PAGE_ALIGN(start);
538                 end &= ~((phys_addr_t)PAGE_SIZE - 1);
539         }
540
541         e820_add_region(start, end - start, type);
542 }
543
544 static void __init xen_ignore_unusable(void)
545 {
546         struct e820entry *entry = xen_e820_map;
547         unsigned int i;
548
549         for (i = 0; i < xen_e820_map_entries; i++, entry++) {
550                 if (entry->type == E820_UNUSABLE)
551                         entry->type = E820_RAM;
552         }
553 }
554
555 static unsigned long __init xen_count_remap_pages(unsigned long max_pfn)
556 {
557         unsigned long extra = 0;
558         const struct e820entry *entry = xen_e820_map;
559         int i;
560
561         for (i = 0; i < xen_e820_map_entries; i++, entry++) {
562                 unsigned long start_pfn = PFN_DOWN(entry->addr);
563                 unsigned long end_pfn = PFN_UP(entry->addr + entry->size);
564
565                 if (start_pfn >= max_pfn)
566                         break;
567                 if (entry->type == E820_RAM)
568                         continue;
569                 if (end_pfn >= max_pfn)
570                         end_pfn = max_pfn;
571                 extra += end_pfn - start_pfn;
572         }
573
574         return extra;
575 }
576
577 bool __init xen_is_e820_reserved(phys_addr_t start, phys_addr_t size)
578 {
579         struct e820entry *entry;
580         unsigned mapcnt;
581         phys_addr_t end;
582
583         if (!size)
584                 return false;
585
586         end = start + size;
587         entry = xen_e820_map;
588
589         for (mapcnt = 0; mapcnt < xen_e820_map_entries; mapcnt++) {
590                 if (entry->type == E820_RAM && entry->addr <= start &&
591                     (entry->addr + entry->size) >= end)
592                         return false;
593
594                 entry++;
595         }
596
597         return true;
598 }
599
600 /*
601  * Find a free area in physical memory not yet reserved and compliant with
602  * E820 map.
603  * Used to relocate pre-allocated areas like initrd or p2m list which are in
604  * conflict with the to be used E820 map.
605  * In case no area is found, return 0. Otherwise return the physical address
606  * of the area which is already reserved for convenience.
607  */
608 phys_addr_t __init xen_find_free_area(phys_addr_t size)
609 {
610         unsigned mapcnt;
611         phys_addr_t addr, start;
612         struct e820entry *entry = xen_e820_map;
613
614         for (mapcnt = 0; mapcnt < xen_e820_map_entries; mapcnt++, entry++) {
615                 if (entry->type != E820_RAM || entry->size < size)
616                         continue;
617                 start = entry->addr;
618                 for (addr = start; addr < start + size; addr += PAGE_SIZE) {
619                         if (!memblock_is_reserved(addr))
620                                 continue;
621                         start = addr + PAGE_SIZE;
622                         if (start + size > entry->addr + entry->size)
623                                 break;
624                 }
625                 if (addr >= start + size) {
626                         memblock_reserve(start, size);
627                         return start;
628                 }
629         }
630
631         return 0;
632 }
633
634 /*
635  * Reserve Xen mfn_list.
636  * See comment above "struct start_info" in <xen/interface/xen.h>
637  * We tried to make the the memblock_reserve more selective so
638  * that it would be clear what region is reserved. Sadly we ran
639  * in the problem wherein on a 64-bit hypervisor with a 32-bit
640  * initial domain, the pt_base has the cr3 value which is not
641  * neccessarily where the pagetable starts! As Jan put it: "
642  * Actually, the adjustment turns out to be correct: The page
643  * tables for a 32-on-64 dom0 get allocated in the order "first L1",
644  * "first L2", "first L3", so the offset to the page table base is
645  * indeed 2. When reading xen/include/public/xen.h's comment
646  * very strictly, this is not a violation (since there nothing is said
647  * that the first thing in the page table space is pointed to by
648  * pt_base; I admit that this seems to be implied though, namely
649  * do I think that it is implied that the page table space is the
650  * range [pt_base, pt_base + nt_pt_frames), whereas that
651  * range here indeed is [pt_base - 2, pt_base - 2 + nt_pt_frames),
652  * which - without a priori knowledge - the kernel would have
653  * difficulty to figure out)." - so lets just fall back to the
654  * easy way and reserve the whole region.
655  */
656 static void __init xen_reserve_xen_mfnlist(void)
657 {
658         if (xen_start_info->mfn_list >= __START_KERNEL_map) {
659                 memblock_reserve(__pa(xen_start_info->mfn_list),
660                                  xen_start_info->pt_base -
661                                  xen_start_info->mfn_list);
662                 return;
663         }
664
665         memblock_reserve(PFN_PHYS(xen_start_info->first_p2m_pfn),
666                          PFN_PHYS(xen_start_info->nr_p2m_frames));
667 }
668
669 /**
670  * machine_specific_memory_setup - Hook for machine specific memory setup.
671  **/
672 char * __init xen_memory_setup(void)
673 {
674         unsigned long max_pfn = xen_start_info->nr_pages;
675         phys_addr_t mem_end, addr, size, chunk_size;
676         u32 type;
677         int rc;
678         struct xen_memory_map memmap;
679         unsigned long max_pages;
680         unsigned long extra_pages = 0;
681         int i;
682         int op;
683
684         max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
685         mem_end = PFN_PHYS(max_pfn);
686
687         memmap.nr_entries = E820MAX;
688         set_xen_guest_handle(memmap.buffer, xen_e820_map);
689
690         op = xen_initial_domain() ?
691                 XENMEM_machine_memory_map :
692                 XENMEM_memory_map;
693         rc = HYPERVISOR_memory_op(op, &memmap);
694         if (rc == -ENOSYS) {
695                 BUG_ON(xen_initial_domain());
696                 memmap.nr_entries = 1;
697                 xen_e820_map[0].addr = 0ULL;
698                 xen_e820_map[0].size = mem_end;
699                 /* 8MB slack (to balance backend allocations). */
700                 xen_e820_map[0].size += 8ULL << 20;
701                 xen_e820_map[0].type = E820_RAM;
702                 rc = 0;
703         }
704         BUG_ON(rc);
705         BUG_ON(memmap.nr_entries == 0);
706         xen_e820_map_entries = memmap.nr_entries;
707
708         /*
709          * Xen won't allow a 1:1 mapping to be created to UNUSABLE
710          * regions, so if we're using the machine memory map leave the
711          * region as RAM as it is in the pseudo-physical map.
712          *
713          * UNUSABLE regions in domUs are not handled and will need
714          * a patch in the future.
715          */
716         if (xen_initial_domain())
717                 xen_ignore_unusable();
718
719         /* Make sure the Xen-supplied memory map is well-ordered. */
720         sanitize_e820_map(xen_e820_map, xen_e820_map_entries,
721                           &xen_e820_map_entries);
722
723         max_pages = xen_get_max_pages();
724         if (max_pages > max_pfn)
725                 extra_pages += max_pages - max_pfn;
726
727         /* How many extra pages do we need due to remapping? */
728         extra_pages += xen_count_remap_pages(max_pfn);
729
730         /*
731          * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
732          * factor the base size.  On non-highmem systems, the base
733          * size is the full initial memory allocation; on highmem it
734          * is limited to the max size of lowmem, so that it doesn't
735          * get completely filled.
736          *
737          * In principle there could be a problem in lowmem systems if
738          * the initial memory is also very large with respect to
739          * lowmem, but we won't try to deal with that here.
740          */
741         extra_pages = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
742                           extra_pages);
743         i = 0;
744         addr = xen_e820_map[0].addr;
745         size = xen_e820_map[0].size;
746         while (i < xen_e820_map_entries) {
747                 chunk_size = size;
748                 type = xen_e820_map[i].type;
749
750                 if (type == E820_RAM) {
751                         if (addr < mem_end) {
752                                 chunk_size = min(size, mem_end - addr);
753                         } else if (extra_pages) {
754                                 chunk_size = min(size, PFN_PHYS(extra_pages));
755                                 extra_pages -= PFN_DOWN(chunk_size);
756                                 xen_add_extra_mem(addr, chunk_size);
757                                 xen_max_p2m_pfn = PFN_DOWN(addr + chunk_size);
758                         } else
759                                 type = E820_UNUSABLE;
760                 }
761
762                 xen_align_and_add_e820_region(addr, chunk_size, type);
763
764                 addr += chunk_size;
765                 size -= chunk_size;
766                 if (size == 0) {
767                         i++;
768                         if (i < xen_e820_map_entries) {
769                                 addr = xen_e820_map[i].addr;
770                                 size = xen_e820_map[i].size;
771                         }
772                 }
773         }
774
775         /*
776          * Set the rest as identity mapped, in case PCI BARs are
777          * located here.
778          *
779          * PFNs above MAX_P2M_PFN are considered identity mapped as
780          * well.
781          */
782         set_phys_range_identity(addr / PAGE_SIZE, ~0ul);
783
784         /*
785          * In domU, the ISA region is normal, usable memory, but we
786          * reserve ISA memory anyway because too many things poke
787          * about in there.
788          */
789         e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
790                         E820_RESERVED);
791
792         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
793
794         /*
795          * Check whether the kernel itself conflicts with the target E820 map.
796          * Failing now is better than running into weird problems later due
797          * to relocating (and even reusing) pages with kernel text or data.
798          */
799         if (xen_is_e820_reserved(__pa_symbol(_text),
800                         __pa_symbol(__bss_stop) - __pa_symbol(_text))) {
801                 xen_raw_console_write("Xen hypervisor allocated kernel memory conflicts with E820 map\n");
802                 BUG();
803         }
804
805         xen_reserve_xen_mfnlist();
806
807         /*
808          * Set identity map on non-RAM pages and prepare remapping the
809          * underlying RAM.
810          */
811         xen_set_identity_and_remap(max_pfn);
812
813         return "Xen";
814 }
815
816 /*
817  * Machine specific memory setup for auto-translated guests.
818  */
819 char * __init xen_auto_xlated_memory_setup(void)
820 {
821         struct xen_memory_map memmap;
822         int i;
823         int rc;
824
825         memmap.nr_entries = E820MAX;
826         set_xen_guest_handle(memmap.buffer, xen_e820_map);
827
828         rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
829         if (rc < 0)
830                 panic("No memory map (%d)\n", rc);
831
832         xen_e820_map_entries = memmap.nr_entries;
833
834         sanitize_e820_map(xen_e820_map, ARRAY_SIZE(xen_e820_map),
835                           &xen_e820_map_entries);
836
837         for (i = 0; i < xen_e820_map_entries; i++)
838                 e820_add_region(xen_e820_map[i].addr, xen_e820_map[i].size,
839                                 xen_e820_map[i].type);
840
841         xen_reserve_xen_mfnlist();
842
843         return "Xen";
844 }
845
846 /*
847  * Set the bit indicating "nosegneg" library variants should be used.
848  * We only need to bother in pure 32-bit mode; compat 32-bit processes
849  * can have un-truncated segments, so wrapping around is allowed.
850  */
851 static void __init fiddle_vdso(void)
852 {
853 #ifdef CONFIG_X86_32
854         /*
855          * This could be called before selected_vdso32 is initialized, so
856          * just fiddle with both possible images.  vdso_image_32_syscall
857          * can't be selected, since it only exists on 64-bit systems.
858          */
859         u32 *mask;
860         mask = vdso_image_32_int80.data +
861                 vdso_image_32_int80.sym_VDSO32_NOTE_MASK;
862         *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
863         mask = vdso_image_32_sysenter.data +
864                 vdso_image_32_sysenter.sym_VDSO32_NOTE_MASK;
865         *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
866 #endif
867 }
868
869 static int register_callback(unsigned type, const void *func)
870 {
871         struct callback_register callback = {
872                 .type = type,
873                 .address = XEN_CALLBACK(__KERNEL_CS, func),
874                 .flags = CALLBACKF_mask_events,
875         };
876
877         return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
878 }
879
880 void xen_enable_sysenter(void)
881 {
882         int ret;
883         unsigned sysenter_feature;
884
885 #ifdef CONFIG_X86_32
886         sysenter_feature = X86_FEATURE_SEP;
887 #else
888         sysenter_feature = X86_FEATURE_SYSENTER32;
889 #endif
890
891         if (!boot_cpu_has(sysenter_feature))
892                 return;
893
894         ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
895         if(ret != 0)
896                 setup_clear_cpu_cap(sysenter_feature);
897 }
898
899 void xen_enable_syscall(void)
900 {
901 #ifdef CONFIG_X86_64
902         int ret;
903
904         ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
905         if (ret != 0) {
906                 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
907                 /* Pretty fatal; 64-bit userspace has no other
908                    mechanism for syscalls. */
909         }
910
911         if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
912                 ret = register_callback(CALLBACKTYPE_syscall32,
913                                         xen_syscall32_target);
914                 if (ret != 0)
915                         setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
916         }
917 #endif /* CONFIG_X86_64 */
918 }
919
920 void __init xen_pvmmu_arch_setup(void)
921 {
922         HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
923         HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
924
925         HYPERVISOR_vm_assist(VMASST_CMD_enable,
926                              VMASST_TYPE_pae_extended_cr3);
927
928         if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
929             register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
930                 BUG();
931
932         xen_enable_sysenter();
933         xen_enable_syscall();
934 }
935
936 /* This function is not called for HVM domains */
937 void __init xen_arch_setup(void)
938 {
939         xen_panic_handler_init();
940         if (!xen_feature(XENFEAT_auto_translated_physmap))
941                 xen_pvmmu_arch_setup();
942
943 #ifdef CONFIG_ACPI
944         if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
945                 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
946                 disable_acpi();
947         }
948 #endif
949
950         memcpy(boot_command_line, xen_start_info->cmd_line,
951                MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
952                COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
953
954         /* Set up idle, making sure it calls safe_halt() pvop */
955         disable_cpuidle();
956         disable_cpufreq();
957         WARN_ON(xen_set_default_idle());
958         fiddle_vdso();
959 #ifdef CONFIG_NUMA
960         numa_off = 1;
961 #endif
962 }