rename amd64 architecture to x86_64
[dragonfly.git] / sys / platform / pc64 / x86_64 / vm_machdep.c
CommitLineData
d7f50089
YY
1/*-
2 * Copyright (c) 1982, 1986 The Regents of the University of California.
3 * Copyright (c) 1989, 1990 William Jolitz
4 * Copyright (c) 1994 John Dyson
c8fe38ae 5 * Copyright (c) 2008 The DragonFly Project.
d7f50089
YY
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department, and William Jolitz.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
41 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
42 * $FreeBSD: src/sys/i386/i386/vm_machdep.c,v 1.132.2.9 2003/01/25 19:02:23 dillon Exp $
d7f50089
YY
43 */
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/malloc.h>
48#include <sys/proc.h>
49#include <sys/buf.h>
50#include <sys/interrupt.h>
51#include <sys/vnode.h>
52#include <sys/vmmeter.h>
53#include <sys/kernel.h>
54#include <sys/sysctl.h>
55#include <sys/unistd.h>
56
57#include <machine/clock.h>
58#include <machine/cpu.h>
59#include <machine/md_var.h>
60#include <machine/smp.h>
d7f50089 61#include <machine/pcb.h>
0855a2af 62#include <machine/pcb_ext.h>
d7f50089
YY
63#include <machine/segments.h>
64#include <machine/globaldata.h> /* npxthread */
65
66#include <vm/vm.h>
67#include <vm/vm_param.h>
68#include <sys/lock.h>
69#include <vm/vm_kern.h>
70#include <vm/vm_page.h>
71#include <vm/vm_map.h>
72#include <vm/vm_extern.h>
73
74#include <sys/thread2.h>
75
0855a2af 76#include <bus/isa/isa.h>
d7f50089 77
c8fe38ae 78static void cpu_reset_real (void);
d7f50089
YY
79/*
80 * Finish a fork operation, with lwp lp2 nearly set up.
81 * Copy and update the pcb, set up the stack so that the child
82 * ready to run and return to user mode.
83 */
84void
85cpu_fork(struct lwp *lp1, struct lwp *lp2, int flags)
86{
c8fe38ae
MD
87 struct pcb *pcb2;
88
89 if ((flags & RFPROC) == 0) {
90 if ((flags & RFMEM) == 0) {
91 /* unshare user LDT */
92 struct pcb *pcb1 = lp1->lwp_thread->td_pcb;
93 struct pcb_ldt *pcb_ldt = pcb1->pcb_ldt;
94 if (pcb_ldt && pcb_ldt->ldt_refcnt > 1) {
95 pcb_ldt = user_ldt_alloc(pcb1,pcb_ldt->ldt_len);
96 user_ldt_free(pcb1);
97 pcb1->pcb_ldt = pcb_ldt;
98 set_user_ldt(pcb1);
99 }
100 }
101 return;
102 }
103
c8fe38ae
MD
104 /* Ensure that lp1's pcb is up to date. */
105 if (mdcpu->gd_npxthread == lp1->lwp_thread)
106 npxsave(lp1->lwp_thread->td_savefpu);
c8fe38ae
MD
107
108 /*
109 * Copy lp1's PCB. This really only applies to the
110 * debug registers and FP state, but its faster to just copy the
111 * whole thing. Because we only save the PCB at switchout time,
112 * the register state may not be current.
113 */
114 pcb2 = lp2->lwp_thread->td_pcb;
115 *pcb2 = *lp1->lwp_thread->td_pcb;
116
117 /*
118 * Create a new fresh stack for the new process.
119 * Copy the trap frame for the return to user mode as if from a
120 * syscall. This copies the user mode register values.
121 *
122 * pcb_rsp must allocate an additional call-return pointer below
123 * the trap frame which will be restored by cpu_heavy_restore from
124 * PCB_RIP, and the thread's td_sp pointer must allocate an
125 * additonal two quadwords below the pcb_rsp call-return pointer to
126 * hold the LWKT restore function pointer and rflags.
127 *
128 * The LWKT restore function pointer must be set to cpu_heavy_restore,
129 * which is our standard heavy-weight process switch-in function.
130 * YYY eventually we should shortcut fork_return and fork_trampoline
131 * to use the LWKT restore function directly so we can get rid of
132 * all the extra crap we are setting up.
133 */
134 lp2->lwp_md.md_regs = (struct trapframe *)pcb2 - 1;
135 bcopy(lp1->lwp_md.md_regs, lp2->lwp_md.md_regs, sizeof(*lp2->lwp_md.md_regs));
136
137 /*
138 * Set registers for trampoline to user mode. Leave space for the
139 * return address on stack. These are the kernel mode register values.
140 */
48ffc236 141 pcb2->pcb_cr3 = vtophys(vmspace_pmap(lp2->lwp_proc->p_vmspace)->pm_pml4);
c8fe38ae
MD
142 pcb2->pcb_cr3 |= PG_RW | PG_U | PG_V;
143 pcb2->pcb_rbx = (unsigned long)fork_return; /* fork_trampoline argument */
144 pcb2->pcb_rbp = 0;
145 pcb2->pcb_rsp = (unsigned long)lp2->lwp_md.md_regs - sizeof(void *);
146 pcb2->pcb_r12 = (unsigned long)lp2; /* fork_trampoline argument */
147 pcb2->pcb_r13 = 0;
148 pcb2->pcb_r14 = 0;
149 pcb2->pcb_r15 = 0;
150 pcb2->pcb_rip = (unsigned long)fork_trampoline;
151 lp2->lwp_thread->td_sp = (char *)(pcb2->pcb_rsp - sizeof(void *));
152 *(u_int64_t *)lp2->lwp_thread->td_sp = PSL_USER;
153 lp2->lwp_thread->td_sp -= sizeof(void *);
154 *(void **)lp2->lwp_thread->td_sp = (void *)cpu_heavy_restore;
155
156 /*
157 * pcb2->pcb_ldt: duplicated below, if necessary.
158 * pcb2->pcb_savefpu: cloned above.
159 * pcb2->pcb_flags: cloned above (always 0 here?).
160 * pcb2->pcb_onfault: cloned above (always NULL here?).
161 */
162
163 /*
164 * XXX don't copy the i/o pages. this should probably be fixed.
165 */
166 pcb2->pcb_ext = 0;
167
168 /* Copy the LDT, if necessary. */
169 if (pcb2->pcb_ldt != 0) {
170 if (flags & RFMEM) {
171 pcb2->pcb_ldt->ldt_refcnt++;
172 } else {
173 pcb2->pcb_ldt = user_ldt_alloc(pcb2,
174 pcb2->pcb_ldt->ldt_len);
175 }
176 }
177 bcopy(&lp1->lwp_thread->td_tls, &lp2->lwp_thread->td_tls,
178 sizeof(lp2->lwp_thread->td_tls));
179 /*
180 * Now, cpu_switch() can schedule the new lwp.
181 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
182 * containing the return address when exiting cpu_switch.
183 * This will normally be to fork_trampoline(), which will have
184 * %rbx loaded with the new lwp's pointer. fork_trampoline()
185 * will set up a stack to call fork_return(lp, frame); to complete
186 * the return to user-mode.
187 */
d7f50089
YY
188}
189
190/*
191 * Prepare new lwp to return to the address specified in params.
192 */
193int
194cpu_prepare_lwp(struct lwp *lp, struct lwp_params *params)
195{
42d8ab5f
JG
196 struct trapframe *regs = lp->lwp_md.md_regs;
197 void *bad_return = NULL;
198 int error;
199
200 regs->tf_rip = (long)params->func;
201 regs->tf_rsp = (long)params->stack;
202 /* Set up argument for function call */
2123d4c3 203 regs->tf_rdi = (long)params->arg; /* JG Can this be in userspace addresses? */
42d8ab5f
JG
204 /*
205 * Set up fake return address. As the lwp function may never return,
206 * we simply copy out a NULL pointer and force the lwp to receive
207 * a SIGSEGV if it returns anyways.
208 */
209 regs->tf_rsp -= sizeof(void *);
210 error = copyout(&bad_return, (void *)regs->tf_rsp, sizeof(bad_return));
211 if (error)
212 return (error);
213
214 cpu_set_fork_handler(lp,
215 (void (*)(void *, struct trapframe *))generic_lwp_return, lp);
d7f50089
YY
216 return (0);
217}
218
219/*
220 * Intercept the return address from a freshly forked process that has NOT
221 * been scheduled yet.
222 *
223 * This is needed to make kernel threads stay in kernel mode.
224 */
225void
226cpu_set_fork_handler(struct lwp *lp, void (*func)(void *, struct trapframe *),
227 void *arg)
228{
c8fe38ae
MD
229 /*
230 * Note that the trap frame follows the args, so the function
231 * is really called like this: func(arg, frame);
232 */
233 lp->lwp_thread->td_pcb->pcb_rbx = (long)func; /* function */
234 lp->lwp_thread->td_pcb->pcb_r12 = (long)arg; /* first arg */
d7f50089
YY
235}
236
237void
238cpu_set_thread_handler(thread_t td, void (*rfunc)(void), void *func, void *arg)
239{
c8fe38ae
MD
240 td->td_pcb->pcb_rbx = (long)func;
241 td->td_pcb->pcb_r12 = (long)arg;
242 td->td_switch = cpu_lwkt_switch;
243 td->td_sp -= sizeof(void *);
244 *(void **)td->td_sp = rfunc; /* exit function on return */
245 td->td_sp -= sizeof(void *);
246 *(void **)td->td_sp = cpu_kthread_restore;
d7f50089
YY
247}
248
249void
250cpu_lwp_exit(void)
251{
0855a2af 252 struct thread *td = curthread;
48ffc236 253 struct pcb *pcb;
0855a2af 254 npxexit();
48ffc236 255 pcb = td->td_pcb;
0855a2af
JG
256 KKASSERT(pcb->pcb_ext == NULL); /* Some i386 functionality was dropped */
257 if (pcb->pcb_flags & PCB_DBREGS) {
258 /*
259 * disable all hardware breakpoints
260 */
261 reset_dbregs();
262 pcb->pcb_flags &= ~PCB_DBREGS;
263 }
264 td->td_gd->gd_cnt.v_swtch++;
265
266 crit_enter_quick(td);
09b62fa4
MD
267 if (td->td_flags & TDF_TSLEEPQ)
268 tsleep_remove(td);
0855a2af
JG
269 lwkt_deschedule_self(td);
270 lwkt_remove_tdallq(td);
271 cpu_thread_exit();
d7f50089
YY
272}
273
274/*
275 * Terminate the current thread. The caller must have already acquired
276 * the thread's rwlock and placed it on a reap list or otherwise notified
277 * a reaper of its existance. We set a special assembly switch function which
278 * releases td_rwlock after it has cleaned up the MMU state and switched
279 * out the stack.
280 *
281 * Must be caller from a critical section and with the thread descheduled.
282 */
283void
284cpu_thread_exit(void)
285{
0855a2af
JG
286 curthread->td_switch = cpu_exit_switch;
287 curthread->td_flags |= TDF_EXITING;
288 lwkt_switch();
289 panic("cpu_thread_exit: lwkt_switch() unexpectedly returned");
d7f50089
YY
290}
291
292/*
293 * Process Reaper. Called after the caller has acquired the thread's
294 * rwlock and removed it from the reap list.
295 */
296void
297cpu_proc_wait(struct proc *p)
298{
0855a2af
JG
299 /* drop per-process resources */
300 pmap_dispose_proc(p);
c8fe38ae
MD
301}
302
303void
304cpu_reset(void)
305{
306 cpu_reset_real();
307}
308
309static void
310cpu_reset_real(void)
311{
312 /*
313 * Attempt to do a CPU reset via the keyboard controller,
314 * do not turn of the GateA20, as any machine that fails
315 * to do the reset here would then end up in no man's land.
316 */
317
318#if !defined(BROKEN_KEYBOARD_RESET)
319 outb(IO_KBD + 4, 0xFE);
320 DELAY(500000); /* wait 0.5 sec to see if that did it */
321 kprintf("Keyboard reset did not work, attempting CPU shutdown\n");
322 DELAY(1000000); /* wait 1 sec for kprintf to complete */
323#endif
324#if JG
325 /* force a shutdown by unmapping entire address space ! */
326 bzero((caddr_t) PTD, PAGE_SIZE);
327#endif
328
329 /* "good night, sweet prince .... <THUNK!>" */
330 cpu_invltlb();
331 /* NOTREACHED */
332 while(1);
333}
334
00039178
YT
335/*
336 * Convert kernel VA to physical address
337 */
338vm_paddr_t
339kvtop(void *addr)
340{
341 vm_paddr_t pa;
342
343 pa = pmap_kextract((vm_offset_t)addr);
344 if (pa == 0)
345 panic("kvtop: zero page frame");
346 return (pa);
347}
348
c8fe38ae 349int
e54488bb 350grow_stack(struct proc *p, vm_offset_t sp)
c8fe38ae
MD
351{
352 int rv;
353
354 rv = vm_map_growstack (p, sp);
355 if (rv != KERN_SUCCESS)
356 return (0);
357
358 return (1);
359}
360
361/*
362 * Tell whether this address is in some physical memory region.
363 * Currently used by the kernel coredump code in order to avoid
364 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
365 * or other unpredictable behaviour.
366 */
367
368int
369is_physical_memory(vm_offset_t addr)
370{
371#if NISA > 0
372 /* The ISA ``memory hole''. */
373 if (addr >= 0xa0000 && addr < 0x100000)
374 return 0;
375#endif
376 /*
377 * stuff other tests for known memory-mapped devices (PCI?)
378 * here
379 */
380
381 return 1;
382}
383
384/*
c1543a89 385 * platform-specific vmspace initialization (nothing for x86_64)
c8fe38ae
MD
386 */
387void
388cpu_vmspace_alloc(struct vmspace *vm __unused)
389{
390}
391
392void
393cpu_vmspace_free(struct vmspace *vm __unused)
394{
d7f50089
YY
395}
396
397int
398kvm_access_check(vm_offset_t saddr, vm_offset_t eaddr, int prot)
399{
0855a2af
JG
400 vm_offset_t addr;
401
402 if (saddr < KvaStart)
403 return EFAULT;
404 if (eaddr >= KvaEnd)
405 return EFAULT;
406 for (addr = saddr; addr < eaddr; addr += PAGE_SIZE) {
407 if (pmap_extract(&kernel_pmap, addr) == 0)
408 return EFAULT;
409 }
410 if (!kernacc((caddr_t)saddr, eaddr - saddr, prot))
411 return EFAULT;
d7f50089
YY
412 return 0;
413}
414