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
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
36 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
37 * from: src/sys/i386/i386/vm_machdep.c,v 1.132.2.2 2000/08/26 04:19:26 yokota
38 * JNPR: vm_machdep.c,v 1.8.2.2 2007/08/16 15:59:17 girish
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
49 #include <sys/vnode.h>
50 #include <sys/vmmeter.h>
51 #include <sys/kernel.h>
52 #include <sys/sysctl.h>
53 #include <sys/unistd.h>
55 #include <machine/clock.h>
56 #include <machine/cpu.h>
57 #include <machine/md_var.h>
58 #include <machine/pcb.h>
59 #include <machine/pltfm.h>
62 #include <vm/vm_param.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_extern.h>
71 #include <sys/sf_buf.h>
74 #define NSFBUFS (512 + maxusers * 16)
77 static void sf_buf_init(void *arg);
78 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL);
80 LIST_HEAD(sf_head, sf_buf);
84 * A hash table of active sendfile(2) buffers
86 static struct sf_head *sf_buf_active;
87 static u_long sf_buf_hashmask;
89 #define SF_BUF_HASH(m) (((m) - vm_page_array) & sf_buf_hashmask)
91 static TAILQ_HEAD(, sf_buf) sf_buf_freelist;
92 static u_int sf_buf_alloc_want;
95 * A lock used to synchronize access to the hash table and free list
97 static struct mtx sf_buf_lock;
100 * Finish a fork operation, with process p2 nearly set up.
101 * Copy and update the pcb, set up the stack so that the child
102 * ready to run and return to user mode.
105 cpu_fork(register struct thread *td1,register struct proc *p2,
106 struct thread *td2,int flags)
108 register struct proc *p1;
112 if ((flags & RFPROC) == 0)
114 /* It is assumed that the vm_thread_alloc called
115 * cpu_thread_alloc() before cpu_fork is called.
118 /* Point the pcb to the top of the stack */
121 /* Copy p1's pcb, note that in this case
122 * our pcb also includes the td_frame being copied
123 * too. The older mips2 code did an additional copy
124 * of the td_frame, for us thats not needed any
125 * longer (this copy does them both
127 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
129 /* Point mdproc and then copy over td1's contents
130 * md_proc is empty for MIPS
132 td2->td_md.md_flags = td1->td_md.md_flags & MDTD_FPUSED;
135 * Set up return-value registers as fork() libc stub expects.
137 td2->td_frame->v0 = 0;
138 td2->td_frame->v1 = 1;
139 td2->td_frame->a3 = 0;
141 if (td1 == PCPU_GET(fpcurthread))
142 MipsSaveCurFPState(td1);
144 pcb2->pcb_context.val[PCB_REG_RA] = (register_t)fork_trampoline;
145 /* Make sp 64-bit aligned */
146 pcb2->pcb_context.val[PCB_REG_SP] = (register_t)(((vm_offset_t)td2->td_pcb &
147 ~(sizeof(__int64_t) - 1)) - STAND_FRAME_SIZE);
148 pcb2->pcb_context.val[PCB_REG_S0] = (register_t)fork_return;
149 pcb2->pcb_context.val[PCB_REG_S1] = (register_t)td2;
150 pcb2->pcb_context.val[PCB_REG_S2] = (register_t)td2->td_frame;
151 pcb2->pcb_context.val[PCB_REG_SR] = SR_INT_MASK;
153 * FREEBSD_DEVELOPERS_FIXME:
154 * Setup any other CPU-Specific registers (Not MIPS Standard)
155 * and/or bits in other standard MIPS registers (if CPU-Specific)
159 td2->td_md.md_saved_intr = MIPS_SR_INT_IE;
160 td2->td_md.md_spinlock_count = 1;
162 pcb2->pcb_context.val[PCB_REG_SR] |= MIPS_SR_COP_2_BIT | MIPS32_SR_PX | MIPS_SR_UX | MIPS_SR_KX | MIPS_SR_SX;
168 * Intercept the return address from a freshly forked process that has NOT
169 * been scheduled yet.
171 * This is needed to make kernel threads stay in kernel mode.
174 cpu_set_fork_handler(struct thread *td, void (*func) __P((void *)), void *arg)
177 * Note that the trap frame follows the args, so the function
178 * is really called like this: func(arg, frame);
180 td->td_pcb->pcb_context.val[PCB_REG_S0] = (register_t) func;
181 td->td_pcb->pcb_context.val[PCB_REG_S1] = (register_t) arg;
185 cpu_exit(struct thread *td)
190 cpu_thread_exit(struct thread *td)
193 if (PCPU_GET(fpcurthread) == td)
194 PCPU_GET(fpcurthread) = (struct thread *)0;
198 cpu_thread_free(struct thread *td)
203 cpu_thread_clean(struct thread *td)
208 cpu_thread_swapin(struct thread *td)
214 * The kstack may be at a different physical address now.
215 * Cache the PTEs for the Kernel stack in the machine dependent
216 * part of the thread struct so cpu_switch() can quickly map in
217 * the pcb struct and kernel stack.
219 if (!(pte = pmap_segmap(kernel_pmap, td->td_md.md_realstack)))
220 panic("cpu_thread_swapin: invalid segmap");
221 pte += ((vm_offset_t)td->td_md.md_realstack >> PGSHIFT) & (NPTEPG - 1);
223 for (i = 0; i < KSTACK_PAGES - 1; i++) {
224 td->td_md.md_upte[i] = *pte & ~(PTE_RO|PTE_WIRED);
230 cpu_thread_swapout(struct thread *td)
235 cpu_thread_alloc(struct thread *td)
240 if(td->td_kstack & (1 << PAGE_SHIFT))
241 td->td_md.md_realstack = td->td_kstack + PAGE_SIZE;
243 td->td_md.md_realstack = td->td_kstack;
245 td->td_pcb = (struct pcb *)(td->td_md.md_realstack +
246 (td->td_kstack_pages - 1) * PAGE_SIZE) - 1;
247 td->td_frame = &td->td_pcb->pcb_regs;
249 if (!(pte = pmap_segmap(kernel_pmap, td->td_md.md_realstack)))
250 panic("cpu_thread_alloc: invalid segmap");
251 pte += ((vm_offset_t)td->td_md.md_realstack >> PGSHIFT) & (NPTEPG - 1);
253 for (i = 0; i < KSTACK_PAGES - 1; i++) {
254 td->td_md.md_upte[i] = *pte & ~(PTE_RO|PTE_WIRED);
260 * Initialize machine state (pcb and trap frame) for a new thread about to
261 * upcall. Put enough state in the new thread's PCB to get it to go back
262 * userret(), where we can intercept it again to set the return (upcall)
263 * Address and stack, along with those from upcals that are from other sources
264 * such as those generated in thread_userret() itself.
267 cpu_set_upcall(struct thread *td, struct thread *td0)
271 /* Point the pcb to the top of the stack. */
275 * Copy the upcall pcb. This loads kernel regs.
276 * Those not loaded individually below get their default
279 * XXXKSE It might be a good idea to simply skip this as
280 * the values of the other registers may be unimportant.
281 * This would remove any requirement for knowing the KSE
282 * at this time (see the matching comment below for
283 * more analysis) (need a good safe default).
284 * In MIPS, the trapframe is the first element of the PCB
285 * and gets copied when we copy the PCB. No seperate copy
288 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
291 * Set registers for trampoline to user mode.
294 pcb2->pcb_context.val[PCB_REG_RA] = (register_t)fork_trampoline;
295 /* Make sp 64-bit aligned */
296 pcb2->pcb_context.val[PCB_REG_SP] = (register_t)(((vm_offset_t)td->td_pcb &
297 ~(sizeof(__int64_t) - 1)) - STAND_FRAME_SIZE);
298 pcb2->pcb_context.val[PCB_REG_S0] = (register_t)fork_return;
299 pcb2->pcb_context.val[PCB_REG_S1] = (register_t)td;
300 pcb2->pcb_context.val[PCB_REG_S2] = (register_t)td->td_frame;
303 /* Dont set IE bit in SR. sched lock release will take care of it */
304 /* idle_mask is jmips pcb2->pcb_context.val[11] = (ALL_INT_MASK & idle_mask); */
305 pcb2->pcb_context.val[PCB_REG_SR] = SR_INT_MASK;
307 pcb2->pcb_context.val[PCB_REG_SR] |= MIPS_SR_COP_2_BIT | MIPS_SR_COP_0_BIT |
308 MIPS32_SR_PX | MIPS_SR_UX | MIPS_SR_KX | MIPS_SR_SX;
312 * FREEBSD_DEVELOPERS_FIXME:
313 * Setup any other CPU-Specific registers (Not MIPS Standard)
317 /* SMP Setup to release sched_lock in fork_exit(). */
318 td->td_md.md_spinlock_count = 1;
319 td->td_md.md_saved_intr = MIPS_SR_INT_IE;
321 /* Maybe we need to fix this? */
322 td->td_md.md_saved_sr = ( (MIPS_SR_COP_2_BIT | MIPS_SR_COP_0_BIT) |
323 (MIPS32_SR_PX | MIPS_SR_UX | MIPS_SR_KX | MIPS_SR_SX) |
324 (MIPS_SR_INT_IE | MIPS_HARD_INT_MASK));
329 * Set that machine state for performing an upcall that has to
330 * be done in thread_userret() so that those upcalls generated
331 * in thread_userret() itself can be done as well.
334 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
337 struct trapframe *tf;
341 * At the point where a function is called, sp must be 8
342 * byte aligned[for compatibility with 64-bit CPUs]
343 * in ``See MIPS Run'' by D. Sweetman, p. 269
345 sp = ((uint32_t)(stack->ss_sp + stack->ss_size) & ~0x7) -
349 * Set the trap frame to point at the beginning of the uts
353 bzero(tf, sizeof(struct trapframe));
354 tf->sp = (register_t)sp;
355 tf->pc = (register_t)entry;
356 tf->a0 = (register_t)arg;
358 tf->sr = SR_KSU_USER | SR_EXL;
360 tf->sr |= MIPS_SR_INT_IE | MIPS_SR_COP_0_BIT | MIPS_SR_UX |
363 /* tf->sr |= (ALL_INT_MASK & idle_mask) | SR_INT_ENAB; */
364 /**XXX the above may now be wrong -- mips2 implements this as panic */
366 * FREEBSD_DEVELOPERS_FIXME:
367 * Setup any other CPU-Specific registers (Not MIPS Standard)
372 * Convert kernel VA to physical address
379 va = pmap_kextract((vm_offset_t)addr);
381 panic("kvtop: zero page frame");
386 * Implement the pre-zeroed page mechanism.
387 * This routine is called from the idle loop.
390 #define ZIDLE_LO(v) ((v) * 2 / 3)
391 #define ZIDLE_HI(v) ((v) * 4 / 5)
394 * Tell whether this address is in some physical memory region.
395 * Currently used by the kernel coredump code in order to avoid
396 * dumping non-memory physical address space.
399 is_physical_memory(vm_offset_t addr)
401 if (addr >= SDRAM_ADDR_START && addr <= SDRAM_ADDR_END)
408 is_cacheable_mem(vm_offset_t pa)
410 if ((pa >= SDRAM_ADDR_START && pa <= SDRAM_ADDR_END) ||
411 #ifdef FLASH_ADDR_START
412 (pa >= FLASH_ADDR_START && pa <= FLASH_ADDR_END))
422 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
425 sf_buf_init(void *arg)
427 struct sf_buf *sf_bufs;
432 TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
434 sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask);
435 TAILQ_INIT(&sf_buf_freelist);
436 sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
437 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
439 for (i = 0; i < nsfbufs; i++) {
440 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
441 TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry);
443 sf_buf_alloc_want = 0;
444 mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF);
448 * Allocate an sf_buf for the given vm_page. On this machine, however, there
449 * is no sf_buf object. Instead, an opaque pointer to the given vm_page is
453 sf_buf_alloc(struct vm_page *m, int flags)
455 struct sf_head *hash_list;
459 hash_list = &sf_buf_active[SF_BUF_HASH(m)];
460 mtx_lock(&sf_buf_lock);
461 LIST_FOREACH(sf, hash_list, list_entry) {
464 if (sf->ref_count == 1) {
465 TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
467 nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
472 while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) {
473 if (flags & SFB_NOWAIT)
476 mbstat.sf_allocwait++;
477 error = msleep(&sf_buf_freelist, &sf_buf_lock,
478 (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0);
482 * If we got a signal, don't risk going back to sleep.
487 TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
489 LIST_REMOVE(sf, list_entry);
490 LIST_INSERT_HEAD(hash_list, sf, list_entry);
494 nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
495 pmap_qenter(sf->kva, &sf->m, 1);
497 mtx_unlock(&sf_buf_lock);
502 * Free the sf_buf. In fact, do nothing because there are no resources
503 * associated with the sf_buf.
506 sf_buf_free(struct sf_buf *sf)
508 mtx_lock(&sf_buf_lock);
510 if (sf->ref_count == 0) {
511 TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry);
513 if (sf_buf_alloc_want > 0)
514 wakeup_one(&sf_buf_freelist);
516 mtx_unlock(&sf_buf_lock);
520 * Software interrupt handler for queued VM system processing.
528 cpu_set_user_tls(struct thread *td, void *tls_base)
536 cpu_throw(struct thread *old, struct thread *new)
539 func_2args_asmmacro(&mips_cpu_throw, old, new);
540 panic("mips_cpu_throw() returned");