]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/ia64/ia64/vm_machdep.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / ia64 / ia64 / 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  * $FreeBSD$
42  */
43 /*-
44  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
45  * All rights reserved.
46  *
47  * Author: Chris G. Demetriou
48  * 
49  * Permission to use, copy, modify and distribute this software and
50  * its documentation is hereby granted, provided that both the copyright
51  * notice and this permission notice appear in all copies of the
52  * software, derivative works or modified versions, and any portions
53  * thereof, and that both notices appear in supporting documentation.
54  * 
55  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 
56  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 
57  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58  * 
59  * Carnegie Mellon requests users of this software to return to
60  *
61  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62  *  School of Computer Science
63  *  Carnegie Mellon University
64  *  Pittsburgh PA 15213-3890
65  *
66  * any improvements or extensions that they make and grant Carnegie the
67  * rights to redistribute these changes.
68  */
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/proc.h>
73 #include <sys/malloc.h>
74 #include <sys/bio.h>
75 #include <sys/buf.h>
76 #include <sys/vnode.h>
77 #include <sys/vmmeter.h>
78 #include <sys/kernel.h>
79 #include <sys/mbuf.h>
80 #include <sys/sf_buf.h>
81 #include <sys/sysctl.h>
82 #include <sys/unistd.h>
83
84 #include <machine/cpu.h>
85 #include <machine/fpu.h>
86 #include <machine/md_var.h>
87 #include <machine/pcb.h>
88
89 #include <vm/vm.h>
90 #include <vm/vm_param.h>
91 #include <sys/lock.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_extern.h>
96
97 #include <i386/include/psl.h>
98
99 void
100 cpu_thread_exit(struct thread *td)
101 {
102
103         /* Throw away the high FP registers. */
104         ia64_highfp_drop(td);
105 }
106
107 void
108 cpu_thread_clean(struct thread *td)
109 {
110 }
111
112 void
113 cpu_thread_alloc(struct thread *td)
114 {
115         intptr_t sp;
116
117         sp = td->td_kstack + td->td_kstack_pages * PAGE_SIZE;
118         sp -= sizeof(struct pcb);
119         td->td_pcb = (struct pcb *)sp;
120         sp -= sizeof(struct trapframe);
121         td->td_frame = (struct trapframe *)sp;
122         td->td_frame->tf_length = sizeof(struct trapframe);
123         mtx_init(&td->td_md.md_highfp_mtx, "High FP lock", NULL, MTX_SPIN);
124 }
125
126 void
127 cpu_thread_free(struct thread *td)
128 {
129
130         mtx_destroy(&td->td_md.md_highfp_mtx);
131 }
132
133 void
134 cpu_thread_swapin(struct thread *td)
135 {
136 }
137
138 void
139 cpu_thread_swapout(struct thread *td)
140 {
141
142         ia64_highfp_save(td);
143 }
144
145 void
146 cpu_set_upcall(struct thread *td, struct thread *td0)
147 {
148         struct pcb *pcb;
149         struct trapframe *tf;
150
151         tf = td->td_frame;
152         KASSERT(tf != NULL, ("foo"));
153         bcopy(td0->td_frame, tf, sizeof(*tf));
154         tf->tf_length = sizeof(struct trapframe);
155         tf->tf_flags = FRAME_SYSCALL;
156         tf->tf_special.ndirty = 0;
157         tf->tf_special.bspstore &= ~0x1ffUL;
158         tf->tf_scratch.gr8 = 0;
159         tf->tf_scratch.gr9 = 1;
160         tf->tf_scratch.gr10 = 0;
161
162         pcb = td->td_pcb;
163         KASSERT(pcb != NULL, ("foo"));
164         bcopy(td0->td_pcb, pcb, sizeof(*pcb));
165         pcb->pcb_special.bspstore = td->td_kstack;
166         pcb->pcb_special.pfs = 0;
167         pcb->pcb_current_pmap = vmspace_pmap(td->td_proc->p_vmspace);
168         pcb->pcb_special.sp = (uintptr_t)tf - 16;
169         pcb->pcb_special.rp = FDESC_FUNC(fork_trampoline);
170         cpu_set_fork_handler(td, (void (*)(void*))fork_return, td);
171
172         /* Setup to release the spin count in fork_exit(). */
173         td->td_md.md_spinlock_count = 1;
174         td->td_md.md_saved_intr = 1;
175 }
176
177 void
178 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
179         stack_t *stack)
180 {
181         struct ia64_fdesc *fd;
182         struct trapframe *tf;
183         uint64_t ndirty, sp;
184
185         tf = td->td_frame;
186         ndirty = tf->tf_special.ndirty + (tf->tf_special.bspstore & 0x1ffUL);
187
188         KASSERT((ndirty & ~PAGE_MASK) == 0,
189             ("Whoa there! We have more than 8KB of dirty registers!"));
190
191         fd = (struct ia64_fdesc *)entry;
192         sp = (uint64_t)stack->ss_sp;
193
194         bzero(&tf->tf_special, sizeof(tf->tf_special));
195         tf->tf_special.iip = fuword(&fd->func);
196         tf->tf_special.gp = fuword(&fd->gp);
197         tf->tf_special.sp = (sp + stack->ss_size - 16) & ~15;
198         tf->tf_special.rsc = 0xf;
199         tf->tf_special.fpsr = IA64_FPSR_DEFAULT;
200         tf->tf_special.psr = IA64_PSR_IC | IA64_PSR_I | IA64_PSR_IT |
201             IA64_PSR_DT | IA64_PSR_RT | IA64_PSR_DFH | IA64_PSR_BN |
202             IA64_PSR_CPL_USER;
203
204         if (tf->tf_flags & FRAME_SYSCALL) {
205                 tf->tf_special.cfm = (3UL<<62) | (1UL<<7) | 1UL;
206                 tf->tf_special.bspstore = sp + 8;
207                 suword((caddr_t)sp, (uint64_t)arg);
208         } else {
209                 tf->tf_special.cfm = (1UL<<63) | (1UL<<7) | 1UL;
210                 tf->tf_special.bspstore = sp;
211                 tf->tf_special.ndirty = 8;
212                 sp = td->td_kstack + ndirty - 8;
213                 if ((sp & 0x1ff) == 0x1f8) {
214                         *(uint64_t*)sp = 0;
215                         tf->tf_special.ndirty += 8;
216                         sp -= 8;
217                 }
218                 *(uint64_t*)sp = (uint64_t)arg;
219         }
220 }
221
222 int
223 cpu_set_user_tls(struct thread *td, void *tls_base)
224 {
225         td->td_frame->tf_special.tp = (unsigned long)tls_base;
226         return (0);
227 }
228
229 /*
230  * Finish a fork operation, with process p2 nearly set up.
231  * Copy and update the pcb, set up the stack so that the child
232  * ready to run and return to user mode.
233  */
234 void
235 cpu_fork(struct thread *td1, struct proc *p2 __unused, struct thread *td2,
236     int flags)
237 {
238         char *stackp;
239         uint64_t ndirty;
240
241         KASSERT(td1 == curthread || td1 == &thread0,
242             ("cpu_fork: td1 not curthread and not thread0"));
243
244         if ((flags & RFPROC) == 0)
245                 return;
246
247         /*
248          * Save the preserved registers and the high FP registers in the
249          * PCB if we're the parent (ie td1 == curthread) so that we have
250          * a valid PCB. This also causes a RSE flush. We don't have to
251          * do that otherwise, because there wouldn't be anything important
252          * to save.
253          */
254         if (td1 == curthread) {
255                 if (savectx(td1->td_pcb) != 0)
256                         panic("unexpected return from savectx()");
257                 ia64_highfp_save(td1);
258         }
259
260         /*
261          * create the child's kernel stack and backing store. We basicly
262          * create an image of the parent's stack and backing store and
263          * adjust where necessary.
264          */
265         stackp = (char *)(td2->td_kstack + td2->td_kstack_pages * PAGE_SIZE);
266
267         stackp -= sizeof(struct pcb);
268         td2->td_pcb = (struct pcb *)stackp;
269         bcopy(td1->td_pcb, td2->td_pcb, sizeof(struct pcb));
270
271         stackp -= sizeof(struct trapframe);
272         td2->td_frame = (struct trapframe *)stackp;
273         bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
274         td2->td_frame->tf_length = sizeof(struct trapframe);
275         ndirty = td2->td_frame->tf_special.ndirty +
276             (td2->td_frame->tf_special.bspstore & 0x1ffUL);
277         bcopy((void*)td1->td_kstack, (void*)td2->td_kstack, ndirty);
278
279         /* Set-up the return values as expected by the fork() libc stub. */
280         if (td2->td_frame->tf_special.psr & IA64_PSR_IS) {
281                 td2->td_frame->tf_scratch.gr8 = 0;
282                 td2->td_frame->tf_scratch.gr10 = 1;
283         } else {
284                 td2->td_frame->tf_scratch.gr8 = 0;
285                 td2->td_frame->tf_scratch.gr9 = 1;
286                 td2->td_frame->tf_scratch.gr10 = 0;
287         }
288
289         td2->td_pcb->pcb_special.bspstore = td2->td_kstack + ndirty;
290         td2->td_pcb->pcb_special.pfs = 0;
291         td2->td_pcb->pcb_current_pmap = vmspace_pmap(td2->td_proc->p_vmspace);
292
293         td2->td_pcb->pcb_special.sp = (uintptr_t)stackp - 16;
294         td2->td_pcb->pcb_special.rp = FDESC_FUNC(fork_trampoline);
295         cpu_set_fork_handler(td2, (void (*)(void*))fork_return, td2);
296
297         /* Setup to release the spin count in fork_exit(). */
298         td2->td_md.md_spinlock_count = 1;
299         td2->td_md.md_saved_intr = 1;
300 }
301
302 /*
303  * Intercept the return address from a freshly forked process that has NOT
304  * been scheduled yet.
305  *
306  * This is needed to make kernel threads stay in kernel mode.
307  */
308 void
309 cpu_set_fork_handler(td, func, arg)
310         struct thread *td;
311         void (*func)(void *);
312         void *arg;
313 {
314         td->td_frame->tf_scratch.gr2 = (u_int64_t)func;
315         td->td_frame->tf_scratch.gr3 = (u_int64_t)arg;
316 }
317
318 /*
319  * cpu_exit is called as the last action during exit.
320  * We drop the fp state (if we have it) and switch to a live one.
321  */
322 void
323 cpu_exit(struct thread *td)
324 {
325 }
326
327 /*
328  * Allocate an sf_buf for the given vm_page.  On this machine, however, there
329  * is no sf_buf object.  Instead, an opaque pointer to the given vm_page is
330  * returned.
331  */
332 struct sf_buf *
333 sf_buf_alloc(struct vm_page *m, int pri)
334 {
335
336         return ((struct sf_buf *)m);
337 }
338
339 /*
340  * Free the sf_buf.  In fact, do nothing because there are no resources
341  * associated with the sf_buf.
342  */
343 void
344 sf_buf_free(struct sf_buf *sf)
345 {
346 }
347
348 /*
349  * Software interrupt handler for queued VM system processing.
350  */   
351 void  
352 swi_vm(void *dummy) 
353 {     
354 #if 0
355         if (busdma_swi_pending != 0)
356                 busdma_swi();
357 #endif
358 }