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