1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29
30 #include <sys/types.h>
31 #include <sys/t_lock.h>
32 #include <sys/param.h>
33 #include <sys/cred.h>
34 #include <sys/debug.h>
35 #include <sys/inline.h>
36 #include <sys/kmem.h>
37 #include <sys/proc.h>
38 #include <sys/regset.h>
39 #include <sys/privregs.h>
40 #include <sys/sysmacros.h>
41 #include <sys/systm.h>
42 #include <sys/vfs.h>
43 #include <sys/vnode.h>
44 #include <sys/psw.h>
45 #include <sys/pcb.h>
46 #include <sys/buf.h>
47 #include <sys/signal.h>
48 #include <sys/user.h>
49 #include <sys/cpuvar.h>
50
51 #include <sys/fault.h>
52 #include <sys/syscall.h>
53 #include <sys/procfs.h>
54 #include <sys/cmn_err.h>
55 #include <sys/stack.h>
56 #include <sys/debugreg.h>
57 #include <sys/copyops.h>
58
59 #include <sys/vmem.h>
60 #include <sys/mman.h>
61 #include <sys/vmparam.h>
62 #include <sys/fp.h>
63 #include <sys/archsystm.h>
64 #include <sys/vmsystm.h>
65 #include <vm/hat.h>
66 #include <vm/as.h>
67 #include <vm/seg.h>
68 #include <vm/seg_kmem.h>
69 #include <vm/seg_kp.h>
70 #include <vm/page.h>
71
72 #include <sys/sysi86.h>
73
74 #include <fs/proc/prdata.h>
75
76 int prnwatch = 10000; /* maximum number of watched areas */
77
78 /*
79 * Force a thread into the kernel if it is not already there.
80 * This is a no-op on uniprocessors.
81 */
82 /* ARGSUSED */
83 void
84 prpokethread(kthread_t *t)
85 {
86 if (t->t_state == TS_ONPROC && t->t_cpu != CPU)
87 poke_cpu(t->t_cpu->cpu_id);
88 }
89
90 /*
91 * Return general registers.
92 */
93 void
94 prgetprregs(klwp_t *lwp, prgregset_t prp)
95 {
96 ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
97
98 getgregs(lwp, prp);
99 }
100
101 /*
102 * Set general registers.
103 * (Note: This can be an alias to setgregs().)
104 */
105 void
106 prsetprregs(klwp_t *lwp, prgregset_t prp, int initial)
107 {
108 if (initial) /* set initial values */
109 lwptoregs(lwp)->r_ps = PSL_USER;
110 (void) setgregs(lwp, prp);
111 }
112
113 #ifdef _SYSCALL32_IMPL
114
115 /*
116 * Convert prgregset32 to native prgregset
117 */
118 void
119 prgregset_32ton(klwp_t *lwp, prgregset32_t src, prgregset_t dst)
120 {
121 struct regs *rp = lwptoregs(lwp);
122
123 dst[REG_GSBASE] = lwp->lwp_pcb.pcb_gsbase;
124 dst[REG_FSBASE] = lwp->lwp_pcb.pcb_fsbase;
125
126 dst[REG_DS] = (uint16_t)src[DS];
127 dst[REG_ES] = (uint16_t)src[ES];
128
129 dst[REG_GS] = (uint16_t)src[GS];
130 dst[REG_FS] = (uint16_t)src[FS];
131 dst[REG_SS] = (uint16_t)src[SS];
132 dst[REG_RSP] = (uint32_t)src[UESP];
133 dst[REG_RFL] =
134 (rp->r_ps & ~PSL_USERMASK) | (src[EFL] & PSL_USERMASK);
135 dst[REG_CS] = (uint16_t)src[CS];
136 dst[REG_RIP] = (uint32_t)src[EIP];
137 dst[REG_ERR] = (uint32_t)src[ERR];
138 dst[REG_TRAPNO] = (uint32_t)src[TRAPNO];
139 dst[REG_RAX] = (uint32_t)src[EAX];
140 dst[REG_RCX] = (uint32_t)src[ECX];
141 dst[REG_RDX] = (uint32_t)src[EDX];
142 dst[REG_RBX] = (uint32_t)src[EBX];
143 dst[REG_RBP] = (uint32_t)src[EBP];
144 dst[REG_RSI] = (uint32_t)src[ESI];
145 dst[REG_RDI] = (uint32_t)src[EDI];
146 dst[REG_R8] = dst[REG_R9] = dst[REG_R10] = dst[REG_R11] =
147 dst[REG_R12] = dst[REG_R13] = dst[REG_R14] = dst[REG_R15] = 0;
148 }
149
150 /*
151 * Return 32-bit general registers
152 */
153 void
154 prgetprregs32(klwp_t *lwp, prgregset32_t prp)
155 {
156 ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
157 getgregs32(lwp, prp);
158 }
159
160 #endif /* _SYSCALL32_IMPL */
161
162 /*
163 * Get the syscall return values for the lwp.
164 */
165 int
166 prgetrvals(klwp_t *lwp, long *rval1, long *rval2)
167 {
168 struct regs *r = lwptoregs(lwp);
169
170 if (r->r_ps & PS_C)
171 return (r->r_r0);
172 if (lwp->lwp_eosys == JUSTRETURN) {
173 *rval1 = 0;
174 *rval2 = 0;
175 } else if (lwp_getdatamodel(lwp) != DATAMODEL_NATIVE) {
176 /*
177 * XX64 Not sure we -really- need to do this, because the
178 * syscall return already masks off the bottom values ..?
179 */
180 *rval1 = r->r_r0 & (uint32_t)0xffffffffu;
181 *rval2 = r->r_r1 & (uint32_t)0xffffffffu;
182 } else {
183 *rval1 = r->r_r0;
184 *rval2 = r->r_r1;
185 }
186 return (0);
187 }
188
189 /*
190 * Does the system support floating-point, either through hardware
191 * or by trapping and emulating floating-point machine instructions?
192 */
193 int
194 prhasfp(void)
195 {
196 extern int fp_kind;
197
198 return (fp_kind != FP_NO);
199 }
200
201 /*
202 * Get floating-point registers.
203 */
204 void
205 prgetprfpregs(klwp_t *lwp, prfpregset_t *pfp)
206 {
207 bzero(pfp, sizeof (prfpregset_t));
208 getfpregs(lwp, pfp);
209 }
210
211 #if defined(_SYSCALL32_IMPL)
212 void
213 prgetprfpregs32(klwp_t *lwp, prfpregset32_t *pfp)
214 {
215 bzero(pfp, sizeof (*pfp));
216 getfpregs32(lwp, pfp);
217 }
218 #endif /* _SYSCALL32_IMPL */
219
220 /*
221 * Set floating-point registers.
222 * (Note: This can be an alias to setfpregs().)
223 */
224 void
225 prsetprfpregs(klwp_t *lwp, prfpregset_t *pfp)
226 {
227 setfpregs(lwp, pfp);
228 }
229
230 #if defined(_SYSCALL32_IMPL)
231 void
232 prsetprfpregs32(klwp_t *lwp, prfpregset32_t *pfp)
233 {
234 setfpregs32(lwp, pfp);
235 }
236 #endif /* _SYSCALL32_IMPL */
237
238 /*
239 * Does the system support extra register state?
240 */
241 int
242 prhasx(proc_t *p)
243 {
244 /* XXX */
245 return (1);
246 }
247
248 /*
249 * Get the size of the extra registers.
250 */
251 int
252 prgetprxregsize(proc_t *p)
253 {
254 return (xregs_getsize(p));
255 }
256
257 /*
258 * Get extra registers.
259 */
260 void
261 prgetprxregs(klwp_t *lwp, caddr_t prx)
262 {
263 extern void xregs_get(struct _klwp *, caddr_t);
264
265 xregs_get(lwp, prx);
266 }
267
268 /*
269 * Set extra registers.
270 */
271 void
272 prsetprxregs(klwp_t *lwp, caddr_t prx)
273 {
274 extern void xregs_set(struct _klwp *, caddr_t);
275
276 xregs_set(lwp, prx);
277 }
278
279 /*
280 * Return the base (lower limit) of the process stack.
281 */
282 caddr_t
283 prgetstackbase(proc_t *p)
284 {
285 return (p->p_usrstack - p->p_stksize);
286 }
287
288 /*
289 * Return the "addr" field for pr_addr in prpsinfo_t.
290 * This is a vestige of the past, so whatever we return is OK.
291 */
292 caddr_t
293 prgetpsaddr(proc_t *p)
294 {
295 return ((caddr_t)p);
296 }
297
298 /*
299 * Arrange to single-step the lwp.
300 */
301 void
302 prstep(klwp_t *lwp, int watchstep)
303 {
304 ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
305
306 /*
307 * flag LWP so that its r_efl trace bit (PS_T) will be set on
308 * next return to usermode.
309 */
310 lwp->lwp_pcb.pcb_flags |= REQUEST_STEP;
311 lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP;
312
313 if (watchstep)
314 lwp->lwp_pcb.pcb_flags |= WATCH_STEP;
315 else
316 lwp->lwp_pcb.pcb_flags |= NORMAL_STEP;
317
318 aston(lwptot(lwp)); /* let trap() set PS_T in rp->r_efl */
319 }
320
321 /*
322 * Undo prstep().
323 */
324 void
325 prnostep(klwp_t *lwp)
326 {
327 ASSERT(ttolwp(curthread) == lwp ||
328 MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
329
330 /*
331 * flag LWP so that its r_efl trace bit (PS_T) will be cleared on
332 * next return to usermode.
333 */
334 lwp->lwp_pcb.pcb_flags |= REQUEST_NOSTEP;
335
336 lwp->lwp_pcb.pcb_flags &=
337 ~(REQUEST_STEP|NORMAL_STEP|WATCH_STEP|DEBUG_PENDING);
338
339 aston(lwptot(lwp)); /* let trap() clear PS_T in rp->r_efl */
340 }
341
342 /*
343 * Return non-zero if a single-step is in effect.
344 */
345 int
346 prisstep(klwp_t *lwp)
347 {
348 ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
349
350 return ((lwp->lwp_pcb.pcb_flags &
351 (NORMAL_STEP|WATCH_STEP|DEBUG_PENDING)) != 0);
352 }
353
354 /*
355 * Set the PC to the specified virtual address.
356 */
357 void
358 prsvaddr(klwp_t *lwp, caddr_t vaddr)
359 {
360 struct regs *r = lwptoregs(lwp);
361
362 ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
363
364 r->r_pc = (uintptr_t)vaddr;
365 }
366
367 /*
368 * Map address "addr" in address space "as" into a kernel virtual address.
369 * The memory is guaranteed to be resident and locked down.
370 */
371 caddr_t
372 prmapin(struct as *as, caddr_t addr, int writing)
373 {
374 page_t *pp;
375 caddr_t kaddr;
376 pfn_t pfnum;
377
378 /*
379 * XXX - Because of past mistakes, we have bits being returned
380 * by getpfnum that are actually the page type bits of the pte.
381 * When the object we are trying to map is a memory page with
382 * a page structure everything is ok and we can use the optimal
383 * method, ppmapin. Otherwise, we have to do something special.
384 */
385 pfnum = hat_getpfnum(as->a_hat, addr);
386 if (pf_is_memory(pfnum)) {
387 pp = page_numtopp_nolock(pfnum);
388 if (pp != NULL) {
389 ASSERT(PAGE_LOCKED(pp));
390 kaddr = ppmapin(pp, writing ?
391 (PROT_READ | PROT_WRITE) : PROT_READ, (caddr_t)-1);
392 return (kaddr + ((uintptr_t)addr & PAGEOFFSET));
393 }
394 }
395
396 /*
397 * Oh well, we didn't have a page struct for the object we were
398 * trying to map in; ppmapin doesn't handle devices, but allocating a
399 * heap address allows ppmapout to free virtual space when done.
400 */
401 kaddr = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
402
403 hat_devload(kas.a_hat, kaddr, MMU_PAGESIZE, pfnum,
404 writing ? (PROT_READ | PROT_WRITE) : PROT_READ, 0);
405
406 return (kaddr + ((uintptr_t)addr & PAGEOFFSET));
407 }
408
409 /*
410 * Unmap address "addr" in address space "as"; inverse of prmapin().
411 */
412 /* ARGSUSED */
413 void
414 prmapout(struct as *as, caddr_t addr, caddr_t vaddr, int writing)
415 {
416 extern void ppmapout(caddr_t);
417
418 vaddr = (caddr_t)((uintptr_t)vaddr & PAGEMASK);
419 ppmapout(vaddr);
420 }
421
422 /*
423 * Make sure the lwp is in an orderly state
424 * for inspection by a debugger through /proc.
425 *
426 * This needs to be called only once while the current thread remains in the
427 * kernel and needs to be called while holding no resources (mutex locks, etc).
428 *
429 * As a hedge against these conditions, if prstop() is called repeatedly
430 * before prunstop() is called, it does nothing and just returns.
431 *
432 * prunstop() must be called before the thread returns to user level.
433 */
434 /* ARGSUSED */
435 void
436 prstop(int why, int what)
437 {
438 klwp_t *lwp = ttolwp(curthread);
439 struct regs *r = lwptoregs(lwp);
440
441 if (lwp->lwp_pcb.pcb_flags & PRSTOP_CALLED)
442 return;
443
444 /*
445 * Make sure we don't deadlock on a recursive call
446 * to prstop(). stop() tests the lwp_nostop flag.
447 */
448 ASSERT(lwp->lwp_nostop == 0);
449 lwp->lwp_nostop = 1;
450
451 if (copyin_nowatch((caddr_t)r->r_pc, &lwp->lwp_pcb.pcb_instr,
452 sizeof (lwp->lwp_pcb.pcb_instr)) == 0)
453 lwp->lwp_pcb.pcb_flags |= INSTR_VALID;
454 else {
455 lwp->lwp_pcb.pcb_flags &= ~INSTR_VALID;
456 lwp->lwp_pcb.pcb_instr = 0;
457 }
458
459 (void) save_syscall_args();
460 ASSERT(lwp->lwp_nostop == 1);
461 lwp->lwp_nostop = 0;
462
463 lwp->lwp_pcb.pcb_flags |= PRSTOP_CALLED;
464 aston(curthread); /* so prunstop() will be called */
465 }
466
467 /*
468 * Inform prstop() that it should do its work again
469 * the next time it is called.
470 */
471 void
472 prunstop(void)
473 {
474 ttolwp(curthread)->lwp_pcb.pcb_flags &= ~PRSTOP_CALLED;
475 }
476
477 /*
478 * Fetch the user-level instruction on which the lwp is stopped.
479 * It was saved by the lwp itself, in prstop().
480 * Return non-zero if the instruction is valid.
481 */
482 int
483 prfetchinstr(klwp_t *lwp, ulong_t *ip)
484 {
485 *ip = (ulong_t)(instr_t)lwp->lwp_pcb.pcb_instr;
486 return (lwp->lwp_pcb.pcb_flags & INSTR_VALID);
487 }
488
489 /*
490 * Called from trap() when a load or store instruction
491 * falls in a watched page but is not a watchpoint.
492 * We emulate the instruction in the kernel.
493 */
494 /* ARGSUSED */
495 int
496 pr_watch_emul(struct regs *rp, caddr_t addr, enum seg_rw rw)
497 {
498 #ifdef SOMEDAY
499 int res;
500 proc_t *p = curproc;
501 char *badaddr = (caddr_t)(-1);
502 int mapped;
503
504 /* prevent recursive calls to pr_watch_emul() */
505 ASSERT(!(curthread->t_flag & T_WATCHPT));
506 curthread->t_flag |= T_WATCHPT;
507
508 watch_disable_addr(addr, 8, rw);
509 res = do_unaligned(rp, &badaddr);
510 watch_enable_addr(addr, 8, rw);
511
512 curthread->t_flag &= ~T_WATCHPT;
513 if (res == SIMU_SUCCESS) {
514 /* adjust the pc */
515 return (1);
516 }
517 #endif
518 return (0);
519 }
520
521 /*
522 * Return the number of active entries in the local descriptor table.
523 */
524 int
525 prnldt(proc_t *p)
526 {
527 int limit, i, n;
528 user_desc_t *udp;
529
530 ASSERT(MUTEX_HELD(&p->p_ldtlock));
531
532 /*
533 * Currently 64 bit processes cannot have private LDTs.
534 */
535 ASSERT(p->p_model != DATAMODEL_LP64 || p->p_ldt == NULL);
536
537 if (p->p_ldt == NULL)
538 return (0);
539 n = 0;
540 limit = p->p_ldtlimit;
541 ASSERT(limit >= 0 && limit < MAXNLDT);
542
543 /*
544 * Count all present user descriptors.
545 */
546 for (i = LDT_UDBASE, udp = &p->p_ldt[i]; i <= limit; i++, udp++)
547 if (udp->usd_type != 0 || udp->usd_dpl != 0 || udp->usd_p != 0)
548 n++;
549 return (n);
550 }
551
552 /*
553 * Fetch the active entries from the local descriptor table.
554 */
555 void
556 prgetldt(proc_t *p, struct ssd *ssd)
557 {
558 int i, limit;
559 user_desc_t *udp;
560
561 ASSERT(MUTEX_HELD(&p->p_ldtlock));
562
563 if (p->p_ldt == NULL)
564 return;
565
566 limit = p->p_ldtlimit;
567 ASSERT(limit >= 0 && limit < MAXNLDT);
568
569 /*
570 * All present user descriptors.
571 */
572 for (i = LDT_UDBASE, udp = &p->p_ldt[i]; i <= limit; i++, udp++)
573 if (udp->usd_type != 0 || udp->usd_dpl != 0 ||
574 udp->usd_p != 0)
575 usd_to_ssd(udp, ssd++, SEL_LDT(i));
576 }