2 * Machine specific setup for xen
4 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
7 #include <linux/module.h>
8 #include <linux/sched.h>
15 #include <asm/setup.h>
17 #include <asm/xen/hypervisor.h>
18 #include <asm/xen/hypercall.h>
21 #include <xen/interface/callback.h>
22 #include <xen/interface/memory.h>
23 #include <xen/interface/physdev.h>
24 #include <xen/interface/memory.h>
25 #include <xen/features.h>
30 /* These are code, but not functions. Defined in entry.S */
31 extern const char xen_hypervisor_callback
[];
32 extern const char xen_failsafe_callback
[];
33 extern void xen_sysenter_target(void);
34 extern void xen_syscall_target(void);
35 extern void xen_syscall32_target(void);
37 /* Amount of extra memory space we add to the e820 ranges */
38 phys_addr_t xen_extra_mem_start
, xen_extra_mem_size
;
40 static __init
void xen_add_extra_mem(unsigned long pages
)
42 u64 size
= (u64
)pages
* PAGE_SIZE
;
47 e820_add_region(xen_extra_mem_start
+ xen_extra_mem_size
, size
, E820_RAM
);
48 sanitize_e820_map(e820
.map
, ARRAY_SIZE(e820
.map
), &e820
.nr_map
);
50 reserve_early(xen_extra_mem_start
+ xen_extra_mem_size
,
51 xen_extra_mem_start
+ xen_extra_mem_size
+ size
,
54 xen_extra_mem_size
+= size
;
57 static unsigned long __init
xen_release_chunk(phys_addr_t start_addr
,
60 struct xen_memory_reservation reservation
= {
65 unsigned long start
, end
;
66 unsigned long len
= 0;
70 start
= PFN_UP(start_addr
);
71 end
= PFN_DOWN(end_addr
);
76 printk(KERN_INFO
"xen_release_chunk: looking at area pfn %lx-%lx: ",
78 for(pfn
= start
; pfn
< end
; pfn
++) {
79 unsigned long mfn
= pfn_to_mfn(pfn
);
81 /* Make sure pfn exists to start with */
82 if (mfn
== INVALID_P2M_ENTRY
|| mfn_to_pfn(mfn
) != pfn
)
85 set_xen_guest_handle(reservation
.extent_start
, &mfn
);
86 reservation
.nr_extents
= 1;
88 ret
= HYPERVISOR_memory_op(XENMEM_decrease_reservation
,
90 WARN(ret
!= 1, "Failed to release memory %lx-%lx err=%d\n",
93 set_phys_to_machine(pfn
, INVALID_P2M_ENTRY
);
97 printk(KERN_CONT
"%ld pages freed\n", len
);
102 static unsigned long __init
xen_return_unused_memory(unsigned long max_pfn
,
103 const struct e820map
*e820
)
105 phys_addr_t max_addr
= PFN_PHYS(max_pfn
);
106 phys_addr_t last_end
= 0;
107 unsigned long released
= 0;
110 for (i
= 0; i
< e820
->nr_map
&& last_end
< max_addr
; i
++) {
111 phys_addr_t end
= e820
->map
[i
].addr
;
112 end
= min(max_addr
, end
);
114 released
+= xen_release_chunk(last_end
, end
);
115 last_end
= e820
->map
[i
].addr
+ e820
->map
[i
].size
;
118 if (last_end
< max_addr
)
119 released
+= xen_release_chunk(last_end
, max_addr
);
121 printk(KERN_INFO
"released %ld pages of unused memory\n", released
);
126 * machine_specific_memory_setup - Hook for machine specific memory setup.
128 char * __init
xen_memory_setup(void)
130 static struct e820entry map
[E820MAX
] __initdata
;
132 unsigned long max_pfn
= xen_start_info
->nr_pages
;
133 unsigned long long mem_end
;
135 struct xen_memory_map memmap
;
136 unsigned long extra_pages
= 0;
139 max_pfn
= min(MAX_DOMAIN_PAGES
, max_pfn
);
140 mem_end
= PFN_PHYS(max_pfn
);
142 memmap
.nr_entries
= E820MAX
;
143 set_xen_guest_handle(memmap
.buffer
, map
);
145 rc
= HYPERVISOR_memory_op(XENMEM_memory_map
, &memmap
);
147 memmap
.nr_entries
= 1;
149 map
[0].size
= mem_end
;
150 /* 8MB slack (to balance backend allocations). */
151 map
[0].size
+= 8ULL << 20;
152 map
[0].type
= E820_RAM
;
158 xen_extra_mem_start
= mem_end
;
159 for (i
= 0; i
< memmap
.nr_entries
; i
++) {
160 unsigned long long end
= map
[i
].addr
+ map
[i
].size
;
161 if (map
[i
].type
== E820_RAM
) {
162 if (map
[i
].addr
> mem_end
)
165 /* Truncate region to max_mem. */
166 map
[i
].size
-= end
- mem_end
;
168 extra_pages
+= PFN_DOWN(end
- mem_end
);
172 e820_add_region(map
[i
].addr
, map
[i
].size
, map
[i
].type
);
176 * Even though this is normal, usable memory under Xen, reserve
177 * ISA memory anyway because too many things think they can poke
180 e820_add_region(ISA_START_ADDRESS
, ISA_END_ADDRESS
- ISA_START_ADDRESS
,
187 * See comment above "struct start_info" in <xen/interface/xen.h>
189 reserve_early(__pa(xen_start_info
->mfn_list
),
190 __pa(xen_start_info
->pt_base
),
193 sanitize_e820_map(e820
.map
, ARRAY_SIZE(e820
.map
), &e820
.nr_map
);
195 extra_pages
+= xen_return_unused_memory(xen_start_info
->nr_pages
, &e820
);
197 xen_add_extra_mem(extra_pages
);
202 static void xen_idle(void)
209 current_thread_info()->status
&= ~TS_POLLING
;
210 smp_mb__after_clear_bit();
212 current_thread_info()->status
|= TS_POLLING
;
217 * Set the bit indicating "nosegneg" library variants should be used.
218 * We only need to bother in pure 32-bit mode; compat 32-bit processes
219 * can have un-truncated segments, so wrapping around is allowed.
221 static void __init
fiddle_vdso(void)
225 mask
= VDSO32_SYMBOL(&vdso32_int80_start
, NOTE_MASK
);
226 *mask
|= 1 << VDSO_NOTE_NONEGSEG_BIT
;
227 mask
= VDSO32_SYMBOL(&vdso32_sysenter_start
, NOTE_MASK
);
228 *mask
|= 1 << VDSO_NOTE_NONEGSEG_BIT
;
232 static __cpuinit
int register_callback(unsigned type
, const void *func
)
234 struct callback_register callback
= {
236 .address
= XEN_CALLBACK(__KERNEL_CS
, func
),
237 .flags
= CALLBACKF_mask_events
,
240 return HYPERVISOR_callback_op(CALLBACKOP_register
, &callback
);
243 void __cpuinit
xen_enable_sysenter(void)
246 unsigned sysenter_feature
;
249 sysenter_feature
= X86_FEATURE_SEP
;
251 sysenter_feature
= X86_FEATURE_SYSENTER32
;
254 if (!boot_cpu_has(sysenter_feature
))
257 ret
= register_callback(CALLBACKTYPE_sysenter
, xen_sysenter_target
);
259 setup_clear_cpu_cap(sysenter_feature
);
262 void __cpuinit
xen_enable_syscall(void)
267 ret
= register_callback(CALLBACKTYPE_syscall
, xen_syscall_target
);
269 printk(KERN_ERR
"Failed to set syscall callback: %d\n", ret
);
270 /* Pretty fatal; 64-bit userspace has no other
271 mechanism for syscalls. */
274 if (boot_cpu_has(X86_FEATURE_SYSCALL32
)) {
275 ret
= register_callback(CALLBACKTYPE_syscall32
,
276 xen_syscall32_target
);
278 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32
);
280 #endif /* CONFIG_X86_64 */
283 void __init
xen_arch_setup(void)
285 struct physdev_set_iopl set_iopl
;
288 xen_panic_handler_init();
290 HYPERVISOR_vm_assist(VMASST_CMD_enable
, VMASST_TYPE_4gb_segments
);
291 HYPERVISOR_vm_assist(VMASST_CMD_enable
, VMASST_TYPE_writable_pagetables
);
293 if (!xen_feature(XENFEAT_auto_translated_physmap
))
294 HYPERVISOR_vm_assist(VMASST_CMD_enable
,
295 VMASST_TYPE_pae_extended_cr3
);
297 if (register_callback(CALLBACKTYPE_event
, xen_hypervisor_callback
) ||
298 register_callback(CALLBACKTYPE_failsafe
, xen_failsafe_callback
))
301 xen_enable_sysenter();
302 xen_enable_syscall();
305 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl
, &set_iopl
);
307 printk(KERN_INFO
"physdev_op failed %d\n", rc
);
310 if (!(xen_start_info
->flags
& SIF_INITDOMAIN
)) {
311 printk(KERN_INFO
"ACPI in unprivileged domain disabled\n");
316 memcpy(boot_command_line
, xen_start_info
->cmd_line
,
317 MAX_GUEST_CMDLINE
> COMMAND_LINE_SIZE
?
318 COMMAND_LINE_SIZE
: MAX_GUEST_CMDLINE
);
322 paravirt_disable_iospace();