format cleanup for readability. Tab out back-slashes.
[dragonfly.git] / sys / i386 / i386 / vm_machdep.c
CommitLineData
984263bc
MD
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
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department, and William Jolitz.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
40 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
41 * $FreeBSD: src/sys/i386/i386/vm_machdep.c,v 1.132.2.9 2003/01/25 19:02:23 dillon Exp $
85100692 42 * $DragonFly: src/sys/i386/i386/Attic/vm_machdep.c,v 1.13 2003/06/28 04:16:02 dillon Exp $
984263bc
MD
43 */
44
45#include "npx.h"
46#include "opt_user_ldt.h"
47#ifdef PC98
48#include "opt_pc98.h"
49#endif
50#include "opt_reset.h"
51
52#include <sys/param.h>
53#include <sys/systm.h>
54#include <sys/malloc.h>
55#include <sys/proc.h>
56#include <sys/buf.h>
57#include <sys/vnode.h>
58#include <sys/vmmeter.h>
59#include <sys/kernel.h>
60#include <sys/sysctl.h>
61#include <sys/unistd.h>
62
63#include <machine/clock.h>
64#include <machine/cpu.h>
65#include <machine/md_var.h>
66#ifdef SMP
67#include <machine/smp.h>
68#endif
69#include <machine/pcb.h>
70#include <machine/pcb_ext.h>
71#include <machine/vm86.h>
85100692 72#include <machine/globaldata.h> /* npxthread */
984263bc
MD
73
74#include <vm/vm.h>
75#include <vm/vm_param.h>
76#include <sys/lock.h>
77#include <vm/vm_kern.h>
78#include <vm/vm_page.h>
79#include <vm/vm_map.h>
80#include <vm/vm_extern.h>
81
82#include <sys/user.h>
ae8050a4 83#include <sys/thread2.h>
984263bc
MD
84
85#ifdef PC98
86#include <pc98/pc98/pc98.h>
87#else
88#include <i386/isa/isa.h>
89#endif
90
91static void cpu_reset_real __P((void));
92#ifdef SMP
93static void cpu_reset_proxy __P((void));
94static u_int cpu_reset_proxyid;
95static volatile u_int cpu_reset_proxy_active;
96#endif
97extern int _ucodesel, _udatasel;
98
99/*
100 * quick version of vm_fault
101 */
102int
103vm_fault_quick(v, prot)
104 caddr_t v;
105 int prot;
106{
107 int r;
108
109 if (prot & VM_PROT_WRITE)
110 r = subyte(v, fubyte(v));
111 else
112 r = fubyte(v);
113 return(r);
114}
115
116/*
117 * Finish a fork operation, with process p2 nearly set up.
118 * Copy and update the pcb, set up the stack so that the child
119 * ready to run and return to user mode.
120 */
121void
122cpu_fork(p1, p2, flags)
123 register struct proc *p1, *p2;
124 int flags;
125{
126 struct pcb *pcb2;
127
128 if ((flags & RFPROC) == 0) {
129#ifdef USER_LDT
130 if ((flags & RFMEM) == 0) {
131 /* unshare user LDT */
b7c628e4 132 struct pcb *pcb1 = p1->p_thread->td_pcb;
984263bc
MD
133 struct pcb_ldt *pcb_ldt = pcb1->pcb_ldt;
134 if (pcb_ldt && pcb_ldt->ldt_refcnt > 1) {
135 pcb_ldt = user_ldt_alloc(pcb1,pcb_ldt->ldt_len);
136 user_ldt_free(pcb1);
137 pcb1->pcb_ldt = pcb_ldt;
138 set_user_ldt(pcb1);
139 }
140 }
141#endif
142 return;
143 }
144
145#if NNPX > 0
146 /* Ensure that p1's pcb is up to date. */
263e4574 147 if (npxthread == p1->p_thread)
b7c628e4 148 npxsave(&p1->p_thread->td_pcb->pcb_save);
984263bc
MD
149#endif
150
151 /* Copy p1's pcb. */
b7c628e4 152 pcb2 = p2->p_thread->td_pcb;
7d0bac62 153 *pcb2 = *p1->p_thread->td_pcb;
984263bc
MD
154
155 /*
156 * Create a new fresh stack for the new process.
157 * Copy the trap frame for the return to user mode as if from a
b7c628e4
MD
158 * syscall. This copies the user mode register values. The
159 * 16 byte offset saves space for vm86, and must match
160 * common_tss.esp0 (kernel stack pointer on entry from user mode)
8ad65e08
MD
161 *
162 * pcb_esp must allocate an additional call-return pointer below
163 * the trap frame which will be restored by cpu_restore, and the
164 * thread's td_sp pointer must allocate an additonal call-return
165 * pointer below the pcb_esp call-return pointer to hold the LWKT
166 * restore function pointer.
167 *
168 * The LWKT restore function pointer must be set to cpu_restore,
169 * which is our standard heavy weight process switch-in function.
170 * YYY eventually we should shortcut fork_return and fork_trampoline
171 * to use the LWKT restore function directly so we can get rid of
172 * all the extra crap we are setting up.
984263bc 173 */
7d0bac62 174 p2->p_md.md_regs = (struct trapframe *)((char *)pcb2 - 16) - 1;
984263bc
MD
175 bcopy(p1->p_md.md_regs, p2->p_md.md_regs, sizeof(*p2->p_md.md_regs));
176
177 /*
178 * Set registers for trampoline to user mode. Leave space for the
179 * return address on stack. These are the kernel mode register values.
180 */
181 pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdir);
182 pcb2->pcb_edi = 0;
183 pcb2->pcb_esi = (int)fork_return; /* fork_trampoline argument */
184 pcb2->pcb_ebp = 0;
185 pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *);
186 pcb2->pcb_ebx = (int)p2; /* fork_trampoline argument */
187 pcb2->pcb_eip = (int)fork_trampoline;
8ad65e08
MD
188 p2->p_thread->td_sp = (char *)(pcb2->pcb_esp - sizeof(void *));
189 *(void **)p2->p_thread->td_sp = (void *)cpu_heavy_restore;
984263bc
MD
190 /*
191 * pcb2->pcb_ldt: duplicated below, if necessary.
192 * pcb2->pcb_savefpu: cloned above.
193 * pcb2->pcb_flags: cloned above (always 0 here?).
194 * pcb2->pcb_onfault: cloned above (always NULL here?).
195 */
196
197#ifdef SMP
198 pcb2->pcb_mpnest = 1;
199#endif
200 /*
201 * XXX don't copy the i/o pages. this should probably be fixed.
202 */
203 pcb2->pcb_ext = 0;
204
205#ifdef USER_LDT
206 /* Copy the LDT, if necessary. */
207 if (pcb2->pcb_ldt != 0) {
208 if (flags & RFMEM) {
209 pcb2->pcb_ldt->ldt_refcnt++;
210 } else {
211 pcb2->pcb_ldt = user_ldt_alloc(pcb2,
212 pcb2->pcb_ldt->ldt_len);
213 }
214 }
215#endif
216
217 /*
218 * Now, cpu_switch() can schedule the new process.
219 * pcb_esp is loaded pointing to the cpu_switch() stack frame
220 * containing the return address when exiting cpu_switch.
221 * This will normally be to fork_trampoline(), which will have
222 * %ebx loaded with the new proc's pointer. fork_trampoline()
223 * will set up a stack to call fork_return(p, frame); to complete
224 * the return to user-mode.
225 */
226}
227
228/*
229 * Intercept the return address from a freshly forked process that has NOT
230 * been scheduled yet.
231 *
232 * This is needed to make kernel threads stay in kernel mode.
233 */
234void
235cpu_set_fork_handler(p, func, arg)
236 struct proc *p;
237 void (*func) __P((void *));
238 void *arg;
239{
240 /*
241 * Note that the trap frame follows the args, so the function
242 * is really called like this: func(arg, frame);
243 */
b7c628e4
MD
244 p->p_thread->td_pcb->pcb_esi = (int) func; /* function */
245 p->p_thread->td_pcb->pcb_ebx = (int) arg; /* first arg */
984263bc
MD
246}
247
0cfcada1
MD
248void
249cpu_set_thread_handler(thread_t td, void (*rfunc)(void), void *func, void *arg)
250{
251 td->td_pcb->pcb_esi = (int)func;
252 td->td_pcb->pcb_ebx = (int) arg;
253 td->td_switch = cpu_lwkt_switch;
254 td->td_sp -= sizeof(void *);
255 *(void **)td->td_sp = rfunc; /* exit function on return */
256 td->td_sp -= sizeof(void *);
257 *(void **)td->td_sp = cpu_kthread_restore;
258}
259
984263bc 260void
99df837e 261cpu_proc_exit(void)
984263bc 262{
99df837e 263 struct thread *td = curthread;
ae8050a4 264 struct pcb *pcb;
8ad65e08 265
99df837e 266
984263bc 267#if NNPX > 0
99df837e
MD
268 KKASSERT(td->td_proc);
269 npxexit(td->td_proc);
984263bc 270#endif /* NNPX */
ae8050a4
MD
271
272 /*
273 * Cleanup the PCB
274 */
99df837e 275 pcb = td->td_pcb;
984263bc
MD
276 if (pcb->pcb_ext != 0) {
277 /*
278 * XXX do we need to move the TSS off the allocated pages
279 * before freeing them? (not done here)
280 */
281 kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
282 ctob(IOPAGES + 1));
283 pcb->pcb_ext = 0;
284 }
285#ifdef USER_LDT
286 user_ldt_free(pcb);
287#endif
288 if (pcb->pcb_flags & PCB_DBREGS) {
289 /*
290 * disable all hardware breakpoints
291 */
292 reset_dbregs();
293 pcb->pcb_flags &= ~PCB_DBREGS;
294 }
295 cnt.v_swtch++;
ae8050a4 296
ae8050a4 297 crit_enter();
ae8050a4 298 lwkt_deschedule_self();
99df837e
MD
299 cpu_thread_exit();
300}
301
302/*
303 * Terminate the current thread. The caller must have already acquired
304 * the thread's rwlock and placed it on a reap list or otherwise notified
305 * a reaper of its existance. We set a special assembly switch function which
306 * releases td_rwlock after it has cleaned up the MMU state and switched
307 * out the stack.
308 *
309 * Must be caller from a critical section and with the thread descheduled.
310 */
311void
312cpu_thread_exit(void)
313{
314 curthread->td_switch = cpu_exit_switch;
8ad65e08 315 lwkt_switch();
984263bc
MD
316 panic("cpu_exit");
317}
318
99df837e
MD
319/*
320 * Process Reaper. Called after the caller has acquired the thread's
321 * rwlock and removed it from the reap list.
322 */
984263bc 323void
99df837e 324cpu_proc_wait(struct proc *p)
984263bc 325{
7e1d4bf4
MD
326 struct thread *td;
327
984263bc 328 /* drop per-process resources */
7e1d4bf4
MD
329 td = pmap_dispose_proc(p);
330 if (td)
99df837e 331 lwkt_free_thread(td);
984263bc
MD
332}
333
334/*
335 * Dump the machine specific header information at the start of a core dump.
336 */
337int
dadab5e9 338cpu_coredump(struct thread *td, struct vnode *vp, struct ucred *cred)
984263bc 339{
dadab5e9 340 struct proc *p = td->td_proc;
984263bc
MD
341 int error;
342 caddr_t tempuser;
343
dadab5e9 344 KKASSERT(p);
984263bc
MD
345 tempuser = malloc(ctob(UPAGES), M_TEMP, M_WAITOK);
346 if (!tempuser)
347 return EINVAL;
348
349 bzero(tempuser, ctob(UPAGES));
350 bcopy(p->p_addr, tempuser, sizeof(struct user));
351 bcopy(p->p_md.md_regs,
352 tempuser + ((caddr_t) p->p_md.md_regs - (caddr_t) p->p_addr),
353 sizeof(struct trapframe));
b7c628e4 354 bcopy(p->p_thread->td_pcb, tempuser + ((char *)p->p_thread->td_pcb - (char *)p->p_addr), sizeof(struct pcb));
984263bc
MD
355
356 error = vn_rdwr(UIO_WRITE, vp, (caddr_t) tempuser, ctob(UPAGES),
dadab5e9 357 (off_t)0, UIO_SYSSPACE, IO_UNIT, cred, (int *)NULL, td);
984263bc
MD
358
359 free(tempuser, M_TEMP);
360
361 return error;
362}
363
364#ifdef notyet
365static void
366setredzone(pte, vaddr)
367 u_short *pte;
368 caddr_t vaddr;
369{
370/* eventually do this by setting up an expand-down stack segment
371 for ss0: selector, allowing stack access down to top of u.
372 this means though that protection violations need to be handled
373 thru a double fault exception that must do an integral task
374 switch to a known good context, within which a dump can be
375 taken. a sensible scheme might be to save the initial context
376 used by sched (that has physical memory mapped 1:1 at bottom)
377 and take the dump while still in mapped mode */
378}
379#endif
380
381/*
382 * Convert kernel VA to physical address
383 */
384u_long
385kvtop(void *addr)
386{
387 vm_offset_t va;
388
389 va = pmap_kextract((vm_offset_t)addr);
390 if (va == 0)
391 panic("kvtop: zero page frame");
392 return((int)va);
393}
394
395/*
396 * Force reset the processor by invalidating the entire address space!
397 */
398
399#ifdef SMP
400static void
401cpu_reset_proxy()
402{
403 u_int saved_mp_lock;
404
405 cpu_reset_proxy_active = 1;
406 while (cpu_reset_proxy_active == 1)
407 ; /* Wait for other cpu to disable interupts */
408 saved_mp_lock = mp_lock;
409 mp_lock = 1;
410 printf("cpu_reset_proxy: Grabbed mp lock for BSP\n");
411 cpu_reset_proxy_active = 3;
412 while (cpu_reset_proxy_active == 3)
413 ; /* Wait for other cpu to enable interrupts */
414 stop_cpus((1<<cpu_reset_proxyid));
415 printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
416 DELAY(1000000);
417 cpu_reset_real();
418}
419#endif
420
421void
422cpu_reset()
423{
424#ifdef SMP
425 if (smp_active == 0) {
426 cpu_reset_real();
427 /* NOTREACHED */
428 } else {
429
430 u_int map;
431 int cnt;
432 printf("cpu_reset called on cpu#%d\n",cpuid);
433
434 map = other_cpus & ~ stopped_cpus;
435
436 if (map != 0) {
437 printf("cpu_reset: Stopping other CPUs\n");
438 stop_cpus(map); /* Stop all other CPUs */
439 }
440
441 if (cpuid == 0) {
442 DELAY(1000000);
443 cpu_reset_real();
444 /* NOTREACHED */
445 } else {
446 /* We are not BSP (CPU #0) */
447
448 cpu_reset_proxyid = cpuid;
449 cpustop_restartfunc = cpu_reset_proxy;
450 printf("cpu_reset: Restarting BSP\n");
451 started_cpus = (1<<0); /* Restart CPU #0 */
452
453 cnt = 0;
454 while (cpu_reset_proxy_active == 0 && cnt < 10000000)
455 cnt++; /* Wait for BSP to announce restart */
456 if (cpu_reset_proxy_active == 0)
457 printf("cpu_reset: Failed to restart BSP\n");
458 __asm __volatile("cli" : : : "memory");
459 cpu_reset_proxy_active = 2;
460 cnt = 0;
461 while (cpu_reset_proxy_active == 2 && cnt < 10000000)
462 cnt++; /* Do nothing */
463 if (cpu_reset_proxy_active == 2) {
464 printf("cpu_reset: BSP did not grab mp lock\n");
465 cpu_reset_real(); /* XXX: Bogus ? */
466 }
467 cpu_reset_proxy_active = 4;
468 __asm __volatile("sti" : : : "memory");
469 while (1);
470 /* NOTREACHED */
471 }
472 }
473#else
474 cpu_reset_real();
475#endif
476}
477
478static void
479cpu_reset_real()
480{
481
482#ifdef PC98
483 /*
484 * Attempt to do a CPU reset via CPU reset port.
485 */
486 disable_intr();
487 if ((inb(0x35) & 0xa0) != 0xa0) {
488 outb(0x37, 0x0f); /* SHUT0 = 0. */
489 outb(0x37, 0x0b); /* SHUT1 = 0. */
490 }
491 outb(0xf0, 0x00); /* Reset. */
492#else
493 /*
494 * Attempt to do a CPU reset via the keyboard controller,
495 * do not turn of the GateA20, as any machine that fails
496 * to do the reset here would then end up in no man's land.
497 */
498
499#if !defined(BROKEN_KEYBOARD_RESET)
500 outb(IO_KBD + 4, 0xFE);
501 DELAY(500000); /* wait 0.5 sec to see if that did it */
502 printf("Keyboard reset did not work, attempting CPU shutdown\n");
503 DELAY(1000000); /* wait 1 sec for printf to complete */
504#endif
505#endif /* PC98 */
506 /* force a shutdown by unmapping entire address space ! */
507 bzero((caddr_t) PTD, PAGE_SIZE);
508
509 /* "good night, sweet prince .... <THUNK!>" */
510 invltlb();
511 /* NOTREACHED */
512 while(1);
513}
514
515int
516grow_stack(p, sp)
517 struct proc *p;
518 u_int sp;
519{
520 int rv;
521
522 rv = vm_map_growstack (p, sp);
523 if (rv != KERN_SUCCESS)
524 return (0);
525
526 return (1);
527}
528
529SYSCTL_DECL(_vm_stats_misc);
530
531static int cnt_prezero;
532
533SYSCTL_INT(_vm_stats_misc, OID_AUTO,
534 cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, "");
535
536/*
537 * Implement the pre-zeroed page mechanism.
538 * This routine is called from the idle loop.
539 */
540
541#define ZIDLE_LO(v) ((v) * 2 / 3)
542#define ZIDLE_HI(v) ((v) * 4 / 5)
543
544int
545vm_page_zero_idle()
546{
547 static int free_rover;
548 static int zero_state;
549 vm_page_t m;
550 int s;
551
552 /*
553 * Attempt to maintain approximately 1/2 of our free pages in a
554 * PG_ZERO'd state. Add some hysteresis to (attempt to) avoid
555 * generally zeroing a page when the system is near steady-state.
556 * Otherwise we might get 'flutter' during disk I/O / IPC or
557 * fast sleeps. We also do not want to be continuously zeroing
558 * pages because doing so may flush our L1 and L2 caches too much.
559 */
560
561 if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count))
562 return(0);
563 if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
564 return(0);
565
566#ifdef SMP
567 if (try_mplock()) {
568#endif
569 s = splvm();
570 __asm __volatile("sti" : : : "memory");
571 zero_state = 0;
572 m = vm_page_list_find(PQ_FREE, free_rover, FALSE);
573 if (m != NULL && (m->flags & PG_ZERO) == 0) {
574 vm_page_queues[m->queue].lcnt--;
575 TAILQ_REMOVE(&vm_page_queues[m->queue].pl, m, pageq);
576 m->queue = PQ_NONE;
577 splx(s);
578 pmap_zero_page(VM_PAGE_TO_PHYS(m));
579 (void)splvm();
580 vm_page_flag_set(m, PG_ZERO);
581 m->queue = PQ_FREE + m->pc;
582 vm_page_queues[m->queue].lcnt++;
583 TAILQ_INSERT_TAIL(&vm_page_queues[m->queue].pl, m,
584 pageq);
585 ++vm_page_zero_count;
586 ++cnt_prezero;
587 if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
588 zero_state = 1;
589 }
590 free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK;
591 splx(s);
592 __asm __volatile("cli" : : : "memory");
593#ifdef SMP
594 rel_mplock();
595#endif
596 return (1);
597#ifdef SMP
598 }
599#endif
600 /*
601 * We have to enable interrupts for a moment if the try_mplock fails
602 * in order to potentially take an IPI. XXX this should be in
603 * swtch.s
604 */
605 __asm __volatile("sti; nop; cli" : : : "memory");
606 return (0);
607}
608
609/*
610 * Software interrupt handler for queued VM system processing.
611 */
612void
613swi_vm()
614{
615 if (busdma_swi_pending != 0)
616 busdma_swi();
617}
618
619/*
620 * Tell whether this address is in some physical memory region.
621 * Currently used by the kernel coredump code in order to avoid
622 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
623 * or other unpredictable behaviour.
624 */
625
626#include "isa.h"
627
628int
629is_physical_memory(addr)
630 vm_offset_t addr;
631{
632
633#if NISA > 0
634 /* The ISA ``memory hole''. */
635 if (addr >= 0xa0000 && addr < 0x100000)
636 return 0;
637#endif
638
639 /*
640 * stuff other tests for known memory-mapped devices (PCI?)
641 * here
642 */
643
644 return 1;
645}