410210f279bf02eac1344d197f473389f934ca7c
[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 /* 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         phys_addr_t 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 >= PFN_UP(__pa(high_memory - 1)))
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, unsigned long *remapped)
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                 *remapped += size;
408         }
409
410         /*
411          * If the PFNs are currently mapped, the VA mapping also needs
412          * to be updated to be 1:1.
413          */
414         for (pfn = start_pfn; pfn <= max_pfn_mapped && pfn < end_pfn; pfn++)
415                 (void)HYPERVISOR_update_va_mapping(
416                         (unsigned long)__va(pfn << PAGE_SHIFT),
417                         mfn_pte(pfn, PAGE_KERNEL_IO), 0);
418
419         return remap_pfn;
420 }
421
422 static void __init xen_set_identity_and_remap(
423         const struct e820entry *list, size_t map_size, unsigned long nr_pages,
424         unsigned long *released, unsigned long *remapped)
425 {
426         phys_addr_t start = 0;
427         unsigned long last_pfn = nr_pages;
428         const struct e820entry *entry;
429         unsigned long num_released = 0;
430         unsigned long num_remapped = 0;
431         int i;
432
433         /*
434          * Combine non-RAM regions and gaps until a RAM region (or the
435          * end of the map) is reached, then set the 1:1 map and
436          * remap the memory in those non-RAM regions.
437          *
438          * The combined non-RAM regions are rounded to a whole number
439          * of pages so any partial pages are accessible via the 1:1
440          * mapping.  This is needed for some BIOSes that put (for
441          * example) the DMI tables in a reserved region that begins on
442          * a non-page boundary.
443          */
444         for (i = 0, entry = list; i < map_size; i++, entry++) {
445                 phys_addr_t end = entry->addr + entry->size;
446                 if (entry->type == E820_RAM || i == map_size - 1) {
447                         unsigned long start_pfn = PFN_DOWN(start);
448                         unsigned long end_pfn = PFN_UP(end);
449
450                         if (entry->type == E820_RAM)
451                                 end_pfn = PFN_UP(entry->addr);
452
453                         if (start_pfn < end_pfn)
454                                 last_pfn = xen_set_identity_and_remap_chunk(
455                                                 list, map_size, start_pfn,
456                                                 end_pfn, nr_pages, last_pfn,
457                                                 &num_released, &num_remapped);
458                         start = end;
459                 }
460         }
461
462         *released = num_released;
463         *remapped = num_remapped;
464
465         pr_info("Released %ld page(s)\n", num_released);
466 }
467
468 /*
469  * Remap the memory prepared in xen_do_set_identity_and_remap_chunk().
470  * The remap information (which mfn remap to which pfn) is contained in the
471  * to be remapped memory itself in a linked list anchored at xen_remap_mfn.
472  * This scheme allows to remap the different chunks in arbitrary order while
473  * the resulting mapping will be independant from the order.
474  */
475 void __init xen_remap_memory(void)
476 {
477         unsigned long buf = (unsigned long)&xen_remap_buf;
478         unsigned long mfn_save, mfn, pfn;
479         unsigned long remapped = 0;
480         unsigned int i;
481         unsigned long pfn_s = ~0UL;
482         unsigned long len = 0;
483
484         mfn_save = virt_to_mfn(buf);
485
486         while (xen_remap_mfn != INVALID_P2M_ENTRY) {
487                 /* Map the remap information */
488                 set_pte_mfn(buf, xen_remap_mfn, PAGE_KERNEL);
489
490                 BUG_ON(xen_remap_mfn != xen_remap_buf.mfns[0]);
491
492                 pfn = xen_remap_buf.target_pfn;
493                 for (i = 0; i < xen_remap_buf.size; i++) {
494                         mfn = xen_remap_buf.mfns[i];
495                         xen_update_mem_tables(pfn, mfn);
496                         remapped++;
497                         pfn++;
498                 }
499                 if (pfn_s == ~0UL || pfn == pfn_s) {
500                         pfn_s = xen_remap_buf.target_pfn;
501                         len += xen_remap_buf.size;
502                 } else if (pfn_s + len == xen_remap_buf.target_pfn) {
503                         len += xen_remap_buf.size;
504                 } else {
505                         xen_del_extra_mem(PFN_PHYS(pfn_s), PFN_PHYS(len));
506                         pfn_s = xen_remap_buf.target_pfn;
507                         len = xen_remap_buf.size;
508                 }
509
510                 mfn = xen_remap_mfn;
511                 xen_remap_mfn = xen_remap_buf.next_area_mfn;
512         }
513
514         if (pfn_s != ~0UL && len)
515                 xen_del_extra_mem(PFN_PHYS(pfn_s), PFN_PHYS(len));
516
517         set_pte_mfn(buf, mfn_save, PAGE_KERNEL);
518
519         pr_info("Remapped %ld page(s)\n", remapped);
520 }
521
522 static unsigned long __init xen_get_max_pages(void)
523 {
524         unsigned long max_pages = MAX_DOMAIN_PAGES;
525         domid_t domid = DOMID_SELF;
526         int ret;
527
528         /*
529          * For the initial domain we use the maximum reservation as
530          * the maximum page.
531          *
532          * For guest domains the current maximum reservation reflects
533          * the current maximum rather than the static maximum. In this
534          * case the e820 map provided to us will cover the static
535          * maximum region.
536          */
537         if (xen_initial_domain()) {
538                 ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
539                 if (ret > 0)
540                         max_pages = ret;
541         }
542
543         return min(max_pages, MAX_DOMAIN_PAGES);
544 }
545
546 static void xen_align_and_add_e820_region(u64 start, u64 size, int type)
547 {
548         u64 end = start + size;
549
550         /* Align RAM regions to page boundaries. */
551         if (type == E820_RAM) {
552                 start = PAGE_ALIGN(start);
553                 end &= ~((u64)PAGE_SIZE - 1);
554         }
555
556         e820_add_region(start, end - start, type);
557 }
558
559 void xen_ignore_unusable(struct e820entry *list, size_t map_size)
560 {
561         struct e820entry *entry;
562         unsigned int i;
563
564         for (i = 0, entry = list; i < map_size; i++, entry++) {
565                 if (entry->type == E820_UNUSABLE)
566                         entry->type = E820_RAM;
567         }
568 }
569
570 /**
571  * machine_specific_memory_setup - Hook for machine specific memory setup.
572  **/
573 char * __init xen_memory_setup(void)
574 {
575         static struct e820entry map[E820MAX] __initdata;
576
577         unsigned long max_pfn = xen_start_info->nr_pages;
578         unsigned long long mem_end;
579         int rc;
580         struct xen_memory_map memmap;
581         unsigned long max_pages;
582         unsigned long extra_pages = 0;
583         unsigned long remapped_pages;
584         int i;
585         int op;
586
587         max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
588         mem_end = PFN_PHYS(max_pfn);
589
590         memmap.nr_entries = E820MAX;
591         set_xen_guest_handle(memmap.buffer, map);
592
593         op = xen_initial_domain() ?
594                 XENMEM_machine_memory_map :
595                 XENMEM_memory_map;
596         rc = HYPERVISOR_memory_op(op, &memmap);
597         if (rc == -ENOSYS) {
598                 BUG_ON(xen_initial_domain());
599                 memmap.nr_entries = 1;
600                 map[0].addr = 0ULL;
601                 map[0].size = mem_end;
602                 /* 8MB slack (to balance backend allocations). */
603                 map[0].size += 8ULL << 20;
604                 map[0].type = E820_RAM;
605                 rc = 0;
606         }
607         BUG_ON(rc);
608         BUG_ON(memmap.nr_entries == 0);
609
610         /*
611          * Xen won't allow a 1:1 mapping to be created to UNUSABLE
612          * regions, so if we're using the machine memory map leave the
613          * region as RAM as it is in the pseudo-physical map.
614          *
615          * UNUSABLE regions in domUs are not handled and will need
616          * a patch in the future.
617          */
618         if (xen_initial_domain())
619                 xen_ignore_unusable(map, memmap.nr_entries);
620
621         /* Make sure the Xen-supplied memory map is well-ordered. */
622         sanitize_e820_map(map, memmap.nr_entries, &memmap.nr_entries);
623
624         max_pages = xen_get_max_pages();
625         if (max_pages > max_pfn)
626                 extra_pages += max_pages - max_pfn;
627
628         /*
629          * Set identity map on non-RAM pages and prepare remapping the
630          * underlying RAM.
631          */
632         xen_set_identity_and_remap(map, memmap.nr_entries, max_pfn,
633                                    &xen_released_pages, &remapped_pages);
634
635         extra_pages += xen_released_pages;
636         extra_pages += remapped_pages;
637
638         /*
639          * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
640          * factor the base size.  On non-highmem systems, the base
641          * size is the full initial memory allocation; on highmem it
642          * is limited to the max size of lowmem, so that it doesn't
643          * get completely filled.
644          *
645          * In principle there could be a problem in lowmem systems if
646          * the initial memory is also very large with respect to
647          * lowmem, but we won't try to deal with that here.
648          */
649         extra_pages = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
650                           extra_pages);
651         i = 0;
652         while (i < memmap.nr_entries) {
653                 u64 addr = map[i].addr;
654                 u64 size = map[i].size;
655                 u32 type = map[i].type;
656
657                 if (type == E820_RAM) {
658                         if (addr < mem_end) {
659                                 size = min(size, mem_end - addr);
660                         } else if (extra_pages) {
661                                 size = min(size, (u64)extra_pages * PAGE_SIZE);
662                                 extra_pages -= size / PAGE_SIZE;
663                                 xen_add_extra_mem(addr, size);
664                                 xen_max_p2m_pfn = PFN_DOWN(addr + size);
665                         } else
666                                 type = E820_UNUSABLE;
667                 }
668
669                 xen_align_and_add_e820_region(addr, size, type);
670
671                 map[i].addr += size;
672                 map[i].size -= size;
673                 if (map[i].size == 0)
674                         i++;
675         }
676
677         /*
678          * Set the rest as identity mapped, in case PCI BARs are
679          * located here.
680          *
681          * PFNs above MAX_P2M_PFN are considered identity mapped as
682          * well.
683          */
684         set_phys_range_identity(map[i-1].addr / PAGE_SIZE, ~0ul);
685
686         /*
687          * In domU, the ISA region is normal, usable memory, but we
688          * reserve ISA memory anyway because too many things poke
689          * about in there.
690          */
691         e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
692                         E820_RESERVED);
693
694         /*
695          * Reserve Xen bits:
696          *  - mfn_list
697          *  - xen_start_info
698          * See comment above "struct start_info" in <xen/interface/xen.h>
699          * We tried to make the the memblock_reserve more selective so
700          * that it would be clear what region is reserved. Sadly we ran
701          * in the problem wherein on a 64-bit hypervisor with a 32-bit
702          * initial domain, the pt_base has the cr3 value which is not
703          * neccessarily where the pagetable starts! As Jan put it: "
704          * Actually, the adjustment turns out to be correct: The page
705          * tables for a 32-on-64 dom0 get allocated in the order "first L1",
706          * "first L2", "first L3", so the offset to the page table base is
707          * indeed 2. When reading xen/include/public/xen.h's comment
708          * very strictly, this is not a violation (since there nothing is said
709          * that the first thing in the page table space is pointed to by
710          * pt_base; I admit that this seems to be implied though, namely
711          * do I think that it is implied that the page table space is the
712          * range [pt_base, pt_base + nt_pt_frames), whereas that
713          * range here indeed is [pt_base - 2, pt_base - 2 + nt_pt_frames),
714          * which - without a priori knowledge - the kernel would have
715          * difficulty to figure out)." - so lets just fall back to the
716          * easy way and reserve the whole region.
717          */
718         memblock_reserve(__pa(xen_start_info->mfn_list),
719                          xen_start_info->pt_base - xen_start_info->mfn_list);
720
721         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
722
723         return "Xen";
724 }
725
726 /*
727  * Machine specific memory setup for auto-translated guests.
728  */
729 char * __init xen_auto_xlated_memory_setup(void)
730 {
731         static struct e820entry map[E820MAX] __initdata;
732
733         struct xen_memory_map memmap;
734         int i;
735         int rc;
736
737         memmap.nr_entries = E820MAX;
738         set_xen_guest_handle(memmap.buffer, map);
739
740         rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
741         if (rc < 0)
742                 panic("No memory map (%d)\n", rc);
743
744         sanitize_e820_map(map, ARRAY_SIZE(map), &memmap.nr_entries);
745
746         for (i = 0; i < memmap.nr_entries; i++)
747                 e820_add_region(map[i].addr, map[i].size, map[i].type);
748
749         memblock_reserve(__pa(xen_start_info->mfn_list),
750                          xen_start_info->pt_base - xen_start_info->mfn_list);
751
752         return "Xen";
753 }
754
755 /*
756  * Set the bit indicating "nosegneg" library variants should be used.
757  * We only need to bother in pure 32-bit mode; compat 32-bit processes
758  * can have un-truncated segments, so wrapping around is allowed.
759  */
760 static void __init fiddle_vdso(void)
761 {
762 #ifdef CONFIG_X86_32
763         /*
764          * This could be called before selected_vdso32 is initialized, so
765          * just fiddle with both possible images.  vdso_image_32_syscall
766          * can't be selected, since it only exists on 64-bit systems.
767          */
768         u32 *mask;
769         mask = vdso_image_32_int80.data +
770                 vdso_image_32_int80.sym_VDSO32_NOTE_MASK;
771         *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
772         mask = vdso_image_32_sysenter.data +
773                 vdso_image_32_sysenter.sym_VDSO32_NOTE_MASK;
774         *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
775 #endif
776 }
777
778 static int register_callback(unsigned type, const void *func)
779 {
780         struct callback_register callback = {
781                 .type = type,
782                 .address = XEN_CALLBACK(__KERNEL_CS, func),
783                 .flags = CALLBACKF_mask_events,
784         };
785
786         return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
787 }
788
789 void xen_enable_sysenter(void)
790 {
791         int ret;
792         unsigned sysenter_feature;
793
794 #ifdef CONFIG_X86_32
795         sysenter_feature = X86_FEATURE_SEP;
796 #else
797         sysenter_feature = X86_FEATURE_SYSENTER32;
798 #endif
799
800         if (!boot_cpu_has(sysenter_feature))
801                 return;
802
803         ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
804         if(ret != 0)
805                 setup_clear_cpu_cap(sysenter_feature);
806 }
807
808 void xen_enable_syscall(void)
809 {
810 #ifdef CONFIG_X86_64
811         int ret;
812
813         ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
814         if (ret != 0) {
815                 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
816                 /* Pretty fatal; 64-bit userspace has no other
817                    mechanism for syscalls. */
818         }
819
820         if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
821                 ret = register_callback(CALLBACKTYPE_syscall32,
822                                         xen_syscall32_target);
823                 if (ret != 0)
824                         setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
825         }
826 #endif /* CONFIG_X86_64 */
827 }
828
829 void __init xen_pvmmu_arch_setup(void)
830 {
831         HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
832         HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
833
834         HYPERVISOR_vm_assist(VMASST_CMD_enable,
835                              VMASST_TYPE_pae_extended_cr3);
836
837         if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
838             register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
839                 BUG();
840
841         xen_enable_sysenter();
842         xen_enable_syscall();
843 }
844
845 /* This function is not called for HVM domains */
846 void __init xen_arch_setup(void)
847 {
848         xen_panic_handler_init();
849         if (!xen_feature(XENFEAT_auto_translated_physmap))
850                 xen_pvmmu_arch_setup();
851
852 #ifdef CONFIG_ACPI
853         if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
854                 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
855                 disable_acpi();
856         }
857 #endif
858
859         memcpy(boot_command_line, xen_start_info->cmd_line,
860                MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
861                COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
862
863         /* Set up idle, making sure it calls safe_halt() pvop */
864         disable_cpuidle();
865         disable_cpufreq();
866         WARN_ON(xen_set_default_idle());
867         fiddle_vdso();
868 #ifdef CONFIG_NUMA
869         numa_off = 1;
870 #endif
871 }