kernel - Fix floating point save state structure and minor npx issues
[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>
684a93c4 75#include <sys/mplock2.h>
d7f50089 76
0855a2af 77#include <bus/isa/isa.h>
d7f50089 78
c8fe38ae 79static void cpu_reset_real (void);
d7f50089
YY
80/*
81 * Finish a fork operation, with lwp lp2 nearly set up.
82 * Copy and update the pcb, set up the stack so that the child
83 * ready to run and return to user mode.
84 */
85void
86cpu_fork(struct lwp *lp1, struct lwp *lp2, int flags)
87{
c8fe38ae
MD
88 struct pcb *pcb2;
89
90 if ((flags & RFPROC) == 0) {
91 if ((flags & RFMEM) == 0) {
92 /* unshare user LDT */
93 struct pcb *pcb1 = lp1->lwp_thread->td_pcb;
94 struct pcb_ldt *pcb_ldt = pcb1->pcb_ldt;
95 if (pcb_ldt && pcb_ldt->ldt_refcnt > 1) {
96 pcb_ldt = user_ldt_alloc(pcb1,pcb_ldt->ldt_len);
97 user_ldt_free(pcb1);
98 pcb1->pcb_ldt = pcb_ldt;
99 set_user_ldt(pcb1);
100 }
101 }
102 return;
103 }
104
c8fe38ae
MD
105 /* Ensure that lp1's pcb is up to date. */
106 if (mdcpu->gd_npxthread == lp1->lwp_thread)
107 npxsave(lp1->lwp_thread->td_savefpu);
c8fe38ae
MD
108
109 /*
110 * Copy lp1's PCB. This really only applies to the
111 * debug registers and FP state, but its faster to just copy the
112 * whole thing. Because we only save the PCB at switchout time,
113 * the register state may not be current.
114 */
115 pcb2 = lp2->lwp_thread->td_pcb;
116 *pcb2 = *lp1->lwp_thread->td_pcb;
117
118 /*
119 * Create a new fresh stack for the new process.
120 * Copy the trap frame for the return to user mode as if from a
121 * syscall. This copies the user mode register values.
122 *
123 * pcb_rsp must allocate an additional call-return pointer below
124 * the trap frame which will be restored by cpu_heavy_restore from
125 * PCB_RIP, and the thread's td_sp pointer must allocate an
126 * additonal two quadwords below the pcb_rsp call-return pointer to
127 * hold the LWKT restore function pointer and rflags.
128 *
129 * The LWKT restore function pointer must be set to cpu_heavy_restore,
130 * which is our standard heavy-weight process switch-in function.
131 * YYY eventually we should shortcut fork_return and fork_trampoline
132 * to use the LWKT restore function directly so we can get rid of
133 * all the extra crap we are setting up.
134 */
135 lp2->lwp_md.md_regs = (struct trapframe *)pcb2 - 1;
136 bcopy(lp1->lwp_md.md_regs, lp2->lwp_md.md_regs, sizeof(*lp2->lwp_md.md_regs));
137
138 /*
139 * Set registers for trampoline to user mode. Leave space for the
140 * return address on stack. These are the kernel mode register values.
141 */
48ffc236 142 pcb2->pcb_cr3 = vtophys(vmspace_pmap(lp2->lwp_proc->p_vmspace)->pm_pml4);
c8fe38ae
MD
143 pcb2->pcb_cr3 |= PG_RW | PG_U | PG_V;
144 pcb2->pcb_rbx = (unsigned long)fork_return; /* fork_trampoline argument */
145 pcb2->pcb_rbp = 0;
146 pcb2->pcb_rsp = (unsigned long)lp2->lwp_md.md_regs - sizeof(void *);
147 pcb2->pcb_r12 = (unsigned long)lp2; /* fork_trampoline argument */
148 pcb2->pcb_r13 = 0;
149 pcb2->pcb_r14 = 0;
150 pcb2->pcb_r15 = 0;
151 pcb2->pcb_rip = (unsigned long)fork_trampoline;
152 lp2->lwp_thread->td_sp = (char *)(pcb2->pcb_rsp - sizeof(void *));
153 *(u_int64_t *)lp2->lwp_thread->td_sp = PSL_USER;
154 lp2->lwp_thread->td_sp -= sizeof(void *);
155 *(void **)lp2->lwp_thread->td_sp = (void *)cpu_heavy_restore;
156
157 /*
158 * pcb2->pcb_ldt: duplicated below, if necessary.
159 * pcb2->pcb_savefpu: cloned above.
160 * pcb2->pcb_flags: cloned above (always 0 here?).
161 * pcb2->pcb_onfault: cloned above (always NULL here?).
162 */
163
164 /*
165 * XXX don't copy the i/o pages. this should probably be fixed.
166 */
167 pcb2->pcb_ext = 0;
168
169 /* Copy the LDT, if necessary. */
170 if (pcb2->pcb_ldt != 0) {
171 if (flags & RFMEM) {
172 pcb2->pcb_ldt->ldt_refcnt++;
173 } else {
174 pcb2->pcb_ldt = user_ldt_alloc(pcb2,
175 pcb2->pcb_ldt->ldt_len);
176 }
177 }
178 bcopy(&lp1->lwp_thread->td_tls, &lp2->lwp_thread->td_tls,
179 sizeof(lp2->lwp_thread->td_tls));
180 /*
181 * Now, cpu_switch() can schedule the new lwp.
182 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
183 * containing the return address when exiting cpu_switch.
184 * This will normally be to fork_trampoline(), which will have
185 * %rbx loaded with the new lwp's pointer. fork_trampoline()
186 * will set up a stack to call fork_return(lp, frame); to complete
187 * the return to user-mode.
188 */
d7f50089
YY
189}
190
191/*
192 * Prepare new lwp to return to the address specified in params.
193 */
194int
195cpu_prepare_lwp(struct lwp *lp, struct lwp_params *params)
196{
42d8ab5f
JG
197 struct trapframe *regs = lp->lwp_md.md_regs;
198 void *bad_return = NULL;
199 int error;
200
201 regs->tf_rip = (long)params->func;
202 regs->tf_rsp = (long)params->stack;
203 /* Set up argument for function call */
3338cc67
MD
204 regs->tf_rdi = (long)params->arg;
205
42d8ab5f
JG
206 /*
207 * Set up fake return address. As the lwp function may never return,
208 * we simply copy out a NULL pointer and force the lwp to receive
209 * a SIGSEGV if it returns anyways.
210 */
211 regs->tf_rsp -= sizeof(void *);
212 error = copyout(&bad_return, (void *)regs->tf_rsp, sizeof(bad_return));
213 if (error)
214 return (error);
215
216 cpu_set_fork_handler(lp,
217 (void (*)(void *, struct trapframe *))generic_lwp_return, lp);
d7f50089
YY
218 return (0);
219}
220
221/*
222 * Intercept the return address from a freshly forked process that has NOT
223 * been scheduled yet.
224 *
225 * This is needed to make kernel threads stay in kernel mode.
226 */
227void
228cpu_set_fork_handler(struct lwp *lp, void (*func)(void *, struct trapframe *),
229 void *arg)
230{
c8fe38ae
MD
231 /*
232 * Note that the trap frame follows the args, so the function
233 * is really called like this: func(arg, frame);
234 */
235 lp->lwp_thread->td_pcb->pcb_rbx = (long)func; /* function */
236 lp->lwp_thread->td_pcb->pcb_r12 = (long)arg; /* first arg */
d7f50089
YY
237}
238
239void
240cpu_set_thread_handler(thread_t td, void (*rfunc)(void), void *func, void *arg)
241{
c8fe38ae
MD
242 td->td_pcb->pcb_rbx = (long)func;
243 td->td_pcb->pcb_r12 = (long)arg;
244 td->td_switch = cpu_lwkt_switch;
245 td->td_sp -= sizeof(void *);
246 *(void **)td->td_sp = rfunc; /* exit function on return */
247 td->td_sp -= sizeof(void *);
248 *(void **)td->td_sp = cpu_kthread_restore;
d7f50089
YY
249}
250
251void
252cpu_lwp_exit(void)
253{
0855a2af 254 struct thread *td = curthread;
48ffc236 255 struct pcb *pcb;
2883d2d8 256
48ffc236 257 pcb = td->td_pcb;
2883d2d8
MD
258
259 /* Some i386 functionality was dropped */
260 KKASSERT(pcb->pcb_ext == NULL);
261
262 /*
263 * disable all hardware breakpoints
264 */
0855a2af 265 if (pcb->pcb_flags & PCB_DBREGS) {
0855a2af
JG
266 reset_dbregs();
267 pcb->pcb_flags &= ~PCB_DBREGS;
268 }
269 td->td_gd->gd_cnt.v_swtch++;
270
271 crit_enter_quick(td);
09b62fa4
MD
272 if (td->td_flags & TDF_TSLEEPQ)
273 tsleep_remove(td);
0855a2af
JG
274 lwkt_deschedule_self(td);
275 lwkt_remove_tdallq(td);
276 cpu_thread_exit();
d7f50089
YY
277}
278
279/*
280 * Terminate the current thread. The caller must have already acquired
281 * the thread's rwlock and placed it on a reap list or otherwise notified
282 * a reaper of its existance. We set a special assembly switch function which
283 * releases td_rwlock after it has cleaned up the MMU state and switched
284 * out the stack.
285 *
286 * Must be caller from a critical section and with the thread descheduled.
287 */
288void
289cpu_thread_exit(void)
290{
9e6e869e 291 npxexit();
0855a2af
JG
292 curthread->td_switch = cpu_exit_switch;
293 curthread->td_flags |= TDF_EXITING;
294 lwkt_switch();
295 panic("cpu_thread_exit: lwkt_switch() unexpectedly returned");
d7f50089
YY
296}
297
c8fe38ae
MD
298void
299cpu_reset(void)
300{
301 cpu_reset_real();
302}
303
304static void
305cpu_reset_real(void)
306{
307 /*
308 * Attempt to do a CPU reset via the keyboard controller,
309 * do not turn of the GateA20, as any machine that fails
310 * to do the reset here would then end up in no man's land.
311 */
312
313#if !defined(BROKEN_KEYBOARD_RESET)
314 outb(IO_KBD + 4, 0xFE);
315 DELAY(500000); /* wait 0.5 sec to see if that did it */
316 kprintf("Keyboard reset did not work, attempting CPU shutdown\n");
317 DELAY(1000000); /* wait 1 sec for kprintf to complete */
318#endif
319#if JG
320 /* force a shutdown by unmapping entire address space ! */
321 bzero((caddr_t) PTD, PAGE_SIZE);
322#endif
323
324 /* "good night, sweet prince .... <THUNK!>" */
325 cpu_invltlb();
326 /* NOTREACHED */
327 while(1);
328}
329
00039178
YT
330/*
331 * Convert kernel VA to physical address
332 */
333vm_paddr_t
334kvtop(void *addr)
335{
336 vm_paddr_t pa;
337
338 pa = pmap_kextract((vm_offset_t)addr);
339 if (pa == 0)
340 panic("kvtop: zero page frame");
341 return (pa);
342}
343
0d4f82f8
MD
344static void
345swi_vm(void *arg, void *frame)
346{
347 if (busdma_swi_pending != 0)
348 busdma_swi();
349}
350
351static void
352swi_vm_setup(void *arg)
353{
1da8d52f 354 register_swi(SWI_VM, swi_vm, NULL, "swi_vm", NULL, 0);
0d4f82f8
MD
355}
356
357SYSINIT(vm_setup, SI_BOOT2_MACHDEP, SI_ORDER_ANY, swi_vm_setup, NULL);
358
359
c8fe38ae
MD
360/*
361 * Tell whether this address is in some physical memory region.
362 * Currently used by the kernel coredump code in order to avoid
363 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
364 * or other unpredictable behaviour.
365 */
366
367int
368is_physical_memory(vm_offset_t addr)
369{
370#if NISA > 0
371 /* The ISA ``memory hole''. */
372 if (addr >= 0xa0000 && addr < 0x100000)
373 return 0;
374#endif
375 /*
376 * stuff other tests for known memory-mapped devices (PCI?)
377 * here
378 */
379
380 return 1;
381}
382
383/*
b2b3ffcd 384 * platform-specific vmspace initialization (nothing for x86_64)
c8fe38ae
MD
385 */
386void
387cpu_vmspace_alloc(struct vmspace *vm __unused)
388{
389}
390
391void
392cpu_vmspace_free(struct vmspace *vm __unused)
393{
d7f50089
YY
394}
395
396int
397kvm_access_check(vm_offset_t saddr, vm_offset_t eaddr, int prot)
398{
0855a2af
JG
399 vm_offset_t addr;
400
401 if (saddr < KvaStart)
402 return EFAULT;
403 if (eaddr >= KvaEnd)
404 return EFAULT;
405 for (addr = saddr; addr < eaddr; addr += PAGE_SIZE) {
406 if (pmap_extract(&kernel_pmap, addr) == 0)
407 return EFAULT;
408 }
409 if (!kernacc((caddr_t)saddr, eaddr - saddr, prot))
410 return EFAULT;
d7f50089
YY
411 return 0;
412}
413
3338cc67
MD
414#if 0
415
416void _test_frame_enter(struct trapframe *frame);
417void _test_frame_exit(struct trapframe *frame);
418
419void
420_test_frame_enter(struct trapframe *frame)
421{
422 thread_t td = curthread;
423
424 if (ISPL(frame->tf_cs) == SEL_UPL) {
425 KKASSERT(td->td_lwp);
426 KASSERT(td->td_lwp->lwp_md.md_regs == frame,
427 ("_test_frame_exit: Frame mismatch %p %p",
428 td->td_lwp->lwp_md.md_regs, frame));
429 td->td_lwp->lwp_saveusp = (void *)frame->tf_rsp;
430 td->td_lwp->lwp_saveupc = (void *)frame->tf_rip;
431 }
432 if ((char *)frame < td->td_kstack ||
433 (char *)frame > td->td_kstack + td->td_kstack_size) {
434 panic("_test_frame_exit: frame not on kstack %p kstack=%p\n",
435 frame, td->td_kstack);
436 }
437}
438
439void
440_test_frame_exit(struct trapframe *frame)
441{
442 thread_t td = curthread;
443
444 if (ISPL(frame->tf_cs) == SEL_UPL) {
445 KKASSERT(td->td_lwp);
446 KASSERT(td->td_lwp->lwp_md.md_regs == frame,
447 ("_test_frame_exit: Frame mismatch %p %p",
448 td->td_lwp->lwp_md.md_regs, frame));
449 if (td->td_lwp->lwp_saveusp != (void *)frame->tf_rsp) {
450 kprintf("_test_frame_exit: %s:%d usp mismatch %p/%p\n",
451 td->td_comm, td->td_proc->p_pid,
452 td->td_lwp->lwp_saveusp,
453 (void *)frame->tf_rsp);
454 }
455 if (td->td_lwp->lwp_saveupc != (void *)frame->tf_rip) {
456 kprintf("_test_frame_exit: %s:%d upc mismatch %p/%p\n",
457 td->td_comm, td->td_proc->p_pid,
458 td->td_lwp->lwp_saveupc,
459 (void *)frame->tf_rip);
460 }
461
462 /*
463 * adulterate the fields to catch entries that
464 * don't run through test_frame_enter
465 */
466 td->td_lwp->lwp_saveusp =
467 (void *)~(intptr_t)td->td_lwp->lwp_saveusp;
468 td->td_lwp->lwp_saveupc =
469 (void *)~(intptr_t)td->td_lwp->lwp_saveupc;
470 }
471 if ((char *)frame < td->td_kstack ||
472 (char *)frame > td->td_kstack + td->td_kstack_size) {
473 panic("_test_frame_exit: frame not on kstack %p kstack=%p\n",
474 frame, td->td_kstack);
475 }
476}
477
478#endif