]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/arm/arm/vm_machdep.c
MFC r271394, r271398:
[FreeBSD/stable/10.git] / sys / arm / arm / vm_machdep.c
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  */
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/proc.h>
52 #include <sys/socketvar.h>
53 #include <sys/sf_buf.h>
54 #include <sys/syscall.h>
55 #include <sys/sysctl.h>
56 #include <sys/sysent.h>
57 #include <sys/unistd.h>
58 #include <machine/cpu.h>
59 #include <machine/frame.h>
60 #include <machine/pcb.h>
61 #include <machine/sysarch.h>
62 #include <sys/lock.h>
63 #include <sys/mutex.h>
64
65 #include <vm/vm.h>
66 #include <vm/pmap.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_kern.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_param.h>
72 #include <vm/vm_pageout.h>
73 #include <vm/uma.h>
74 #include <vm/uma_int.h>
75
76 #include <machine/md_var.h>
77 #include <machine/vfp.h>
78
79 /*
80  * struct switchframe and trapframe must both be a multiple of 8
81  * for correct stack alignment.
82  */
83 CTASSERT(sizeof(struct switchframe) == 24);
84 CTASSERT(sizeof(struct trapframe) == 80);
85
86 #ifndef NSFBUFS
87 #define NSFBUFS         (512 + maxusers * 16)
88 #endif
89
90 static int nsfbufs;
91 static int nsfbufspeak;
92 static int nsfbufsused;
93
94 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
95     "Maximum number of sendfile(2) sf_bufs available");
96 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
97     "Number of sendfile(2) sf_bufs at peak usage");
98 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
99     "Number of sendfile(2) sf_bufs in use");
100
101 static void     sf_buf_init(void *arg);
102 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL);
103
104 LIST_HEAD(sf_head, sf_buf);
105
106 /*
107  * A hash table of active sendfile(2) buffers
108  */
109 static struct sf_head *sf_buf_active;
110 static u_long sf_buf_hashmask;
111
112 #define SF_BUF_HASH(m)  (((m) - vm_page_array) & sf_buf_hashmask)
113
114 static TAILQ_HEAD(, sf_buf) sf_buf_freelist;
115 static u_int    sf_buf_alloc_want;
116
117 /*
118  * A lock used to synchronize access to the hash table and free list
119  */
120 static struct mtx sf_buf_lock;
121
122 /*
123  * Finish a fork operation, with process p2 nearly set up.
124  * Copy and update the pcb, set up the stack so that the child
125  * ready to run and return to user mode.
126  */
127 void
128 cpu_fork(register struct thread *td1, register struct proc *p2,
129     struct thread *td2, int flags)
130 {
131         struct pcb *pcb2;
132         struct trapframe *tf;
133         struct switchframe *sf;
134         struct mdproc *mdp2;
135
136         if ((flags & RFPROC) == 0)
137                 return;
138         pcb2 = (struct pcb *)(td2->td_kstack + td2->td_kstack_pages * PAGE_SIZE) - 1;
139 #ifdef __XSCALE__
140 #ifndef CPU_XSCALE_CORE3
141         pmap_use_minicache(td2->td_kstack, td2->td_kstack_pages * PAGE_SIZE);
142 #endif
143 #endif
144         td2->td_pcb = pcb2;
145         bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
146         mdp2 = &p2->p_md;
147         bcopy(&td1->td_proc->p_md, mdp2, sizeof(*mdp2));
148         pcb2->un_32.pcb32_sp = td2->td_kstack +
149             USPACE_SVC_STACK_TOP - sizeof(*pcb2);
150         pcb2->pcb_vfpcpu = -1;
151         pcb2->pcb_vfpstate.fpscr = VFPSCR_DN | VFPSCR_FZ;
152         pmap_activate(td2);
153         td2->td_frame = tf = (struct trapframe *)STACKALIGN(
154             pcb2->un_32.pcb32_sp - sizeof(struct trapframe));
155         *tf = *td1->td_frame;
156         sf = (struct switchframe *)tf - 1;
157         sf->sf_r4 = (u_int)fork_return;
158         sf->sf_r5 = (u_int)td2;
159         sf->sf_pc = (u_int)fork_trampoline;
160         tf->tf_spsr &= ~PSR_C;
161         tf->tf_r0 = 0;
162         tf->tf_r1 = 0;
163         pcb2->un_32.pcb32_sp = (u_int)sf;
164         KASSERT((pcb2->un_32.pcb32_sp & 7) == 0,
165             ("cpu_fork: Incorrect stack alignment"));
166
167         /* Setup to release spin count in fork_exit(). */
168         td2->td_md.md_spinlock_count = 1;
169         td2->td_md.md_saved_cspr = 0;
170 #ifdef ARM_TP_ADDRESS
171         td2->td_md.md_tp = *(register_t *)ARM_TP_ADDRESS;
172 #else
173         td2->td_md.md_tp = (register_t) get_tls();
174 #endif
175 }
176                                 
177 void
178 cpu_thread_swapin(struct thread *td)
179 {
180 }
181
182 void
183 cpu_thread_swapout(struct thread *td)
184 {
185 }
186
187 /*
188  * Detatch mapped page and release resources back to the system.
189  */
190 void
191 sf_buf_free(struct sf_buf *sf)
192 {
193
194          mtx_lock(&sf_buf_lock);
195          sf->ref_count--;
196          if (sf->ref_count == 0) {
197                  TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry);
198                  nsfbufsused--;
199                  pmap_kremove(sf->kva);
200                  sf->m = NULL;
201                  LIST_REMOVE(sf, list_entry);
202                  if (sf_buf_alloc_want > 0)
203                          wakeup(&sf_buf_freelist);
204          }
205          mtx_unlock(&sf_buf_lock);
206 }
207
208 /*
209  * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
210  */
211 static void
212 sf_buf_init(void *arg)
213 {
214         struct sf_buf *sf_bufs;
215         vm_offset_t sf_base;
216         int i;
217
218         nsfbufs = NSFBUFS;
219         TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
220                 
221         sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask);
222         TAILQ_INIT(&sf_buf_freelist);
223         sf_base = kva_alloc(nsfbufs * PAGE_SIZE);
224         sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
225             M_NOWAIT | M_ZERO);
226         for (i = 0; i < nsfbufs; i++) {
227                 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
228                 TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry);
229         }
230         sf_buf_alloc_want = 0;
231         mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF);
232 }
233
234 /*
235  * Get an sf_buf from the freelist. Will block if none are available.
236  */
237 struct sf_buf *
238 sf_buf_alloc(struct vm_page *m, int flags)
239 {
240         struct sf_head *hash_list;
241         struct sf_buf *sf;
242         int error;
243
244         hash_list = &sf_buf_active[SF_BUF_HASH(m)];
245         mtx_lock(&sf_buf_lock);
246         LIST_FOREACH(sf, hash_list, list_entry) {
247                 if (sf->m == m) {
248                         sf->ref_count++;
249                         if (sf->ref_count == 1) {
250                                 TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
251                                 nsfbufsused++;
252                                 nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
253                         }
254                         goto done;
255                 }
256         }
257         while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) {
258                 if (flags & SFB_NOWAIT)
259                         goto done;
260                 sf_buf_alloc_want++;
261                 SFSTAT_INC(sf_allocwait);
262                 error = msleep(&sf_buf_freelist, &sf_buf_lock,
263                     (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0);
264                 sf_buf_alloc_want--;
265         
266
267                 /*
268                  * If we got a signal, don't risk going back to sleep.
269                  */
270                 if (error)
271                         goto done;
272         }
273         TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
274         if (sf->m != NULL)
275                 LIST_REMOVE(sf, list_entry);
276         LIST_INSERT_HEAD(hash_list, sf, list_entry);
277         sf->ref_count = 1;
278         sf->m = m;
279         nsfbufsused++;
280         nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
281         pmap_kenter(sf->kva, VM_PAGE_TO_PHYS(sf->m));
282 done:
283         mtx_unlock(&sf_buf_lock);
284         return (sf);
285 }
286
287 void
288 cpu_set_syscall_retval(struct thread *td, int error)
289 {
290         struct trapframe *frame;
291         int fixup;
292 #ifdef __ARMEB__
293         u_int call;
294 #endif
295
296         frame = td->td_frame;
297         fixup = 0;
298
299 #ifdef __ARMEB__
300         /*
301          * __syscall returns an off_t while most other syscalls return an
302          * int. As an off_t is 64-bits and an int is 32-bits we need to
303          * place the returned data into r1. As the lseek and frerebsd6_lseek
304          * syscalls also return an off_t they do not need this fixup.
305          */
306 #ifdef __ARM_EABI__
307         call = frame->tf_r7;
308 #else
309         call = *(u_int32_t *)(frame->tf_pc - INSN_SIZE) & 0x000fffff;
310 #endif
311         if (call == SYS___syscall) {
312                 register_t *ap = &frame->tf_r0;
313                 register_t code = ap[_QUAD_LOWWORD];
314                 if (td->td_proc->p_sysent->sv_mask)
315                         code &= td->td_proc->p_sysent->sv_mask;
316                 fixup = (code != SYS_freebsd6_lseek && code != SYS_lseek)
317                     ? 1 : 0;
318         }
319 #endif
320
321         switch (error) {
322         case 0:
323                 if (fixup) {
324                         frame->tf_r0 = 0;
325                         frame->tf_r1 = td->td_retval[0];
326                 } else {
327                         frame->tf_r0 = td->td_retval[0];
328                         frame->tf_r1 = td->td_retval[1];
329                 }
330                 frame->tf_spsr &= ~PSR_C;   /* carry bit */
331                 break;
332         case ERESTART:
333                 /*
334                  * Reconstruct the pc to point at the swi.
335                  */
336                 frame->tf_pc -= INSN_SIZE;
337                 break;
338         case EJUSTRETURN:
339                 /* nothing to do */
340                 break;
341         default:
342                 frame->tf_r0 = error;
343                 frame->tf_spsr |= PSR_C;    /* carry bit */
344                 break;
345         }
346 }
347
348 /*
349  * Initialize machine state (pcb and trap frame) for a new thread about to
350  * upcall. Put enough state in the new thread's PCB to get it to go back
351  * userret(), where we can intercept it again to set the return (upcall)
352  * Address and stack, along with those from upcals that are from other sources
353  * such as those generated in thread_userret() itself.
354  */
355 void
356 cpu_set_upcall(struct thread *td, struct thread *td0)
357 {
358         struct trapframe *tf;
359         struct switchframe *sf;
360
361         bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
362         bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
363         tf = td->td_frame;
364         sf = (struct switchframe *)tf - 1;
365         sf->sf_r4 = (u_int)fork_return;
366         sf->sf_r5 = (u_int)td;
367         sf->sf_pc = (u_int)fork_trampoline;
368         tf->tf_spsr &= ~PSR_C;
369         tf->tf_r0 = 0;
370         td->td_pcb->un_32.pcb32_sp = (u_int)sf;
371         KASSERT((td->td_pcb->un_32.pcb32_sp & 7) == 0,
372             ("cpu_set_upcall: Incorrect stack alignment"));
373
374         /* Setup to release spin count in fork_exit(). */
375         td->td_md.md_spinlock_count = 1;
376         td->td_md.md_saved_cspr = 0;
377 }
378
379 /*
380  * Set that machine state for performing an upcall that has to
381  * be done in thread_userret() so that those upcalls generated
382  * in thread_userret() itself can be done as well.
383  */
384 void
385 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
386         stack_t *stack)
387 {
388         struct trapframe *tf = td->td_frame;
389
390         tf->tf_usr_sp = STACKALIGN((int)stack->ss_sp + stack->ss_size
391             - sizeof(struct trapframe));
392         tf->tf_pc = (int)entry;
393         tf->tf_r0 = (int)arg;
394         tf->tf_spsr = PSR_USR32_MODE;
395 }
396
397 int
398 cpu_set_user_tls(struct thread *td, void *tls_base)
399 {
400
401         td->td_md.md_tp = (register_t)tls_base;
402         if (td == curthread) {
403                 critical_enter();
404 #ifdef ARM_TP_ADDRESS
405                 *(register_t *)ARM_TP_ADDRESS = (register_t)tls_base;
406 #else
407                 set_tls((void *)tls_base);
408 #endif
409                 critical_exit();
410         }
411         return (0);
412 }
413
414 void
415 cpu_thread_exit(struct thread *td)
416 {
417 }
418
419 void
420 cpu_thread_alloc(struct thread *td)
421 {
422         td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_pages *
423             PAGE_SIZE) - 1;
424         /*
425          * Ensure td_frame is aligned to an 8 byte boundary as it will be
426          * placed into the stack pointer which must be 8 byte aligned in
427          * the ARM EABI.
428          */
429         td->td_frame = (struct trapframe *)STACKALIGN((u_int)td->td_kstack +
430             USPACE_SVC_STACK_TOP - sizeof(struct pcb) -
431             sizeof(struct trapframe));
432 #ifdef __XSCALE__
433 #ifndef CPU_XSCALE_CORE3
434         pmap_use_minicache(td->td_kstack, td->td_kstack_pages * PAGE_SIZE);
435 #endif
436 #endif
437 }
438
439 void
440 cpu_thread_free(struct thread *td)
441 {
442 }
443
444 void
445 cpu_thread_clean(struct thread *td)
446 {
447 }
448
449 /*
450  * Intercept the return address from a freshly forked process that has NOT
451  * been scheduled yet.
452  *
453  * This is needed to make kernel threads stay in kernel mode.
454  */
455 void
456 cpu_set_fork_handler(struct thread *td, void (*func)(void *), void *arg)
457 {
458         struct switchframe *sf;
459         struct trapframe *tf;
460         
461         tf = td->td_frame;
462         sf = (struct switchframe *)tf - 1;
463         sf->sf_r4 = (u_int)func;
464         sf->sf_r5 = (u_int)arg;
465         td->td_pcb->un_32.pcb32_sp = (u_int)sf;
466         KASSERT((td->td_pcb->un_32.pcb32_sp & 7) == 0,
467             ("cpu_set_fork_handler: Incorrect stack alignment"));
468 }
469
470 /*
471  * Software interrupt handler for queued VM system processing.
472  */
473 void
474 swi_vm(void *dummy)
475 {
476         
477         if (busdma_swi_pending)
478                 busdma_swi();
479 }
480
481 void
482 cpu_exit(struct thread *td)
483 {
484 }
485