AMD64 - Fix many compile-time warnings. int/ptr type mismatches, %llx, etc.
[dragonfly.git] / sys / platform / pc64 / amd64 / 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 $
c8fe38ae 43 * $DragonFly: src/sys/platform/pc64/amd64/vm_machdep.c,v 1.3 2008/08/29 17:07:10 dillon Exp $
d7f50089
YY
44 */
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/malloc.h>
49#include <sys/proc.h>
50#include <sys/buf.h>
51#include <sys/interrupt.h>
52#include <sys/vnode.h>
53#include <sys/vmmeter.h>
54#include <sys/kernel.h>
55#include <sys/sysctl.h>
56#include <sys/unistd.h>
57
58#include <machine/clock.h>
59#include <machine/cpu.h>
60#include <machine/md_var.h>
61#include <machine/smp.h>
d7f50089 62#include <machine/pcb.h>
0855a2af 63#include <machine/pcb_ext.h>
d7f50089
YY
64#include <machine/segments.h>
65#include <machine/globaldata.h> /* npxthread */
66
67#include <vm/vm.h>
68#include <vm/vm_param.h>
69#include <sys/lock.h>
70#include <vm/vm_kern.h>
71#include <vm/vm_page.h>
72#include <vm/vm_map.h>
73#include <vm/vm_extern.h>
74
75#include <sys/thread2.h>
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
105#if NNPX > 0
106 /* Ensure that lp1's pcb is up to date. */
107 if (mdcpu->gd_npxthread == lp1->lwp_thread)
108 npxsave(lp1->lwp_thread->td_savefpu);
109#endif
110
111 /*
112 * Copy lp1's PCB. This really only applies to the
113 * debug registers and FP state, but its faster to just copy the
114 * whole thing. Because we only save the PCB at switchout time,
115 * the register state may not be current.
116 */
117 pcb2 = lp2->lwp_thread->td_pcb;
118 *pcb2 = *lp1->lwp_thread->td_pcb;
119
120 /*
121 * Create a new fresh stack for the new process.
122 * Copy the trap frame for the return to user mode as if from a
123 * syscall. This copies the user mode register values.
124 *
125 * pcb_rsp must allocate an additional call-return pointer below
126 * the trap frame which will be restored by cpu_heavy_restore from
127 * PCB_RIP, and the thread's td_sp pointer must allocate an
128 * additonal two quadwords below the pcb_rsp call-return pointer to
129 * hold the LWKT restore function pointer and rflags.
130 *
131 * The LWKT restore function pointer must be set to cpu_heavy_restore,
132 * which is our standard heavy-weight process switch-in function.
133 * YYY eventually we should shortcut fork_return and fork_trampoline
134 * to use the LWKT restore function directly so we can get rid of
135 * all the extra crap we are setting up.
136 */
137 lp2->lwp_md.md_regs = (struct trapframe *)pcb2 - 1;
138 bcopy(lp1->lwp_md.md_regs, lp2->lwp_md.md_regs, sizeof(*lp2->lwp_md.md_regs));
139
140 /*
141 * Set registers for trampoline to user mode. Leave space for the
142 * return address on stack. These are the kernel mode register values.
143 */
67553e72 144 pcb2->pcb_cr3 = vtophys(vmspace_pmap(lp2->lwp_proc->p_vmspace)->pm_pml4);
c8fe38ae
MD
145 pcb2->pcb_cr3 |= PG_RW | PG_U | PG_V;
146 pcb2->pcb_rbx = (unsigned long)fork_return; /* fork_trampoline argument */
147 pcb2->pcb_rbp = 0;
148 pcb2->pcb_rsp = (unsigned long)lp2->lwp_md.md_regs - sizeof(void *);
149 pcb2->pcb_r12 = (unsigned long)lp2; /* fork_trampoline argument */
150 pcb2->pcb_r13 = 0;
151 pcb2->pcb_r14 = 0;
152 pcb2->pcb_r15 = 0;
153 pcb2->pcb_rip = (unsigned long)fork_trampoline;
154 lp2->lwp_thread->td_sp = (char *)(pcb2->pcb_rsp - sizeof(void *));
155 *(u_int64_t *)lp2->lwp_thread->td_sp = PSL_USER;
156 lp2->lwp_thread->td_sp -= sizeof(void *);
157 *(void **)lp2->lwp_thread->td_sp = (void *)cpu_heavy_restore;
158
159 /*
160 * pcb2->pcb_ldt: duplicated below, if necessary.
161 * pcb2->pcb_savefpu: cloned above.
162 * pcb2->pcb_flags: cloned above (always 0 here?).
163 * pcb2->pcb_onfault: cloned above (always NULL here?).
164 */
165
166 /*
167 * XXX don't copy the i/o pages. this should probably be fixed.
168 */
169 pcb2->pcb_ext = 0;
170
171 /* Copy the LDT, if necessary. */
172 if (pcb2->pcb_ldt != 0) {
173 if (flags & RFMEM) {
174 pcb2->pcb_ldt->ldt_refcnt++;
175 } else {
176 pcb2->pcb_ldt = user_ldt_alloc(pcb2,
177 pcb2->pcb_ldt->ldt_len);
178 }
179 }
180 bcopy(&lp1->lwp_thread->td_tls, &lp2->lwp_thread->td_tls,
181 sizeof(lp2->lwp_thread->td_tls));
182 /*
183 * Now, cpu_switch() can schedule the new lwp.
184 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
185 * containing the return address when exiting cpu_switch.
186 * This will normally be to fork_trampoline(), which will have
187 * %rbx loaded with the new lwp's pointer. fork_trampoline()
188 * will set up a stack to call fork_return(lp, frame); to complete
189 * the return to user-mode.
190 */
d7f50089
YY
191}
192
193/*
194 * Prepare new lwp to return to the address specified in params.
195 */
196int
197cpu_prepare_lwp(struct lwp *lp, struct lwp_params *params)
198{
973c11b9
MD
199 struct trapframe *regs = lp->lwp_md.md_regs;
200 void *bad_return = NULL;
201 int error;
202
203 regs->tf_rip = (long)params->func;
204 regs->tf_rsp = (long)params->stack;
205 /* Set up argument for function call */
206 regs->tf_rdi = (long)params->arg;
207 /*
208 * Set up fake return address. As the lwp function may never return,
209 * we simply copy out a NULL pointer and force the lwp to receive
210 * a SIGSEGV if it returns anyways.
211 */
212 regs->tf_rsp -= sizeof(void *);
213 error = copyout(&bad_return, (void *)regs->tf_rsp, sizeof(bad_return));
214 if (error)
215 return (error);
216
217 cpu_set_fork_handler(lp,
218 (void (*)(void *, struct trapframe *))generic_lwp_return, lp);
d7f50089
YY
219 return (0);
220}
221
222/*
223 * Intercept the return address from a freshly forked process that has NOT
224 * been scheduled yet.
225 *
226 * This is needed to make kernel threads stay in kernel mode.
227 */
228void
229cpu_set_fork_handler(struct lwp *lp, void (*func)(void *, struct trapframe *),
230 void *arg)
231{
c8fe38ae
MD
232 /*
233 * Note that the trap frame follows the args, so the function
234 * is really called like this: func(arg, frame);
235 */
236 lp->lwp_thread->td_pcb->pcb_rbx = (long)func; /* function */
237 lp->lwp_thread->td_pcb->pcb_r12 = (long)arg; /* first arg */
d7f50089
YY
238}
239
240void
241cpu_set_thread_handler(thread_t td, void (*rfunc)(void), void *func, void *arg)
242{
c8fe38ae
MD
243 td->td_pcb->pcb_rbx = (long)func;
244 td->td_pcb->pcb_r12 = (long)arg;
245 td->td_switch = cpu_lwkt_switch;
246 td->td_sp -= sizeof(void *);
247 *(void **)td->td_sp = rfunc; /* exit function on return */
248 td->td_sp -= sizeof(void *);
249 *(void **)td->td_sp = cpu_kthread_restore;
d7f50089
YY
250}
251
252void
253cpu_lwp_exit(void)
254{
0855a2af 255 struct thread *td = curthread;
67553e72 256 struct pcb *pcb;
0855a2af
JG
257#if NNPX > 0
258 npxexit();
259#endif /* NNPX */
67553e72 260 pcb = td->td_pcb;
0855a2af
JG
261 KKASSERT(pcb->pcb_ext == NULL); /* Some i386 functionality was dropped */
262 if (pcb->pcb_flags & PCB_DBREGS) {
263 /*
264 * disable all hardware breakpoints
265 */
266 reset_dbregs();
267 pcb->pcb_flags &= ~PCB_DBREGS;
268 }
269 td->td_gd->gd_cnt.v_swtch++;
270
271 crit_enter_quick(td);
272 lwkt_deschedule_self(td);
273 lwkt_remove_tdallq(td);
274 cpu_thread_exit();
d7f50089
YY
275}
276
277/*
278 * Terminate the current thread. The caller must have already acquired
279 * the thread's rwlock and placed it on a reap list or otherwise notified
280 * a reaper of its existance. We set a special assembly switch function which
281 * releases td_rwlock after it has cleaned up the MMU state and switched
282 * out the stack.
283 *
284 * Must be caller from a critical section and with the thread descheduled.
285 */
286void
287cpu_thread_exit(void)
288{
0855a2af
JG
289 curthread->td_switch = cpu_exit_switch;
290 curthread->td_flags |= TDF_EXITING;
291 lwkt_switch();
292 panic("cpu_thread_exit: lwkt_switch() unexpectedly returned");
d7f50089
YY
293}
294
295/*
296 * Process Reaper. Called after the caller has acquired the thread's
297 * rwlock and removed it from the reap list.
298 */
299void
300cpu_proc_wait(struct proc *p)
301{
0855a2af
JG
302 /* drop per-process resources */
303 pmap_dispose_proc(p);
c8fe38ae
MD
304}
305
306void
307cpu_reset(void)
308{
309 cpu_reset_real();
310}
311
312static void
313cpu_reset_real(void)
314{
315 /*
316 * Attempt to do a CPU reset via the keyboard controller,
317 * do not turn of the GateA20, as any machine that fails
318 * to do the reset here would then end up in no man's land.
319 */
320
321#if !defined(BROKEN_KEYBOARD_RESET)
322 outb(IO_KBD + 4, 0xFE);
323 DELAY(500000); /* wait 0.5 sec to see if that did it */
324 kprintf("Keyboard reset did not work, attempting CPU shutdown\n");
325 DELAY(1000000); /* wait 1 sec for kprintf to complete */
326#endif
327#if JG
328 /* force a shutdown by unmapping entire address space ! */
329 bzero((caddr_t) PTD, PAGE_SIZE);
330#endif
331
332 /* "good night, sweet prince .... <THUNK!>" */
333 cpu_invltlb();
334 /* NOTREACHED */
335 while(1);
336}
337
338int
339grow_stack(struct proc *p, u_long sp)
340{
341 int rv;
342
343 rv = vm_map_growstack (p, sp);
344 if (rv != KERN_SUCCESS)
345 return (0);
346
347 return (1);
348}
349
350/*
351 * Tell whether this address is in some physical memory region.
352 * Currently used by the kernel coredump code in order to avoid
353 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
354 * or other unpredictable behaviour.
355 */
356
357int
358is_physical_memory(vm_offset_t addr)
359{
360#if NISA > 0
361 /* The ISA ``memory hole''. */
362 if (addr >= 0xa0000 && addr < 0x100000)
363 return 0;
364#endif
365 /*
366 * stuff other tests for known memory-mapped devices (PCI?)
367 * here
368 */
369
370 return 1;
371}
372
373/*
374 * platform-specific vmspace initialization (nothing for amd64)
375 */
376void
377cpu_vmspace_alloc(struct vmspace *vm __unused)
378{
379}
380
381void
382cpu_vmspace_free(struct vmspace *vm __unused)
383{
d7f50089
YY
384}
385
386int
387kvm_access_check(vm_offset_t saddr, vm_offset_t eaddr, int prot)
388{
0855a2af
JG
389 vm_offset_t addr;
390
391 if (saddr < KvaStart)
392 return EFAULT;
393 if (eaddr >= KvaEnd)
394 return EFAULT;
395 for (addr = saddr; addr < eaddr; addr += PAGE_SIZE) {
396 if (pmap_extract(&kernel_pmap, addr) == 0)
397 return EFAULT;
398 }
399 if (!kernacc((caddr_t)saddr, eaddr - saddr, prot))
400 return EFAULT;
d7f50089
YY
401 return 0;
402}
403