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