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 * 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.
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
39 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
40 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
52 #include <sys/socketvar.h>
53 #include <sys/sf_buf.h>
54 #include <sys/unistd.h>
55 #include <machine/cpu.h>
56 #include <machine/pcb.h>
57 #include <machine/sysarch.h>
59 #include <sys/mutex.h>
63 #include <vm/vm_extern.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_param.h>
68 #include <vm/vm_pageout.h>
70 #include <vm/uma_int.h>
72 #include <machine/md_var.h>
75 #define NSFBUFS (512 + maxusers * 16)
78 #ifndef ARM_USE_SMALL_ALLOC
79 static void sf_buf_init(void *arg);
80 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL);
82 LIST_HEAD(sf_head, sf_buf);
86 * A hash table of active sendfile(2) buffers
88 static struct sf_head *sf_buf_active;
89 static u_long sf_buf_hashmask;
91 #define SF_BUF_HASH(m) (((m) - vm_page_array) & sf_buf_hashmask)
93 static TAILQ_HEAD(, sf_buf) sf_buf_freelist;
94 static u_int sf_buf_alloc_want;
97 * A lock used to synchronize access to the hash table and free list
99 static struct mtx sf_buf_lock;
103 * Finish a fork operation, with process p2 nearly set up.
104 * Copy and update the pcb, set up the stack so that the child
105 * ready to run and return to user mode.
108 cpu_fork(register struct thread *td1, register struct proc *p2,
109 struct thread *td2, int flags)
111 struct pcb *pcb1, *pcb2;
112 struct trapframe *tf;
113 struct switchframe *sf;
116 if ((flags & RFPROC) == 0)
119 pcb2 = (struct pcb *)(td2->td_kstack + td2->td_kstack_pages * PAGE_SIZE) - 1;
121 #ifndef CPU_XSCALE_CORE3
122 pmap_use_minicache(td2->td_kstack, td2->td_kstack_pages * PAGE_SIZE);
123 if (td2->td_altkstack)
124 pmap_use_minicache(td2->td_altkstack, td2->td_altkstack_pages *
129 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
131 bcopy(&td1->td_proc->p_md, mdp2, sizeof(*mdp2));
132 pcb2->un_32.pcb32_und_sp = td2->td_kstack + USPACE_UNDEF_STACK_TOP;
133 pcb2->un_32.pcb32_sp = td2->td_kstack +
134 USPACE_SVC_STACK_TOP - sizeof(*pcb2);
137 (struct trapframe *)pcb2->un_32.pcb32_sp - 1;
138 *tf = *td1->td_frame;
139 sf = (struct switchframe *)tf - 1;
140 sf->sf_r4 = (u_int)fork_return;
141 sf->sf_r5 = (u_int)td2;
142 sf->sf_pc = (u_int)fork_trampoline;
143 tf->tf_spsr &= ~PSR_C_bit;
146 pcb2->un_32.pcb32_sp = (u_int)sf;
148 /* Setup to release spin count in fork_exit(). */
149 td2->td_md.md_spinlock_count = 1;
150 td2->td_md.md_saved_cspr = 0;
151 td2->td_md.md_tp = *(uint32_t **)ARM_TP_ADDRESS;
155 cpu_thread_swapin(struct thread *td)
160 cpu_thread_swapout(struct thread *td)
165 * Detatch mapped page and release resources back to the system.
168 sf_buf_free(struct sf_buf *sf)
170 #ifndef ARM_USE_SMALL_ALLOC
171 mtx_lock(&sf_buf_lock);
173 if (sf->ref_count == 0) {
174 TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry);
176 if (sf_buf_alloc_want > 0)
177 wakeup_one(&sf_buf_freelist);
179 mtx_unlock(&sf_buf_lock);
183 #ifndef ARM_USE_SMALL_ALLOC
185 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
188 sf_buf_init(void *arg)
190 struct sf_buf *sf_bufs;
195 TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
197 sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask);
198 TAILQ_INIT(&sf_buf_freelist);
199 sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
200 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
202 for (i = 0; i < nsfbufs; i++) {
203 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
204 TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry);
206 sf_buf_alloc_want = 0;
207 mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF);
212 * Get an sf_buf from the freelist. Will block if none are available.
215 sf_buf_alloc(struct vm_page *m, int flags)
217 #ifdef ARM_USE_SMALL_ALLOC
218 return ((struct sf_buf *)m);
220 struct sf_head *hash_list;
224 hash_list = &sf_buf_active[SF_BUF_HASH(m)];
225 mtx_lock(&sf_buf_lock);
226 LIST_FOREACH(sf, hash_list, list_entry) {
229 if (sf->ref_count == 1) {
230 TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
232 nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
237 while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) {
238 if (flags & SFB_NOWAIT)
241 mbstat.sf_allocwait++;
242 error = msleep(&sf_buf_freelist, &sf_buf_lock,
243 (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0);
248 * If we got a signal, don't risk going back to sleep.
253 TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
255 LIST_REMOVE(sf, list_entry);
256 LIST_INSERT_HEAD(hash_list, sf, list_entry);
260 nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
261 pmap_kenter(sf->kva, VM_PAGE_TO_PHYS(sf->m));
263 mtx_unlock(&sf_buf_lock);
269 * Initialize machine state (pcb and trap frame) for a new thread about to
270 * upcall. Put enough state in the new thread's PCB to get it to go back
271 * userret(), where we can intercept it again to set the return (upcall)
272 * Address and stack, along with those from upcals that are from other sources
273 * such as those generated in thread_userret() itself.
276 cpu_set_upcall(struct thread *td, struct thread *td0)
278 struct trapframe *tf;
279 struct switchframe *sf;
281 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
282 bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
284 sf = (struct switchframe *)tf - 1;
285 sf->sf_r4 = (u_int)fork_return;
286 sf->sf_r5 = (u_int)td;
287 sf->sf_pc = (u_int)fork_trampoline;
288 tf->tf_spsr &= ~PSR_C_bit;
290 td->td_pcb->un_32.pcb32_sp = (u_int)sf;
291 td->td_pcb->un_32.pcb32_und_sp = td->td_kstack + USPACE_UNDEF_STACK_TOP;
293 /* Setup to release spin count in fork_exit(). */
294 td->td_md.md_spinlock_count = 1;
295 td->td_md.md_saved_cspr = 0;
299 * Set that machine state for performing an upcall that has to
300 * be done in thread_userret() so that those upcalls generated
301 * in thread_userret() itself can be done as well.
304 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
307 struct trapframe *tf = td->td_frame;
309 tf->tf_usr_sp = ((int)stack->ss_sp + stack->ss_size
310 - sizeof(struct trapframe)) & ~7;
311 tf->tf_pc = (int)entry;
312 tf->tf_r0 = (int)arg;
313 tf->tf_spsr = PSR_USR32_MODE;
317 cpu_set_user_tls(struct thread *td, void *tls_base)
321 td->td_md.md_tp = tls_base;
324 *(void **)ARM_TP_ADDRESS = tls_base;
331 cpu_thread_exit(struct thread *td)
336 cpu_thread_alloc(struct thread *td)
338 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_pages *
340 td->td_frame = (struct trapframe *)
341 ((u_int)td->td_kstack + USPACE_SVC_STACK_TOP - sizeof(struct pcb)) - 1;
343 #ifndef CPU_XSCALE_CORE3
344 pmap_use_minicache(td->td_kstack, td->td_kstack_pages * PAGE_SIZE);
350 cpu_thread_free(struct thread *td)
355 cpu_thread_clean(struct thread *td)
360 * Intercept the return address from a freshly forked process that has NOT
361 * been scheduled yet.
363 * This is needed to make kernel threads stay in kernel mode.
366 cpu_set_fork_handler(struct thread *td, void (*func)(void *), void *arg)
368 struct switchframe *sf;
369 struct trapframe *tf;
372 sf = (struct switchframe *)tf - 1;
373 sf->sf_r4 = (u_int)func;
374 sf->sf_r5 = (u_int)arg;
375 td->td_pcb->un_32.pcb32_sp = (u_int)sf;
379 * Software interrupt handler for queued VM system processing.
385 if (busdma_swi_pending)
390 cpu_exit(struct thread *td)
394 #define BITS_PER_INT (8 * sizeof(int))
395 vm_offset_t arm_nocache_startaddr;
396 static int arm_nocache_allocated[ARM_NOCACHE_KVA_SIZE / (PAGE_SIZE *
400 * Functions to map and unmap memory non-cached into KVA the kernel won't try
401 * to allocate. The goal is to provide uncached memory to busdma, to honor
403 * We can allocate at most ARM_NOCACHE_KVA_SIZE bytes.
404 * The allocator is rather dummy, each page is represented by a bit in
405 * a bitfield, 0 meaning the page is not allocated, 1 meaning it is.
406 * As soon as it finds enough contiguous pages to satisfy the request,
407 * it returns the address.
410 arm_remap_nocache(void *addr, vm_size_t size)
414 size = round_page(size);
415 for (i = 0; i < ARM_NOCACHE_KVA_SIZE / PAGE_SIZE; i++) {
416 if (!(arm_nocache_allocated[i / BITS_PER_INT] & (1 << (i %
418 for (j = i; j < i + (size / (PAGE_SIZE)); j++)
419 if (arm_nocache_allocated[j / BITS_PER_INT] &
420 (1 << (j % BITS_PER_INT)))
422 if (j == i + (size / (PAGE_SIZE)))
426 if (i < ARM_NOCACHE_KVA_SIZE / PAGE_SIZE) {
427 vm_offset_t tomap = arm_nocache_startaddr + i * PAGE_SIZE;
428 void *ret = (void *)tomap;
429 vm_paddr_t physaddr = vtophys((vm_offset_t)addr);
431 for (; tomap < (vm_offset_t)ret + size; tomap += PAGE_SIZE,
432 physaddr += PAGE_SIZE, i++) {
433 pmap_kenter_nocache(tomap, physaddr);
434 arm_nocache_allocated[i / BITS_PER_INT] |= 1 << (i %
444 arm_unmap_nocache(void *addr, vm_size_t size)
446 vm_offset_t raddr = (vm_offset_t)addr;
449 size = round_page(size);
450 i = (raddr - arm_nocache_startaddr) / (PAGE_SIZE);
451 for (; size > 0; size -= PAGE_SIZE, i++)
452 arm_nocache_allocated[i / BITS_PER_INT] &= ~(1 << (i %
456 #ifdef ARM_USE_SMALL_ALLOC
458 static TAILQ_HEAD(,arm_small_page) pages_normal =
459 TAILQ_HEAD_INITIALIZER(pages_normal);
460 static TAILQ_HEAD(,arm_small_page) pages_wt =
461 TAILQ_HEAD_INITIALIZER(pages_wt);
462 static TAILQ_HEAD(,arm_small_page) free_pgdesc =
463 TAILQ_HEAD_INITIALIZER(free_pgdesc);
465 extern uma_zone_t l2zone;
467 struct mtx smallalloc_mtx;
469 MALLOC_DEFINE(M_VMSMALLALLOC, "vm_small_alloc", "VM Small alloc data");
471 vm_offset_t alloc_firstaddr;
473 #ifdef ARM_HAVE_SUPERSECTIONS
474 #define S_FRAME L1_SUP_FRAME
475 #define S_SIZE L1_SUP_SIZE
477 #define S_FRAME L1_S_FRAME
478 #define S_SIZE L1_S_SIZE
482 arm_ptovirt(vm_paddr_t pa)
485 vm_offset_t addr = alloc_firstaddr;
487 KASSERT(alloc_firstaddr != 0, ("arm_ptovirt called to early ?"));
488 for (i = 0; dump_avail[i + 1]; i += 2) {
489 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
491 addr += (dump_avail[i + 1] & S_FRAME) + S_SIZE -
492 (dump_avail[i] & S_FRAME);
494 KASSERT(dump_avail[i + 1] != 0, ("Trying to access invalid physical address"));
495 return (addr + (pa - (dump_avail[i] & S_FRAME)));
499 arm_init_smallalloc(void)
501 vm_offset_t to_map = 0, mapaddr;
505 * We need to use dump_avail and not phys_avail, since we want to
506 * map the whole memory and not just the memory available to the VM
507 * to be able to do a pa => va association for any address.
510 for (i = 0; dump_avail[i + 1]; i+= 2) {
511 to_map += (dump_avail[i + 1] & S_FRAME) + S_SIZE -
512 (dump_avail[i] & S_FRAME);
514 alloc_firstaddr = mapaddr = KERNBASE - to_map;
515 for (i = 0; dump_avail[i + 1]; i+= 2) {
516 vm_offset_t size = (dump_avail[i + 1] & S_FRAME) +
517 S_SIZE - (dump_avail[i] & S_FRAME);
520 #ifdef ARM_HAVE_SUPERSECTIONS
521 pmap_kenter_supersection(mapaddr,
522 (dump_avail[i] & L1_SUP_FRAME) + did,
525 pmap_kenter_section(mapaddr,
526 (dump_avail[i] & L1_S_FRAME) + did, SECTION_CACHE);
536 arm_add_smallalloc_pages(void *list, void *mem, int bytes, int pagetable)
538 struct arm_small_page *pg;
542 pg = (struct arm_small_page *)list;
545 TAILQ_INSERT_HEAD(&pages_wt, pg, pg_list);
547 TAILQ_INSERT_HEAD(&pages_normal, pg, pg_list);
548 list = (char *)list + sizeof(*pg);
549 mem = (char *)mem + PAGE_SIZE;
555 uma_small_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
558 struct arm_small_page *sp;
559 TAILQ_HEAD(,arm_small_page) *head;
560 static vm_pindex_t color;
563 *flags = UMA_SLAB_PRIV;
565 * For CPUs where we setup page tables as write back, there's no
566 * need to maintain two separate pools.
568 if (zone == l2zone && pte_l1_s_cache_mode != pte_l1_s_cache_mode_pt)
569 head = (void *)&pages_wt;
571 head = (void *)&pages_normal;
573 mtx_lock(&smallalloc_mtx);
574 sp = TAILQ_FIRST(head);
579 mtx_unlock(&smallalloc_mtx);
580 if (zone == l2zone &&
581 pte_l1_s_cache_mode != pte_l1_s_cache_mode_pt) {
582 *flags = UMA_SLAB_KMEM;
583 ret = ((void *)kmem_malloc(kmem_map, bytes, M_NOWAIT));
586 if ((wait & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT)
587 pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED;
589 pflags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED;
591 pflags |= VM_ALLOC_ZERO;
593 m = vm_page_alloc(NULL, color++,
594 pflags | VM_ALLOC_NOOBJ);
602 ret = (void *)arm_ptovirt(VM_PAGE_TO_PHYS(m));
603 if ((wait & M_ZERO) && (m->flags & PG_ZERO) == 0)
604 bzero(ret, PAGE_SIZE);
607 TAILQ_REMOVE(head, sp, pg_list);
608 TAILQ_INSERT_HEAD(&free_pgdesc, sp, pg_list);
610 mtx_unlock(&smallalloc_mtx);
617 uma_small_free(void *mem, int size, u_int8_t flags)
622 if (flags & UMA_SLAB_KMEM)
623 kmem_free(kmem_map, (vm_offset_t)mem, size);
625 struct arm_small_page *sp;
627 if ((vm_offset_t)mem >= KERNBASE) {
628 mtx_lock(&smallalloc_mtx);
629 sp = TAILQ_FIRST(&free_pgdesc);
630 KASSERT(sp != NULL, ("No more free page descriptor ?"));
631 TAILQ_REMOVE(&free_pgdesc, sp, pg_list);
633 pmap_get_pde_pte(kernel_pmap, (vm_offset_t)mem, &pd,
635 if ((*pd & pte_l1_s_cache_mask) ==
636 pte_l1_s_cache_mode_pt &&
637 pte_l1_s_cache_mode_pt != pte_l1_s_cache_mode)
638 TAILQ_INSERT_HEAD(&pages_wt, sp, pg_list);
640 TAILQ_INSERT_HEAD(&pages_normal, sp, pg_list);
641 mtx_unlock(&smallalloc_mtx);
644 vm_paddr_t pa = vtophys((vm_offset_t)mem);
646 m = PHYS_TO_VM_PAGE(pa);
649 atomic_subtract_int(&cnt.v_wire_count, 1);