Print this page
XXXX adding PID information to netstat output
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/doorfs/door_sys.c
+++ new/usr/src/uts/common/fs/doorfs/door_sys.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 /*
27 27 * System call I/F to doors (outside of vnodes I/F) and misc support
28 28 * routines
29 29 */
30 30 #include <sys/types.h>
31 31 #include <sys/systm.h>
32 32 #include <sys/door.h>
33 33 #include <sys/door_data.h>
34 34 #include <sys/proc.h>
35 35 #include <sys/thread.h>
36 36 #include <sys/prsystm.h>
37 37 #include <sys/procfs.h>
38 38 #include <sys/class.h>
39 39 #include <sys/cred.h>
40 40 #include <sys/kmem.h>
41 41 #include <sys/cmn_err.h>
42 42 #include <sys/stack.h>
43 43 #include <sys/debug.h>
44 44 #include <sys/cpuvar.h>
45 45 #include <sys/file.h>
46 46 #include <sys/fcntl.h>
47 47 #include <sys/vnode.h>
48 48 #include <sys/vfs.h>
49 49 #include <sys/vfs_opreg.h>
50 50 #include <sys/sobject.h>
51 51 #include <sys/schedctl.h>
52 52 #include <sys/callb.h>
53 53 #include <sys/ucred.h>
54 54
55 55 #include <sys/mman.h>
56 56 #include <sys/sysmacros.h>
57 57 #include <sys/vmsystm.h>
58 58 #include <vm/as.h>
59 59 #include <vm/hat.h>
60 60 #include <vm/page.h>
61 61 #include <vm/seg.h>
62 62 #include <vm/seg_vn.h>
63 63 #include <vm/seg_vn.h>
64 64 #include <vm/seg_kpm.h>
65 65
66 66 #include <sys/modctl.h>
67 67 #include <sys/syscall.h>
68 68 #include <sys/pathname.h>
69 69 #include <sys/rctl.h>
70 70
71 71 /*
72 72 * The maximum amount of data (in bytes) that will be transferred using
73 73 * an intermediate kernel buffer. For sizes greater than this we map
74 74 * in the destination pages and perform a 1-copy transfer.
75 75 */
76 76 size_t door_max_arg = 16 * 1024;
77 77
78 78 /*
79 79 * Maximum amount of data that will be transferred in a reply to a
80 80 * door_upcall. Need to guard against a process returning huge amounts
81 81 * of data and getting the kernel stuck in kmem_alloc.
82 82 */
83 83 size_t door_max_upcall_reply = 1024 * 1024;
84 84
85 85 /*
86 86 * Maximum number of descriptors allowed to be passed in a single
87 87 * door_call or door_return. We need to allocate kernel memory
88 88 * for all of them at once, so we can't let it scale without limit.
89 89 */
90 90 uint_t door_max_desc = 1024;
91 91
92 92 /*
93 93 * Definition of a door handle, used by other kernel subsystems when
94 94 * calling door functions. This is really a file structure but we
95 95 * want to hide that fact.
96 96 */
97 97 struct __door_handle {
98 98 file_t dh_file;
99 99 };
100 100
101 101 #define DHTOF(dh) ((file_t *)(dh))
102 102 #define FTODH(fp) ((door_handle_t)(fp))
103 103
104 104 static int doorfs(long, long, long, long, long, long);
105 105
106 106 static struct sysent door_sysent = {
107 107 6,
108 108 SE_ARGC | SE_NOUNLOAD,
109 109 (int (*)())doorfs,
110 110 };
111 111
112 112 static struct modlsys modlsys = {
113 113 &mod_syscallops, "doors", &door_sysent
114 114 };
115 115
116 116 #ifdef _SYSCALL32_IMPL
117 117
118 118 static int
119 119 doorfs32(int32_t arg1, int32_t arg2, int32_t arg3, int32_t arg4,
120 120 int32_t arg5, int32_t subcode);
121 121
122 122 static struct sysent door_sysent32 = {
123 123 6,
124 124 SE_ARGC | SE_NOUNLOAD,
125 125 (int (*)())doorfs32,
126 126 };
127 127
128 128 static struct modlsys modlsys32 = {
129 129 &mod_syscallops32,
130 130 "32-bit door syscalls",
131 131 &door_sysent32
132 132 };
133 133 #endif
134 134
135 135 static struct modlinkage modlinkage = {
136 136 MODREV_1,
137 137 &modlsys,
138 138 #ifdef _SYSCALL32_IMPL
139 139 &modlsys32,
140 140 #endif
141 141 NULL
142 142 };
143 143
144 144 dev_t doordev;
145 145
146 146 extern struct vfs door_vfs;
147 147 extern struct vnodeops *door_vnodeops;
148 148
149 149 int
150 150 _init(void)
151 151 {
152 152 static const fs_operation_def_t door_vfsops_template[] = {
153 153 NULL, NULL
154 154 };
155 155 extern const fs_operation_def_t door_vnodeops_template[];
156 156 vfsops_t *door_vfsops;
157 157 major_t major;
158 158 int error;
159 159
160 160 mutex_init(&door_knob, NULL, MUTEX_DEFAULT, NULL);
161 161 if ((major = getudev()) == (major_t)-1)
162 162 return (ENXIO);
163 163 doordev = makedevice(major, 0);
164 164
165 165 /* Create a dummy vfs */
166 166 error = vfs_makefsops(door_vfsops_template, &door_vfsops);
167 167 if (error != 0) {
168 168 cmn_err(CE_WARN, "door init: bad vfs ops");
169 169 return (error);
170 170 }
171 171 VFS_INIT(&door_vfs, door_vfsops, NULL);
172 172 door_vfs.vfs_flag = VFS_RDONLY;
173 173 door_vfs.vfs_dev = doordev;
174 174 vfs_make_fsid(&(door_vfs.vfs_fsid), doordev, 0);
175 175
176 176 error = vn_make_ops("doorfs", door_vnodeops_template, &door_vnodeops);
177 177 if (error != 0) {
178 178 vfs_freevfsops(door_vfsops);
179 179 cmn_err(CE_WARN, "door init: bad vnode ops");
180 180 return (error);
181 181 }
182 182 return (mod_install(&modlinkage));
183 183 }
184 184
185 185 int
186 186 _info(struct modinfo *modinfop)
187 187 {
188 188 return (mod_info(&modlinkage, modinfop));
189 189 }
190 190
191 191 /* system call functions */
192 192 static int door_call(int, void *);
193 193 static int door_return(caddr_t, size_t, door_desc_t *, uint_t, caddr_t, size_t);
194 194 static int door_create(void (*pc_cookie)(void *, char *, size_t, door_desc_t *,
195 195 uint_t), void *data_cookie, uint_t);
196 196 static int door_revoke(int);
197 197 static int door_info(int, struct door_info *);
198 198 static int door_ucred(struct ucred_s *);
199 199 static int door_bind(int);
200 200 static int door_unbind(void);
201 201 static int door_unref(void);
202 202 static int door_getparam(int, int, size_t *);
203 203 static int door_setparam(int, int, size_t);
204 204
205 205 #define DOOR_RETURN_OLD 4 /* historic value, for s10 */
206 206
207 207 /*
208 208 * System call wrapper for all door related system calls
209 209 */
210 210 static int
211 211 doorfs(long arg1, long arg2, long arg3, long arg4, long arg5, long subcode)
212 212 {
213 213 switch (subcode) {
214 214 case DOOR_CALL:
215 215 return (door_call(arg1, (void *)arg2));
216 216 case DOOR_RETURN: {
217 217 door_return_desc_t *drdp = (door_return_desc_t *)arg3;
218 218
219 219 if (drdp != NULL) {
220 220 door_return_desc_t drd;
221 221 if (copyin(drdp, &drd, sizeof (drd)))
222 222 return (EFAULT);
223 223 return (door_return((caddr_t)arg1, arg2, drd.desc_ptr,
224 224 drd.desc_num, (caddr_t)arg4, arg5));
225 225 }
226 226 return (door_return((caddr_t)arg1, arg2, NULL,
227 227 0, (caddr_t)arg4, arg5));
228 228 }
229 229 case DOOR_RETURN_OLD:
230 230 /*
231 231 * In order to support the S10 runtime environment, we
232 232 * still respond to the old syscall subcode for door_return.
233 233 * We treat it as having no stack limits. This code should
234 234 * be removed when such support is no longer needed.
235 235 */
236 236 return (door_return((caddr_t)arg1, arg2, (door_desc_t *)arg3,
237 237 arg4, (caddr_t)arg5, 0));
238 238 case DOOR_CREATE:
239 239 return (door_create((void (*)())arg1, (void *)arg2, arg3));
240 240 case DOOR_REVOKE:
241 241 return (door_revoke(arg1));
242 242 case DOOR_INFO:
243 243 return (door_info(arg1, (struct door_info *)arg2));
244 244 case DOOR_BIND:
245 245 return (door_bind(arg1));
246 246 case DOOR_UNBIND:
247 247 return (door_unbind());
248 248 case DOOR_UNREFSYS:
249 249 return (door_unref());
250 250 case DOOR_UCRED:
251 251 return (door_ucred((struct ucred_s *)arg1));
252 252 case DOOR_GETPARAM:
253 253 return (door_getparam(arg1, arg2, (size_t *)arg3));
254 254 case DOOR_SETPARAM:
255 255 return (door_setparam(arg1, arg2, arg3));
256 256 default:
257 257 return (set_errno(EINVAL));
258 258 }
259 259 }
260 260
261 261 #ifdef _SYSCALL32_IMPL
262 262 /*
263 263 * System call wrapper for all door related system calls from 32-bit programs.
264 264 * Needed at the moment because of the casts - they undo some damage
265 265 * that truss causes (sign-extending the stack pointer) when truss'ing
266 266 * a 32-bit program using doors.
267 267 */
268 268 static int
269 269 doorfs32(int32_t arg1, int32_t arg2, int32_t arg3,
270 270 int32_t arg4, int32_t arg5, int32_t subcode)
271 271 {
272 272 switch (subcode) {
273 273 case DOOR_CALL:
274 274 return (door_call(arg1, (void *)(uintptr_t)(caddr32_t)arg2));
275 275 case DOOR_RETURN: {
276 276 door_return_desc32_t *drdp =
277 277 (door_return_desc32_t *)(uintptr_t)(caddr32_t)arg3;
278 278 if (drdp != NULL) {
279 279 door_return_desc32_t drd;
280 280 if (copyin(drdp, &drd, sizeof (drd)))
281 281 return (EFAULT);
282 282 return (door_return(
283 283 (caddr_t)(uintptr_t)(caddr32_t)arg1, arg2,
284 284 (door_desc_t *)(uintptr_t)drd.desc_ptr,
285 285 drd.desc_num, (caddr_t)(uintptr_t)(caddr32_t)arg4,
286 286 (size_t)(uintptr_t)(size32_t)arg5));
287 287 }
288 288 return (door_return((caddr_t)(uintptr_t)(caddr32_t)arg1,
289 289 arg2, NULL, 0, (caddr_t)(uintptr_t)(caddr32_t)arg4,
290 290 (size_t)(uintptr_t)(size32_t)arg5));
291 291 }
292 292 case DOOR_RETURN_OLD:
293 293 /*
294 294 * In order to support the S10 runtime environment, we
295 295 * still respond to the old syscall subcode for door_return.
296 296 * We treat it as having no stack limits. This code should
297 297 * be removed when such support is no longer needed.
298 298 */
299 299 return (door_return((caddr_t)(uintptr_t)(caddr32_t)arg1, arg2,
300 300 (door_desc_t *)(uintptr_t)(caddr32_t)arg3, arg4,
301 301 (caddr_t)(uintptr_t)(caddr32_t)arg5, 0));
302 302 case DOOR_CREATE:
303 303 return (door_create((void (*)())(uintptr_t)(caddr32_t)arg1,
304 304 (void *)(uintptr_t)(caddr32_t)arg2, arg3));
305 305 case DOOR_REVOKE:
306 306 return (door_revoke(arg1));
307 307 case DOOR_INFO:
308 308 return (door_info(arg1,
309 309 (struct door_info *)(uintptr_t)(caddr32_t)arg2));
310 310 case DOOR_BIND:
311 311 return (door_bind(arg1));
312 312 case DOOR_UNBIND:
313 313 return (door_unbind());
314 314 case DOOR_UNREFSYS:
315 315 return (door_unref());
316 316 case DOOR_UCRED:
317 317 return (door_ucred(
318 318 (struct ucred_s *)(uintptr_t)(caddr32_t)arg1));
319 319 case DOOR_GETPARAM:
320 320 return (door_getparam(arg1, arg2,
321 321 (size_t *)(uintptr_t)(caddr32_t)arg3));
322 322 case DOOR_SETPARAM:
323 323 return (door_setparam(arg1, arg2, (size_t)(size32_t)arg3));
324 324
325 325 default:
326 326 return (set_errno(EINVAL));
327 327 }
328 328 }
329 329 #endif
330 330
331 331 void shuttle_resume(kthread_t *, kmutex_t *);
332 332 void shuttle_swtch(kmutex_t *);
333 333 void shuttle_sleep(kthread_t *);
334 334
335 335 /*
336 336 * Support routines
337 337 */
338 338 static int door_create_common(void (*)(), void *, uint_t, int, int *,
339 339 file_t **);
340 340 static int door_overflow(kthread_t *, caddr_t, size_t, door_desc_t *, uint_t);
341 341 static int door_args(kthread_t *, int);
342 342 static int door_results(kthread_t *, caddr_t, size_t, door_desc_t *, uint_t);
343 343 static int door_copy(struct as *, caddr_t, caddr_t, uint_t);
344 344 static void door_server_exit(proc_t *, kthread_t *);
345 345 static void door_release_server(door_node_t *, kthread_t *);
346 346 static kthread_t *door_get_server(door_node_t *);
347 347 static door_node_t *door_lookup(int, file_t **);
348 348 static int door_translate_in(void);
349 349 static int door_translate_out(void);
350 350 static void door_fd_rele(door_desc_t *, uint_t, int);
351 351 static void door_list_insert(door_node_t *);
352 352 static void door_info_common(door_node_t *, door_info_t *, file_t *);
353 353 static int door_release_fds(door_desc_t *, uint_t);
354 354 static void door_fd_close(door_desc_t *, uint_t);
355 355 static void door_fp_close(struct file **, uint_t);
356 356
357 357 static door_data_t *
358 358 door_my_data(int create_if_missing)
359 359 {
360 360 door_data_t *ddp;
361 361
362 362 ddp = curthread->t_door;
363 363 if (create_if_missing && ddp == NULL)
364 364 ddp = curthread->t_door = kmem_zalloc(sizeof (*ddp), KM_SLEEP);
365 365
366 366 return (ddp);
367 367 }
368 368
369 369 static door_server_t *
370 370 door_my_server(int create_if_missing)
371 371 {
372 372 door_data_t *ddp = door_my_data(create_if_missing);
373 373
374 374 return ((ddp != NULL)? DOOR_SERVER(ddp) : NULL);
375 375 }
376 376
377 377 static door_client_t *
378 378 door_my_client(int create_if_missing)
379 379 {
380 380 door_data_t *ddp = door_my_data(create_if_missing);
381 381
382 382 return ((ddp != NULL)? DOOR_CLIENT(ddp) : NULL);
383 383 }
384 384
385 385 /*
386 386 * System call to create a door
387 387 */
388 388 int
389 389 door_create(void (*pc_cookie)(), void *data_cookie, uint_t attributes)
390 390 {
391 391 int fd;
392 392 int err;
393 393
394 394 if ((attributes & ~DOOR_CREATE_MASK) ||
395 395 ((attributes & (DOOR_UNREF | DOOR_UNREF_MULTI)) ==
396 396 (DOOR_UNREF | DOOR_UNREF_MULTI)))
397 397 return (set_errno(EINVAL));
398 398
399 399 if ((err = door_create_common(pc_cookie, data_cookie, attributes, 0,
400 400 &fd, NULL)) != 0)
401 401 return (set_errno(err));
402 402
403 403 f_setfd(fd, FD_CLOEXEC);
404 404 return (fd);
405 405 }
406 406
407 407 /*
408 408 * Common code for creating user and kernel doors. If a door was
409 409 * created, stores a file structure pointer in the location pointed
410 410 * to by fpp (if fpp is non-NULL) and returns 0. Also, if a non-NULL
411 411 * pointer to a file descriptor is passed in as fdp, allocates a file
412 412 * descriptor representing the door. If a door could not be created,
413 413 * returns an error.
414 414 */
415 415 static int
416 416 door_create_common(void (*pc_cookie)(), void *data_cookie, uint_t attributes,
417 417 int from_kernel, int *fdp, file_t **fpp)
418 418 {
419 419 door_node_t *dp;
420 420 vnode_t *vp;
421 421 struct file *fp;
422 422 static door_id_t index = 0;
423 423 proc_t *p = (from_kernel)? &p0 : curproc;
424 424
425 425 dp = kmem_zalloc(sizeof (door_node_t), KM_SLEEP);
426 426
427 427 dp->door_vnode = vn_alloc(KM_SLEEP);
428 428 dp->door_target = p;
429 429 dp->door_data = data_cookie;
430 430 dp->door_pc = pc_cookie;
431 431 dp->door_flags = attributes;
432 432 #ifdef _SYSCALL32_IMPL
433 433 if (!from_kernel && get_udatamodel() != DATAMODEL_NATIVE)
434 434 dp->door_data_max = UINT32_MAX;
435 435 else
436 436 #endif
437 437 dp->door_data_max = SIZE_MAX;
438 438 dp->door_data_min = 0UL;
439 439 dp->door_desc_max = (attributes & DOOR_REFUSE_DESC)? 0 : INT_MAX;
440 440
441 441 vp = DTOV(dp);
442 442 vn_setops(vp, door_vnodeops);
443 443 vp->v_type = VDOOR;
444 444 vp->v_vfsp = &door_vfs;
445 445 vp->v_data = (caddr_t)dp;
446 446 mutex_enter(&door_knob);
447 447 dp->door_index = index++;
448 448 /* add to per-process door list */
449 449 door_list_insert(dp);
450 450 mutex_exit(&door_knob);
451 451
452 452 if (falloc(vp, FREAD | FWRITE, &fp, fdp)) {
453 453 /*
454 454 * If the file table is full, remove the door from the
455 455 * per-process list, free the door, and return NULL.
456 456 */
457 457 mutex_enter(&door_knob);
458 458 door_list_delete(dp);
459 459 mutex_exit(&door_knob);
460 460 vn_free(vp);
461 461 kmem_free(dp, sizeof (door_node_t));
462 462 return (EMFILE);
463 463 }
464 464 vn_exists(vp);
465 465 if (fdp != NULL)
466 466 setf(*fdp, fp);
467 467 mutex_exit(&fp->f_tlock);
468 468
469 469 if (fpp != NULL)
470 470 *fpp = fp;
471 471 return (0);
472 472 }
473 473
474 474 static int
475 475 door_check_limits(door_node_t *dp, door_arg_t *da, int upcall)
476 476 {
477 477 ASSERT(MUTEX_HELD(&door_knob));
478 478
479 479 /* we allow unref upcalls through, despite any minimum */
480 480 if (da->data_size < dp->door_data_min &&
481 481 !(upcall && da->data_ptr == DOOR_UNREF_DATA))
482 482 return (ENOBUFS);
483 483
484 484 if (da->data_size > dp->door_data_max)
485 485 return (ENOBUFS);
486 486
487 487 if (da->desc_num > 0 && (dp->door_flags & DOOR_REFUSE_DESC))
488 488 return (ENOTSUP);
489 489
490 490 if (da->desc_num > dp->door_desc_max)
491 491 return (ENFILE);
492 492
493 493 return (0);
494 494 }
495 495
496 496 /*
497 497 * Door invocation.
498 498 */
499 499 int
500 500 door_call(int did, void *args)
501 501 {
502 502 /* Locals */
503 503 door_node_t *dp;
504 504 kthread_t *server_thread;
505 505 int error = 0;
506 506 klwp_t *lwp;
507 507 door_client_t *ct; /* curthread door_data */
508 508 door_server_t *st; /* server thread door_data */
509 509 door_desc_t *start = NULL;
510 510 uint_t ncopied = 0;
511 511 size_t dsize;
512 512 /* destructor for data returned by a kernel server */
513 513 void (*destfn)() = NULL;
514 514 void *destarg;
515 515 model_t datamodel;
516 516 int gotresults = 0;
517 517 int needcleanup = 0;
518 518 int cancel_pending;
519 519
520 520 lwp = ttolwp(curthread);
521 521 datamodel = lwp_getdatamodel(lwp);
522 522
523 523 ct = door_my_client(1);
524 524
525 525 /*
526 526 * Get the arguments
527 527 */
528 528 if (args) {
529 529 if (datamodel == DATAMODEL_NATIVE) {
530 530 if (copyin(args, &ct->d_args, sizeof (door_arg_t)) != 0)
531 531 return (set_errno(EFAULT));
532 532 } else {
533 533 door_arg32_t da32;
534 534
535 535 if (copyin(args, &da32, sizeof (door_arg32_t)) != 0)
536 536 return (set_errno(EFAULT));
537 537 ct->d_args.data_ptr =
538 538 (char *)(uintptr_t)da32.data_ptr;
539 539 ct->d_args.data_size = da32.data_size;
540 540 ct->d_args.desc_ptr =
541 541 (door_desc_t *)(uintptr_t)da32.desc_ptr;
542 542 ct->d_args.desc_num = da32.desc_num;
543 543 ct->d_args.rbuf =
544 544 (char *)(uintptr_t)da32.rbuf;
545 545 ct->d_args.rsize = da32.rsize;
546 546 }
547 547 } else {
548 548 /* No arguments, and no results allowed */
549 549 ct->d_noresults = 1;
550 550 ct->d_args.data_size = 0;
551 551 ct->d_args.desc_num = 0;
552 552 ct->d_args.rsize = 0;
553 553 }
554 554
555 555 if ((dp = door_lookup(did, NULL)) == NULL)
556 556 return (set_errno(EBADF));
557 557
558 558 /*
559 559 * We don't want to hold the door FD over the entire operation;
560 560 * instead, we put a hold on the door vnode and release the FD
561 561 * immediately
562 562 */
563 563 VN_HOLD(DTOV(dp));
564 564 releasef(did);
565 565
566 566 /*
567 567 * This should be done in shuttle_resume(), just before going to
568 568 * sleep, but we want to avoid overhead while holding door_knob.
569 569 * prstop() is just a no-op if we don't really go to sleep.
570 570 * We test not-kernel-address-space for the sake of clustering code.
571 571 */
572 572 if (lwp && lwp->lwp_nostop == 0 && curproc->p_as != &kas)
573 573 prstop(PR_REQUESTED, 0);
574 574
575 575 mutex_enter(&door_knob);
576 576 if (DOOR_INVALID(dp)) {
577 577 mutex_exit(&door_knob);
578 578 error = EBADF;
579 579 goto out;
580 580 }
581 581
582 582 /*
583 583 * before we do anything, check that we are not overflowing the
584 584 * required limits.
585 585 */
586 586 error = door_check_limits(dp, &ct->d_args, 0);
587 587 if (error != 0) {
588 588 mutex_exit(&door_knob);
589 589 goto out;
590 590 }
591 591
592 592 /*
593 593 * Check for in-kernel door server.
594 594 */
595 595 if (dp->door_target == &p0) {
596 596 caddr_t rbuf = ct->d_args.rbuf;
597 597 size_t rsize = ct->d_args.rsize;
598 598
599 599 dp->door_active++;
600 600 ct->d_kernel = 1;
601 601 ct->d_error = DOOR_WAIT;
602 602 mutex_exit(&door_knob);
603 603 /* translate file descriptors to vnodes */
604 604 if (ct->d_args.desc_num) {
605 605 error = door_translate_in();
606 606 if (error)
607 607 goto out;
608 608 }
609 609 /*
610 610 * Call kernel door server. Arguments are passed and
611 611 * returned as a door_arg pointer. When called, data_ptr
612 612 * points to user data and desc_ptr points to a kernel list
613 613 * of door descriptors that have been converted to file
614 614 * structure pointers. It's the server function's
615 615 * responsibility to copyin the data pointed to by data_ptr
616 616 * (this avoids extra copying in some cases). On return,
617 617 * data_ptr points to a user buffer of data, and desc_ptr
618 618 * points to a kernel list of door descriptors representing
619 619 * files. When a reference is passed to a kernel server,
620 620 * it is the server's responsibility to release the reference
621 621 * (by calling closef). When the server includes a
622 622 * reference in its reply, it is released as part of the
623 623 * the call (the server must duplicate the reference if
624 624 * it wants to retain a copy). The destfn, if set to
625 625 * non-NULL, is a destructor to be called when the returned
626 626 * kernel data (if any) is no longer needed (has all been
627 627 * translated and copied to user level).
628 628 */
629 629 (*(dp->door_pc))(dp->door_data, &ct->d_args,
630 630 &destfn, &destarg, &error);
631 631 mutex_enter(&door_knob);
632 632 /* not implemented yet */
633 633 if (--dp->door_active == 0 && (dp->door_flags & DOOR_DELAY))
634 634 door_deliver_unref(dp);
635 635 mutex_exit(&door_knob);
636 636 if (error)
637 637 goto out;
638 638
639 639 /* translate vnodes to files */
640 640 if (ct->d_args.desc_num) {
641 641 error = door_translate_out();
642 642 if (error)
643 643 goto out;
644 644 }
645 645 ct->d_buf = ct->d_args.rbuf;
646 646 ct->d_bufsize = ct->d_args.rsize;
647 647 if (rsize < (ct->d_args.data_size +
648 648 (ct->d_args.desc_num * sizeof (door_desc_t)))) {
649 649 /* handle overflow */
650 650 error = door_overflow(curthread, ct->d_args.data_ptr,
651 651 ct->d_args.data_size, ct->d_args.desc_ptr,
652 652 ct->d_args.desc_num);
653 653 if (error)
654 654 goto out;
655 655 /* door_overflow sets d_args rbuf and rsize */
656 656 } else {
657 657 ct->d_args.rbuf = rbuf;
658 658 ct->d_args.rsize = rsize;
659 659 }
660 660 goto results;
661 661 }
662 662
663 663 /*
664 664 * Get a server thread from the target domain
665 665 */
666 666 if ((server_thread = door_get_server(dp)) == NULL) {
667 667 if (DOOR_INVALID(dp))
668 668 error = EBADF;
669 669 else
670 670 error = EAGAIN;
671 671 mutex_exit(&door_knob);
672 672 goto out;
673 673 }
674 674
675 675 st = DOOR_SERVER(server_thread->t_door);
676 676 if (ct->d_args.desc_num || ct->d_args.data_size) {
677 677 int is_private = (dp->door_flags & DOOR_PRIVATE);
678 678 /*
679 679 * Move data from client to server
680 680 */
681 681 DOOR_T_HOLD(st);
682 682 mutex_exit(&door_knob);
683 683 error = door_args(server_thread, is_private);
684 684 mutex_enter(&door_knob);
685 685 DOOR_T_RELEASE(st);
686 686 if (error) {
687 687 /*
688 688 * We're not going to resume this thread after all
689 689 */
690 690 door_release_server(dp, server_thread);
691 691 shuttle_sleep(server_thread);
692 692 mutex_exit(&door_knob);
693 693 goto out;
694 694 }
695 695 }
696 696
697 697 dp->door_active++;
698 698 ct->d_error = DOOR_WAIT;
699 699 ct->d_args_done = 0;
700 700 st->d_caller = curthread;
701 701 st->d_active = dp;
702 702
703 703 shuttle_resume(server_thread, &door_knob);
704 704
705 705 mutex_enter(&door_knob);
706 706 shuttle_return:
707 707 if ((error = ct->d_error) < 0) { /* DOOR_WAIT or DOOR_EXIT */
708 708 /*
709 709 * Premature wakeup. Find out why (stop, forkall, sig, exit ...)
710 710 */
711 711 mutex_exit(&door_knob); /* May block in ISSIG */
712 712 cancel_pending = 0;
713 713 if (ISSIG(curthread, FORREAL) || lwp->lwp_sysabort ||
714 714 MUSTRETURN(curproc, curthread) ||
715 715 (cancel_pending = schedctl_cancel_pending()) != 0) {
716 716 /* Signal, forkall, ... */
717 717 lwp->lwp_sysabort = 0;
718 718 if (cancel_pending)
719 719 schedctl_cancel_eintr();
720 720 mutex_enter(&door_knob);
721 721 error = EINTR;
722 722 /*
723 723 * If the server has finished processing our call,
724 724 * or exited (calling door_slam()), then d_error
725 725 * will have changed. If the server hasn't finished
726 726 * yet, d_error will still be DOOR_WAIT, and we
727 727 * let it know we are not interested in any
728 728 * results by sending a SIGCANCEL, unless the door
729 729 * is marked with DOOR_NO_CANCEL.
730 730 */
731 731 if (ct->d_error == DOOR_WAIT &&
732 732 st->d_caller == curthread) {
733 733 proc_t *p = ttoproc(server_thread);
734 734
735 735 st->d_active = NULL;
736 736 st->d_caller = NULL;
737 737
738 738 if (!(dp->door_flags & DOOR_NO_CANCEL)) {
739 739 DOOR_T_HOLD(st);
740 740 mutex_exit(&door_knob);
741 741
742 742 mutex_enter(&p->p_lock);
743 743 sigtoproc(p, server_thread, SIGCANCEL);
744 744 mutex_exit(&p->p_lock);
745 745
746 746 mutex_enter(&door_knob);
747 747 DOOR_T_RELEASE(st);
748 748 }
749 749 }
750 750 } else {
751 751 /*
752 752 * Return from stop(), server exit...
753 753 *
754 754 * Note that the server could have done a
755 755 * door_return while the client was in stop state
756 756 * (ISSIG), in which case the error condition
757 757 * is updated by the server.
758 758 */
759 759 mutex_enter(&door_knob);
760 760 if (ct->d_error == DOOR_WAIT) {
761 761 /* Still waiting for a reply */
762 762 shuttle_swtch(&door_knob);
763 763 mutex_enter(&door_knob);
764 764 lwp->lwp_asleep = 0;
765 765 goto shuttle_return;
766 766 } else if (ct->d_error == DOOR_EXIT) {
767 767 /* Server exit */
768 768 error = EINTR;
769 769 } else {
770 770 /* Server did a door_return during ISSIG */
771 771 error = ct->d_error;
772 772 }
773 773 }
774 774 /*
775 775 * Can't exit if the server is currently copying
776 776 * results for me.
777 777 */
778 778 while (DOOR_T_HELD(ct))
779 779 cv_wait(&ct->d_cv, &door_knob);
780 780
781 781 /*
782 782 * If the server has not processed our message, free the
783 783 * descriptors.
784 784 */
785 785 if (!ct->d_args_done) {
786 786 needcleanup = 1;
787 787 ct->d_args_done = 1;
788 788 }
789 789
790 790 /*
791 791 * Find out if results were successfully copied.
792 792 */
793 793 if (ct->d_error == 0)
794 794 gotresults = 1;
795 795 }
796 796 ASSERT(ct->d_args_done);
797 797 lwp->lwp_asleep = 0; /* /proc */
798 798 lwp->lwp_sysabort = 0; /* /proc */
799 799 if (--dp->door_active == 0 && (dp->door_flags & DOOR_DELAY))
800 800 door_deliver_unref(dp);
801 801 mutex_exit(&door_knob);
802 802
803 803 if (needcleanup)
804 804 door_fp_close(ct->d_fpp, ct->d_args.desc_num);
805 805
806 806 results:
807 807 /*
808 808 * Move the results to userland (if any)
809 809 */
810 810
811 811 if (ct->d_noresults)
812 812 goto out;
813 813
814 814 if (error) {
815 815 /*
816 816 * If server returned results successfully, then we've
817 817 * been interrupted and may need to clean up.
818 818 */
819 819 if (gotresults) {
820 820 ASSERT(error == EINTR);
821 821 door_fp_close(ct->d_fpp, ct->d_args.desc_num);
822 822 }
823 823 goto out;
824 824 }
825 825
826 826 /*
827 827 * Copy back data if we haven't caused an overflow (already
828 828 * handled) and we are using a 2 copy transfer, or we are
829 829 * returning data from a kernel server.
830 830 */
831 831 if (ct->d_args.data_size) {
832 832 ct->d_args.data_ptr = ct->d_args.rbuf;
833 833 if (ct->d_kernel || (!ct->d_overflow &&
834 834 ct->d_args.data_size <= door_max_arg)) {
835 835 if (copyout_nowatch(ct->d_buf, ct->d_args.rbuf,
836 836 ct->d_args.data_size)) {
837 837 door_fp_close(ct->d_fpp, ct->d_args.desc_num);
838 838 error = EFAULT;
839 839 goto out;
840 840 }
841 841 }
842 842 }
843 843
844 844 /*
845 845 * stuff returned doors into our proc, copyout the descriptors
846 846 */
847 847 if (ct->d_args.desc_num) {
848 848 struct file **fpp;
849 849 door_desc_t *didpp;
850 850 uint_t n = ct->d_args.desc_num;
851 851
852 852 dsize = n * sizeof (door_desc_t);
853 853 start = didpp = kmem_alloc(dsize, KM_SLEEP);
854 854 fpp = ct->d_fpp;
855 855
856 856 while (n--) {
857 857 if (door_insert(*fpp, didpp) == -1) {
858 858 /* Close remaining files */
859 859 door_fp_close(fpp, n + 1);
860 860 error = EMFILE;
861 861 goto out;
862 862 }
863 863 fpp++; didpp++; ncopied++;
864 864 }
865 865
866 866 ct->d_args.desc_ptr = (door_desc_t *)(ct->d_args.rbuf +
867 867 roundup(ct->d_args.data_size, sizeof (door_desc_t)));
868 868
869 869 if (copyout_nowatch(start, ct->d_args.desc_ptr, dsize)) {
870 870 error = EFAULT;
871 871 goto out;
872 872 }
873 873 }
874 874
875 875 /*
876 876 * Return the results
877 877 */
878 878 if (datamodel == DATAMODEL_NATIVE) {
879 879 if (copyout_nowatch(&ct->d_args, args,
880 880 sizeof (door_arg_t)) != 0)
881 881 error = EFAULT;
882 882 } else {
883 883 door_arg32_t da32;
884 884
885 885 da32.data_ptr = (caddr32_t)(uintptr_t)ct->d_args.data_ptr;
886 886 da32.data_size = ct->d_args.data_size;
887 887 da32.desc_ptr = (caddr32_t)(uintptr_t)ct->d_args.desc_ptr;
888 888 da32.desc_num = ct->d_args.desc_num;
889 889 da32.rbuf = (caddr32_t)(uintptr_t)ct->d_args.rbuf;
890 890 da32.rsize = ct->d_args.rsize;
891 891 if (copyout_nowatch(&da32, args, sizeof (door_arg32_t)) != 0) {
892 892 error = EFAULT;
893 893 }
894 894 }
895 895
896 896 out:
897 897 ct->d_noresults = 0;
898 898
899 899 /* clean up the overflow buffer if an error occurred */
900 900 if (error != 0 && ct->d_overflow) {
901 901 (void) as_unmap(curproc->p_as, ct->d_args.rbuf,
902 902 ct->d_args.rsize);
903 903 }
904 904 ct->d_overflow = 0;
905 905
906 906 /* call destructor */
907 907 if (destfn) {
908 908 ASSERT(ct->d_kernel);
909 909 (*destfn)(dp->door_data, destarg);
910 910 ct->d_buf = NULL;
911 911 ct->d_bufsize = 0;
912 912 }
913 913
914 914 if (dp)
915 915 VN_RELE(DTOV(dp));
916 916
917 917 if (ct->d_buf) {
918 918 ASSERT(!ct->d_kernel);
919 919 kmem_free(ct->d_buf, ct->d_bufsize);
920 920 ct->d_buf = NULL;
921 921 ct->d_bufsize = 0;
922 922 }
923 923 ct->d_kernel = 0;
924 924
925 925 /* clean up the descriptor copyout buffer */
926 926 if (start != NULL) {
927 927 if (error != 0)
928 928 door_fd_close(start, ncopied);
929 929 kmem_free(start, dsize);
930 930 }
931 931
932 932 if (ct->d_fpp) {
933 933 kmem_free(ct->d_fpp, ct->d_fpp_size);
934 934 ct->d_fpp = NULL;
935 935 ct->d_fpp_size = 0;
936 936 }
937 937
938 938 if (error)
939 939 return (set_errno(error));
940 940
941 941 return (0);
942 942 }
943 943
944 944 static int
945 945 door_setparam_common(door_node_t *dp, int from_kernel, int type, size_t val)
946 946 {
947 947 int error = 0;
948 948
949 949 mutex_enter(&door_knob);
950 950
951 951 if (DOOR_INVALID(dp)) {
952 952 mutex_exit(&door_knob);
953 953 return (EBADF);
954 954 }
955 955
956 956 /*
957 957 * door_ki_setparam() can only affect kernel doors.
958 958 * door_setparam() can only affect doors attached to the current
959 959 * process.
960 960 */
961 961 if ((from_kernel && dp->door_target != &p0) ||
962 962 (!from_kernel && dp->door_target != curproc)) {
963 963 mutex_exit(&door_knob);
964 964 return (EPERM);
965 965 }
966 966
967 967 switch (type) {
968 968 case DOOR_PARAM_DESC_MAX:
969 969 if (val > INT_MAX)
970 970 error = ERANGE;
971 971 else if ((dp->door_flags & DOOR_REFUSE_DESC) && val != 0)
972 972 error = ENOTSUP;
973 973 else
974 974 dp->door_desc_max = (uint_t)val;
975 975 break;
976 976
977 977 case DOOR_PARAM_DATA_MIN:
978 978 if (val > dp->door_data_max)
979 979 error = EINVAL;
980 980 else
981 981 dp->door_data_min = val;
982 982 break;
983 983
984 984 case DOOR_PARAM_DATA_MAX:
985 985 if (val < dp->door_data_min)
986 986 error = EINVAL;
987 987 else
988 988 dp->door_data_max = val;
989 989 break;
990 990
991 991 default:
992 992 error = EINVAL;
993 993 break;
994 994 }
995 995
996 996 mutex_exit(&door_knob);
997 997 return (error);
998 998 }
999 999
1000 1000 static int
1001 1001 door_getparam_common(door_node_t *dp, int type, size_t *out)
1002 1002 {
1003 1003 int error = 0;
1004 1004
1005 1005 mutex_enter(&door_knob);
1006 1006 switch (type) {
1007 1007 case DOOR_PARAM_DESC_MAX:
1008 1008 *out = (size_t)dp->door_desc_max;
1009 1009 break;
1010 1010 case DOOR_PARAM_DATA_MIN:
1011 1011 *out = dp->door_data_min;
1012 1012 break;
1013 1013 case DOOR_PARAM_DATA_MAX:
1014 1014 *out = dp->door_data_max;
1015 1015 break;
1016 1016 default:
1017 1017 error = EINVAL;
1018 1018 break;
1019 1019 }
1020 1020 mutex_exit(&door_knob);
1021 1021 return (error);
1022 1022 }
1023 1023
1024 1024 int
1025 1025 door_setparam(int did, int type, size_t val)
1026 1026 {
1027 1027 door_node_t *dp;
1028 1028 int error = 0;
1029 1029
1030 1030 if ((dp = door_lookup(did, NULL)) == NULL)
1031 1031 return (set_errno(EBADF));
1032 1032
1033 1033 error = door_setparam_common(dp, 0, type, val);
1034 1034
1035 1035 releasef(did);
1036 1036
1037 1037 if (error)
1038 1038 return (set_errno(error));
1039 1039
1040 1040 return (0);
1041 1041 }
1042 1042
1043 1043 int
1044 1044 door_getparam(int did, int type, size_t *out)
1045 1045 {
1046 1046 door_node_t *dp;
1047 1047 size_t val = 0;
1048 1048 int error = 0;
1049 1049
1050 1050 if ((dp = door_lookup(did, NULL)) == NULL)
1051 1051 return (set_errno(EBADF));
1052 1052
1053 1053 error = door_getparam_common(dp, type, &val);
1054 1054
1055 1055 releasef(did);
1056 1056
1057 1057 if (error)
1058 1058 return (set_errno(error));
1059 1059
1060 1060 if (get_udatamodel() == DATAMODEL_NATIVE) {
1061 1061 if (copyout(&val, out, sizeof (val)))
1062 1062 return (set_errno(EFAULT));
1063 1063 #ifdef _SYSCALL32_IMPL
1064 1064 } else {
1065 1065 size32_t val32 = (size32_t)val;
1066 1066
1067 1067 if (val != val32)
1068 1068 return (set_errno(EOVERFLOW));
1069 1069
1070 1070 if (copyout(&val32, out, sizeof (val32)))
1071 1071 return (set_errno(EFAULT));
1072 1072 #endif /* _SYSCALL32_IMPL */
1073 1073 }
1074 1074
1075 1075 return (0);
1076 1076 }
1077 1077
1078 1078 /*
1079 1079 * A copyout() which proceeds from high addresses to low addresses. This way,
1080 1080 * stack guard pages are effective.
1081 1081 *
1082 1082 * Note that we use copyout_nowatch(); this is called while the client is
1083 1083 * held.
1084 1084 */
1085 1085 static int
1086 1086 door_stack_copyout(const void *kaddr, void *uaddr, size_t count)
1087 1087 {
1088 1088 const char *kbase = (const char *)kaddr;
1089 1089 uintptr_t ubase = (uintptr_t)uaddr;
1090 1090 size_t pgsize = PAGESIZE;
1091 1091
1092 1092 if (count <= pgsize)
1093 1093 return (copyout_nowatch(kaddr, uaddr, count));
1094 1094
1095 1095 while (count > 0) {
1096 1096 uintptr_t start, end, offset, amount;
1097 1097
1098 1098 end = ubase + count;
1099 1099 start = P2ALIGN(end - 1, pgsize);
1100 1100 if (P2ALIGN(ubase, pgsize) == start)
1101 1101 start = ubase;
1102 1102
1103 1103 offset = start - ubase;
1104 1104 amount = end - start;
1105 1105
1106 1106 ASSERT(amount > 0 && amount <= count && amount <= pgsize);
1107 1107
1108 1108 if (copyout_nowatch(kbase + offset, (void *)start, amount))
1109 1109 return (1);
1110 1110 count -= amount;
1111 1111 }
1112 1112 return (0);
1113 1113 }
1114 1114
1115 1115 /*
1116 1116 * Writes the stack layout for door_return() into the door_server_t of the
1117 1117 * server thread.
1118 1118 */
1119 1119 static int
1120 1120 door_layout(kthread_t *tp, size_t data_size, uint_t ndesc, int info_needed)
1121 1121 {
1122 1122 door_server_t *st = DOOR_SERVER(tp->t_door);
1123 1123 door_layout_t *out = &st->d_layout;
1124 1124 uintptr_t base_sp = (uintptr_t)st->d_sp;
1125 1125 size_t ssize = st->d_ssize;
1126 1126 size_t descsz;
1127 1127 uintptr_t descp, datap, infop, resultsp, finalsp;
1128 1128 size_t align = STACK_ALIGN;
1129 1129 size_t results_sz = sizeof (struct door_results);
1130 1130 model_t datamodel = lwp_getdatamodel(ttolwp(tp));
1131 1131
1132 1132 ASSERT(!st->d_layout_done);
1133 1133
1134 1134 #ifndef _STACK_GROWS_DOWNWARD
1135 1135 #error stack does not grow downward, door_layout() must change
1136 1136 #endif
1137 1137
1138 1138 #ifdef _SYSCALL32_IMPL
1139 1139 if (datamodel != DATAMODEL_NATIVE) {
1140 1140 align = STACK_ALIGN32;
1141 1141 results_sz = sizeof (struct door_results32);
1142 1142 }
1143 1143 #endif
1144 1144
1145 1145 descsz = ndesc * sizeof (door_desc_t);
1146 1146
1147 1147 /*
1148 1148 * To speed up the overflow checking, we do an initial check
1149 1149 * that the passed in data size won't cause us to wrap past
1150 1150 * base_sp. Since door_max_desc limits descsz, we can
1151 1151 * safely use it here. 65535 is an arbitrary 'bigger than
1152 1152 * we need, small enough to not cause trouble' constant;
1153 1153 * the only constraint is that it must be > than:
1154 1154 *
1155 1155 * 5 * STACK_ALIGN +
1156 1156 * sizeof (door_info_t) +
1157 1157 * sizeof (door_results_t) +
1158 1158 * (max adjustment from door_final_sp())
1159 1159 *
1160 1160 * After we compute the layout, we can safely do a "did we wrap
1161 1161 * around" check, followed by a check against the recorded
1162 1162 * stack size.
1163 1163 */
1164 1164 if (data_size >= SIZE_MAX - (size_t)65535UL - descsz)
1165 1165 return (E2BIG); /* overflow */
1166 1166
1167 1167 descp = P2ALIGN(base_sp - descsz, align);
1168 1168 datap = P2ALIGN(descp - data_size, align);
1169 1169
1170 1170 if (info_needed)
1171 1171 infop = P2ALIGN(datap - sizeof (door_info_t), align);
1172 1172 else
1173 1173 infop = datap;
1174 1174
1175 1175 resultsp = P2ALIGN(infop - results_sz, align);
1176 1176 finalsp = door_final_sp(resultsp, align, datamodel);
1177 1177
1178 1178 if (finalsp > base_sp)
1179 1179 return (E2BIG); /* overflow */
1180 1180
1181 1181 if (ssize != 0 && (base_sp - finalsp) > ssize)
1182 1182 return (E2BIG); /* doesn't fit in stack */
1183 1183
1184 1184 out->dl_descp = (ndesc != 0)? (caddr_t)descp : 0;
1185 1185 out->dl_datap = (data_size != 0)? (caddr_t)datap : 0;
1186 1186 out->dl_infop = info_needed? (caddr_t)infop : 0;
1187 1187 out->dl_resultsp = (caddr_t)resultsp;
1188 1188 out->dl_sp = (caddr_t)finalsp;
1189 1189
1190 1190 st->d_layout_done = 1;
1191 1191 return (0);
1192 1192 }
1193 1193
1194 1194 static int
1195 1195 door_server_dispatch(door_client_t *ct, door_node_t *dp)
1196 1196 {
1197 1197 door_server_t *st = DOOR_SERVER(curthread->t_door);
1198 1198 door_layout_t *layout = &st->d_layout;
1199 1199 int error = 0;
1200 1200
1201 1201 int is_private = (dp->door_flags & DOOR_PRIVATE);
1202 1202
1203 1203 door_pool_t *pool = (is_private)? &dp->door_servers :
1204 1204 &curproc->p_server_threads;
1205 1205
1206 1206 int empty_pool = (pool->dp_threads == NULL);
1207 1207
1208 1208 caddr_t infop = NULL;
1209 1209 char *datap = NULL;
1210 1210 size_t datasize = 0;
1211 1211 size_t descsize;
1212 1212
1213 1213 file_t **fpp = ct->d_fpp;
1214 1214 door_desc_t *start = NULL;
1215 1215 uint_t ndesc = 0;
1216 1216 uint_t ncopied = 0;
1217 1217
1218 1218 if (ct != NULL) {
1219 1219 datap = ct->d_args.data_ptr;
1220 1220 datasize = ct->d_args.data_size;
1221 1221 ndesc = ct->d_args.desc_num;
1222 1222 }
1223 1223
1224 1224 descsize = ndesc * sizeof (door_desc_t);
1225 1225
1226 1226 /*
1227 1227 * Reset datap to NULL if we aren't passing any data. Be careful
1228 1228 * to let unref notifications through, though.
1229 1229 */
1230 1230 if (datap == DOOR_UNREF_DATA) {
1231 1231 if (ct->d_upcall != NULL)
1232 1232 datasize = 0;
1233 1233 else
1234 1234 datap = NULL;
1235 1235 } else if (datasize == 0) {
1236 1236 datap = NULL;
1237 1237 }
1238 1238
1239 1239 /*
1240 1240 * Get the stack layout, if it hasn't already been done.
1241 1241 */
1242 1242 if (!st->d_layout_done) {
1243 1243 error = door_layout(curthread, datasize, ndesc,
1244 1244 (is_private && empty_pool));
1245 1245 if (error != 0)
1246 1246 goto fail;
1247 1247 }
1248 1248
1249 1249 /*
1250 1250 * fill out the stack, starting from the top. Layout was already
1251 1251 * filled in by door_args() or door_translate_out().
1252 1252 */
1253 1253 if (layout->dl_descp != NULL) {
1254 1254 ASSERT(ndesc != 0);
1255 1255 start = kmem_alloc(descsize, KM_SLEEP);
1256 1256
1257 1257 while (ndesc > 0) {
1258 1258 if (door_insert(*fpp, &start[ncopied]) == -1) {
1259 1259 error = EMFILE;
1260 1260 goto fail;
1261 1261 }
1262 1262 ndesc--;
1263 1263 ncopied++;
1264 1264 fpp++;
1265 1265 }
1266 1266 if (door_stack_copyout(start, layout->dl_descp, descsize)) {
1267 1267 error = E2BIG;
1268 1268 goto fail;
1269 1269 }
1270 1270 }
1271 1271 fpp = NULL; /* finished processing */
1272 1272
1273 1273 if (layout->dl_datap != NULL) {
1274 1274 ASSERT(datasize != 0);
1275 1275 datap = layout->dl_datap;
1276 1276 if (ct->d_upcall != NULL || datasize <= door_max_arg) {
1277 1277 if (door_stack_copyout(ct->d_buf, datap, datasize)) {
1278 1278 error = E2BIG;
1279 1279 goto fail;
1280 1280 }
1281 1281 }
1282 1282 }
1283 1283
1284 1284 if (is_private && empty_pool) {
1285 1285 door_info_t di;
1286 1286
1287 1287 infop = layout->dl_infop;
1288 1288 ASSERT(infop != NULL);
1289 1289
1290 1290 di.di_target = curproc->p_pid;
1291 1291 di.di_proc = (door_ptr_t)(uintptr_t)dp->door_pc;
1292 1292 di.di_data = (door_ptr_t)(uintptr_t)dp->door_data;
1293 1293 di.di_uniquifier = dp->door_index;
1294 1294 di.di_attributes = (dp->door_flags & DOOR_ATTR_MASK) |
1295 1295 DOOR_LOCAL;
1296 1296
1297 1297 if (door_stack_copyout(&di, infop, sizeof (di))) {
1298 1298 error = E2BIG;
1299 1299 goto fail;
1300 1300 }
1301 1301 }
1302 1302
1303 1303 if (get_udatamodel() == DATAMODEL_NATIVE) {
1304 1304 struct door_results dr;
1305 1305
1306 1306 dr.cookie = dp->door_data;
1307 1307 dr.data_ptr = datap;
1308 1308 dr.data_size = datasize;
1309 1309 dr.desc_ptr = (door_desc_t *)layout->dl_descp;
1310 1310 dr.desc_num = ncopied;
1311 1311 dr.pc = dp->door_pc;
1312 1312 dr.nservers = !empty_pool;
1313 1313 dr.door_info = (door_info_t *)infop;
1314 1314
1315 1315 if (door_stack_copyout(&dr, layout->dl_resultsp, sizeof (dr))) {
1316 1316 error = E2BIG;
1317 1317 goto fail;
1318 1318 }
1319 1319 #ifdef _SYSCALL32_IMPL
1320 1320 } else {
1321 1321 struct door_results32 dr32;
1322 1322
1323 1323 dr32.cookie = (caddr32_t)(uintptr_t)dp->door_data;
1324 1324 dr32.data_ptr = (caddr32_t)(uintptr_t)datap;
1325 1325 dr32.data_size = (size32_t)datasize;
1326 1326 dr32.desc_ptr = (caddr32_t)(uintptr_t)layout->dl_descp;
1327 1327 dr32.desc_num = ncopied;
1328 1328 dr32.pc = (caddr32_t)(uintptr_t)dp->door_pc;
1329 1329 dr32.nservers = !empty_pool;
1330 1330 dr32.door_info = (caddr32_t)(uintptr_t)infop;
1331 1331
1332 1332 if (door_stack_copyout(&dr32, layout->dl_resultsp,
1333 1333 sizeof (dr32))) {
1334 1334 error = E2BIG;
1335 1335 goto fail;
1336 1336 }
1337 1337 #endif
1338 1338 }
1339 1339
1340 1340 error = door_finish_dispatch(layout->dl_sp);
1341 1341 fail:
1342 1342 if (start != NULL) {
1343 1343 if (error != 0)
1344 1344 door_fd_close(start, ncopied);
1345 1345 kmem_free(start, descsize);
1346 1346 }
1347 1347 if (fpp != NULL)
1348 1348 door_fp_close(fpp, ndesc);
1349 1349
1350 1350 return (error);
1351 1351 }
1352 1352
1353 1353 /*
1354 1354 * Return the results (if any) to the caller (if any) and wait for the
1355 1355 * next invocation on a door.
1356 1356 */
1357 1357 int
1358 1358 door_return(caddr_t data_ptr, size_t data_size,
1359 1359 door_desc_t *desc_ptr, uint_t desc_num, caddr_t sp, size_t ssize)
1360 1360 {
1361 1361 kthread_t *caller;
1362 1362 klwp_t *lwp;
1363 1363 int error = 0;
1364 1364 door_node_t *dp;
1365 1365 door_server_t *st; /* curthread door_data */
1366 1366 door_client_t *ct; /* caller door_data */
1367 1367 int cancel_pending;
1368 1368
1369 1369 st = door_my_server(1);
1370 1370
1371 1371 /*
1372 1372 * If thread was bound to a door that no longer exists, return
1373 1373 * an error. This can happen if a thread is bound to a door
1374 1374 * before the process calls forkall(); in the child, the door
1375 1375 * doesn't exist and door_fork() sets the d_invbound flag.
1376 1376 */
1377 1377 if (st->d_invbound)
1378 1378 return (set_errno(EINVAL));
1379 1379
1380 1380 st->d_sp = sp; /* Save base of stack. */
1381 1381 st->d_ssize = ssize; /* and its size */
1382 1382
1383 1383 /*
1384 1384 * This should be done in shuttle_resume(), just before going to
1385 1385 * sleep, but we want to avoid overhead while holding door_knob.
1386 1386 * prstop() is just a no-op if we don't really go to sleep.
1387 1387 * We test not-kernel-address-space for the sake of clustering code.
1388 1388 */
1389 1389 lwp = ttolwp(curthread);
1390 1390 if (lwp && lwp->lwp_nostop == 0 && curproc->p_as != &kas)
1391 1391 prstop(PR_REQUESTED, 0);
1392 1392
1393 1393 /* Make sure the caller hasn't gone away */
1394 1394 mutex_enter(&door_knob);
1395 1395 if ((caller = st->d_caller) == NULL || caller->t_door == NULL) {
1396 1396 if (desc_num != 0) {
1397 1397 /* close any DOOR_RELEASE descriptors */
1398 1398 mutex_exit(&door_knob);
1399 1399 error = door_release_fds(desc_ptr, desc_num);
1400 1400 if (error)
1401 1401 return (set_errno(error));
1402 1402 mutex_enter(&door_knob);
1403 1403 }
1404 1404 goto out;
1405 1405 }
1406 1406 ct = DOOR_CLIENT(caller->t_door);
1407 1407
1408 1408 ct->d_args.data_size = data_size;
1409 1409 ct->d_args.desc_num = desc_num;
1410 1410 /*
1411 1411 * Transfer results, if any, to the client
1412 1412 */
1413 1413 if (data_size != 0 || desc_num != 0) {
1414 1414 /*
1415 1415 * Prevent the client from exiting until we have finished
1416 1416 * moving results.
1417 1417 */
1418 1418 DOOR_T_HOLD(ct);
1419 1419 mutex_exit(&door_knob);
1420 1420 error = door_results(caller, data_ptr, data_size,
1421 1421 desc_ptr, desc_num);
1422 1422 mutex_enter(&door_knob);
1423 1423 DOOR_T_RELEASE(ct);
1424 1424 /*
1425 1425 * Pass EOVERFLOW errors back to the client
1426 1426 */
1427 1427 if (error && error != EOVERFLOW) {
1428 1428 mutex_exit(&door_knob);
1429 1429 return (set_errno(error));
1430 1430 }
1431 1431 }
1432 1432 out:
1433 1433 /* Put ourselves on the available server thread list */
1434 1434 door_release_server(st->d_pool, curthread);
1435 1435
1436 1436 /*
1437 1437 * Make sure the caller is still waiting to be resumed
1438 1438 */
1439 1439 if (caller) {
1440 1440 disp_lock_t *tlp;
1441 1441
1442 1442 thread_lock(caller);
1443 1443 ct->d_error = error; /* Return any errors */
1444 1444 if (caller->t_state == TS_SLEEP &&
1445 1445 SOBJ_TYPE(caller->t_sobj_ops) == SOBJ_SHUTTLE) {
1446 1446 cpu_t *cp = CPU;
1447 1447
1448 1448 tlp = caller->t_lockp;
1449 1449 /*
1450 1450 * Setting t_disp_queue prevents erroneous preemptions
1451 1451 * if this thread is still in execution on another
1452 1452 * processor
1453 1453 */
1454 1454 caller->t_disp_queue = cp->cpu_disp;
1455 1455 CL_ACTIVE(caller);
1456 1456 /*
1457 1457 * We are calling thread_onproc() instead of
1458 1458 * THREAD_ONPROC() because compiler can reorder
1459 1459 * the two stores of t_state and t_lockp in
1460 1460 * THREAD_ONPROC().
1461 1461 */
1462 1462 thread_onproc(caller, cp);
1463 1463 disp_lock_exit_high(tlp);
1464 1464 shuttle_resume(caller, &door_knob);
1465 1465 } else {
1466 1466 /* May have been setrun or in stop state */
1467 1467 thread_unlock(caller);
1468 1468 shuttle_swtch(&door_knob);
1469 1469 }
1470 1470 } else {
1471 1471 shuttle_swtch(&door_knob);
1472 1472 }
1473 1473
1474 1474 /*
1475 1475 * We've sprung to life. Determine if we are part of a door
1476 1476 * invocation, or just interrupted
1477 1477 */
1478 1478 mutex_enter(&door_knob);
1479 1479 if ((dp = st->d_active) != NULL) {
1480 1480 /*
1481 1481 * Normal door invocation. Return any error condition
1482 1482 * encountered while trying to pass args to the server
1483 1483 * thread.
1484 1484 */
1485 1485 lwp->lwp_asleep = 0;
1486 1486 /*
1487 1487 * Prevent the caller from leaving us while we
1488 1488 * are copying out the arguments from it's buffer.
1489 1489 */
1490 1490 ASSERT(st->d_caller != NULL);
1491 1491 ct = DOOR_CLIENT(st->d_caller->t_door);
1492 1492
1493 1493 DOOR_T_HOLD(ct);
1494 1494 mutex_exit(&door_knob);
1495 1495 error = door_server_dispatch(ct, dp);
1496 1496 mutex_enter(&door_knob);
1497 1497 DOOR_T_RELEASE(ct);
1498 1498
1499 1499 /* let the client know we have processed his message */
1500 1500 ct->d_args_done = 1;
1501 1501
1502 1502 if (error) {
1503 1503 caller = st->d_caller;
1504 1504 if (caller)
1505 1505 ct = DOOR_CLIENT(caller->t_door);
1506 1506 else
1507 1507 ct = NULL;
1508 1508 goto out;
1509 1509 }
1510 1510 mutex_exit(&door_knob);
1511 1511 return (0);
1512 1512 } else {
1513 1513 /*
1514 1514 * We are not involved in a door_invocation.
1515 1515 * Check for /proc related activity...
1516 1516 */
1517 1517 st->d_caller = NULL;
1518 1518 door_server_exit(curproc, curthread);
1519 1519 mutex_exit(&door_knob);
1520 1520 cancel_pending = 0;
1521 1521 if (ISSIG(curthread, FORREAL) || lwp->lwp_sysabort ||
1522 1522 MUSTRETURN(curproc, curthread) ||
1523 1523 (cancel_pending = schedctl_cancel_pending()) != 0) {
1524 1524 if (cancel_pending)
1525 1525 schedctl_cancel_eintr();
1526 1526 lwp->lwp_asleep = 0;
1527 1527 lwp->lwp_sysabort = 0;
1528 1528 return (set_errno(EINTR));
1529 1529 }
1530 1530 /* Go back and wait for another request */
1531 1531 lwp->lwp_asleep = 0;
1532 1532 mutex_enter(&door_knob);
1533 1533 caller = NULL;
1534 1534 goto out;
1535 1535 }
1536 1536 }
1537 1537
1538 1538 /*
1539 1539 * Revoke any future invocations on this door
1540 1540 */
1541 1541 int
1542 1542 door_revoke(int did)
1543 1543 {
1544 1544 door_node_t *d;
1545 1545 int error;
1546 1546
1547 1547 if ((d = door_lookup(did, NULL)) == NULL)
1548 1548 return (set_errno(EBADF));
1549 1549
1550 1550 mutex_enter(&door_knob);
1551 1551 if (d->door_target != curproc) {
1552 1552 mutex_exit(&door_knob);
1553 1553 releasef(did);
1554 1554 return (set_errno(EPERM));
1555 1555 }
1556 1556 d->door_flags |= DOOR_REVOKED;
1557 1557 if (d->door_flags & DOOR_PRIVATE)
1558 1558 cv_broadcast(&d->door_servers.dp_cv);
1559 1559 else
1560 1560 cv_broadcast(&curproc->p_server_threads.dp_cv);
1561 1561 mutex_exit(&door_knob);
1562 1562 releasef(did);
1563 1563 /* Invalidate the descriptor */
1564 1564 if ((error = closeandsetf(did, NULL)) != 0)
1565 1565 return (set_errno(error));
1566 1566 return (0);
1567 1567 }
1568 1568
1569 1569 int
1570 1570 door_info(int did, struct door_info *d_info)
1571 1571 {
1572 1572 door_node_t *dp;
1573 1573 door_info_t di;
1574 1574 door_server_t *st;
1575 1575 file_t *fp = NULL;
1576 1576
1577 1577 if (did == DOOR_QUERY) {
1578 1578 /* Get information on door current thread is bound to */
1579 1579 if ((st = door_my_server(0)) == NULL ||
1580 1580 (dp = st->d_pool) == NULL)
1581 1581 /* Thread isn't bound to a door */
1582 1582 return (set_errno(EBADF));
1583 1583 } else if ((dp = door_lookup(did, &fp)) == NULL) {
1584 1584 /* Not a door */
1585 1585 return (set_errno(EBADF));
1586 1586 }
1587 1587
1588 1588 door_info_common(dp, &di, fp);
1589 1589
1590 1590 if (did != DOOR_QUERY)
1591 1591 releasef(did);
1592 1592
1593 1593 if (copyout(&di, d_info, sizeof (struct door_info)))
1594 1594 return (set_errno(EFAULT));
1595 1595 return (0);
1596 1596 }
1597 1597
1598 1598 /*
1599 1599 * Common code for getting information about a door either via the
1600 1600 * door_info system call or the door_ki_info kernel call.
1601 1601 */
1602 1602 void
1603 1603 door_info_common(door_node_t *dp, struct door_info *dip, file_t *fp)
1604 1604 {
1605 1605 int unref_count;
1606 1606
1607 1607 bzero(dip, sizeof (door_info_t));
1608 1608
1609 1609 mutex_enter(&door_knob);
1610 1610 if (dp->door_target == NULL)
1611 1611 dip->di_target = -1;
1612 1612 else
1613 1613 dip->di_target = dp->door_target->p_pid;
1614 1614
1615 1615 dip->di_attributes = dp->door_flags & DOOR_ATTR_MASK;
1616 1616 if (dp->door_target == curproc)
1617 1617 dip->di_attributes |= DOOR_LOCAL;
1618 1618 dip->di_proc = (door_ptr_t)(uintptr_t)dp->door_pc;
1619 1619 dip->di_data = (door_ptr_t)(uintptr_t)dp->door_data;
1620 1620 dip->di_uniquifier = dp->door_index;
1621 1621 /*
1622 1622 * If this door is in the middle of having an unreferenced
1623 1623 * notification delivered, don't count the VN_HOLD by
1624 1624 * door_deliver_unref in determining if it is unreferenced.
1625 1625 * This handles the case where door_info is called from the
1626 1626 * thread delivering the unref notification.
1627 1627 */
1628 1628 if (dp->door_flags & DOOR_UNREF_ACTIVE)
1629 1629 unref_count = 2;
1630 1630 else
1631 1631 unref_count = 1;
1632 1632 mutex_exit(&door_knob);
1633 1633
1634 1634 if (fp == NULL) {
1635 1635 /*
1636 1636 * If this thread is bound to the door, then we can just
1637 1637 * check the vnode; a ref count of 1 (or 2 if this is
1638 1638 * handling an unref notification) means that the hold
1639 1639 * from the door_bind is the only reference to the door
1640 1640 * (no file descriptor refers to it).
1641 1641 */
1642 1642 if (DTOV(dp)->v_count == unref_count)
1643 1643 dip->di_attributes |= DOOR_IS_UNREF;
1644 1644 } else {
1645 1645 /*
1646 1646 * If we're working from a file descriptor or door handle
1647 1647 * we need to look at the file structure count. We don't
1648 1648 * need to hold the vnode lock since this is just a snapshot.
1649 1649 */
1650 1650 mutex_enter(&fp->f_tlock);
1651 1651 if (fp->f_count == 1 && DTOV(dp)->v_count == unref_count)
1652 1652 dip->di_attributes |= DOOR_IS_UNREF;
1653 1653 mutex_exit(&fp->f_tlock);
1654 1654 }
1655 1655 }
1656 1656
1657 1657 /*
1658 1658 * Return credentials of the door caller (if any) for this invocation
1659 1659 */
1660 1660 int
1661 1661 door_ucred(struct ucred_s *uch)
1662 1662 {
1663 1663 kthread_t *caller;
1664 1664 door_server_t *st;
1665 1665 door_client_t *ct;
1666 1666 door_upcall_t *dup;
1667 1667 struct proc *p;
1668 1668 struct ucred_s *res;
1669 1669 int err;
1670 1670
1671 1671 mutex_enter(&door_knob);
1672 1672 if ((st = door_my_server(0)) == NULL ||
1673 1673 (caller = st->d_caller) == NULL) {
1674 1674 mutex_exit(&door_knob);
1675 1675 return (set_errno(EINVAL));
1676 1676 }
1677 1677
1678 1678 ASSERT(caller->t_door != NULL);
1679 1679 ct = DOOR_CLIENT(caller->t_door);
1680 1680
1681 1681 /* Prevent caller from exiting while we examine the cred */
1682 1682 DOOR_T_HOLD(ct);
1683 1683 mutex_exit(&door_knob);
1684 1684
1685 1685 p = ttoproc(caller);
1686 1686
1687 1687 /*
1688 1688 * If the credentials are not specified by the client, get the one
1689 1689 * associated with the calling process.
1690 1690 */
1691 1691 if ((dup = ct->d_upcall) != NULL)
1692 1692 res = cred2ucred(dup->du_cred, p0.p_pid, NULL, CRED());
1693 1693 else
1694 1694 res = cred2ucred(caller->t_cred, p->p_pid, NULL, CRED());
1695 1695
1696 1696 mutex_enter(&door_knob);
1697 1697 DOOR_T_RELEASE(ct);
1698 1698 mutex_exit(&door_knob);
1699 1699
1700 1700 err = copyout(res, uch, res->uc_size);
1701 1701
1702 1702 kmem_free(res, res->uc_size);
1703 1703
1704 1704 if (err != 0)
1705 1705 return (set_errno(EFAULT));
1706 1706
1707 1707 return (0);
1708 1708 }
1709 1709
1710 1710 /*
1711 1711 * Bind the current lwp to the server thread pool associated with 'did'
1712 1712 */
1713 1713 int
1714 1714 door_bind(int did)
1715 1715 {
1716 1716 door_node_t *dp;
1717 1717 door_server_t *st;
1718 1718
1719 1719 if ((dp = door_lookup(did, NULL)) == NULL) {
1720 1720 /* Not a door */
1721 1721 return (set_errno(EBADF));
1722 1722 }
1723 1723
1724 1724 /*
1725 1725 * Can't bind to a non-private door, and can't bind to a door
1726 1726 * served by another process.
1727 1727 */
1728 1728 if ((dp->door_flags & DOOR_PRIVATE) == 0 ||
1729 1729 dp->door_target != curproc) {
1730 1730 releasef(did);
1731 1731 return (set_errno(EINVAL));
1732 1732 }
1733 1733
1734 1734 st = door_my_server(1);
1735 1735 if (st->d_pool)
1736 1736 door_unbind_thread(st->d_pool);
1737 1737 st->d_pool = dp;
1738 1738 st->d_invbound = 0;
1739 1739 door_bind_thread(dp);
1740 1740 releasef(did);
1741 1741
1742 1742 return (0);
1743 1743 }
1744 1744
1745 1745 /*
1746 1746 * Unbind the current lwp from it's server thread pool
1747 1747 */
1748 1748 int
1749 1749 door_unbind(void)
1750 1750 {
1751 1751 door_server_t *st;
1752 1752
1753 1753 if ((st = door_my_server(0)) == NULL)
1754 1754 return (set_errno(EBADF));
1755 1755
1756 1756 if (st->d_invbound) {
1757 1757 ASSERT(st->d_pool == NULL);
1758 1758 st->d_invbound = 0;
1759 1759 return (0);
1760 1760 }
1761 1761 if (st->d_pool == NULL)
1762 1762 return (set_errno(EBADF));
1763 1763 door_unbind_thread(st->d_pool);
1764 1764 st->d_pool = NULL;
1765 1765 return (0);
1766 1766 }
1767 1767
1768 1768 /*
1769 1769 * Create a descriptor for the associated file and fill in the
1770 1770 * attributes associated with it.
1771 1771 *
1772 1772 * Return 0 for success, -1 otherwise;
1773 1773 */
1774 1774 int
1775 1775 door_insert(struct file *fp, door_desc_t *dp)
1776 1776 {
↓ open down ↓ |
1776 lines elided |
↑ open up ↑ |
1777 1777 struct vnode *vp;
1778 1778 int fd;
1779 1779 door_attr_t attributes = DOOR_DESCRIPTOR;
1780 1780
1781 1781 ASSERT(MUTEX_NOT_HELD(&door_knob));
1782 1782 if ((fd = ufalloc(0)) == -1)
1783 1783 return (-1);
1784 1784 setf(fd, fp);
1785 1785 dp->d_data.d_desc.d_descriptor = fd;
1786 1786
1787 + /* add curproc to the pid list associated with that file */
1788 + if (fp->f_vnode != NULL)
1789 + (void) VOP_IOCTL(fp->f_vnode, F_FORKED, (intptr_t)curproc, FKIOCTL,
1790 + kcred, NULL, NULL);
1791 +
1787 1792 /* Fill in the attributes */
1788 1793 if (VOP_REALVP(fp->f_vnode, &vp, NULL))
1789 1794 vp = fp->f_vnode;
1790 1795 if (vp && vp->v_type == VDOOR) {
1791 1796 if (VTOD(vp)->door_target == curproc)
1792 1797 attributes |= DOOR_LOCAL;
1793 1798 attributes |= VTOD(vp)->door_flags & DOOR_ATTR_MASK;
1794 1799 dp->d_data.d_desc.d_id = VTOD(vp)->door_index;
1795 1800 }
1796 1801 dp->d_attributes = attributes;
1797 1802 return (0);
1798 1803 }
1799 1804
1800 1805 /*
1801 1806 * Return an available thread for this server. A NULL return value indicates
1802 1807 * that either:
1803 1808 * The door has been revoked, or
1804 1809 * a signal was received.
1805 1810 * The two conditions can be differentiated using DOOR_INVALID(dp).
1806 1811 */
1807 1812 static kthread_t *
1808 1813 door_get_server(door_node_t *dp)
1809 1814 {
1810 1815 kthread_t **ktp;
1811 1816 kthread_t *server_t;
1812 1817 door_pool_t *pool;
1813 1818 door_server_t *st;
1814 1819 int signalled;
1815 1820
1816 1821 disp_lock_t *tlp;
1817 1822 cpu_t *cp;
1818 1823
1819 1824 ASSERT(MUTEX_HELD(&door_knob));
1820 1825
1821 1826 if (dp->door_flags & DOOR_PRIVATE)
1822 1827 pool = &dp->door_servers;
1823 1828 else
1824 1829 pool = &dp->door_target->p_server_threads;
1825 1830
1826 1831 for (;;) {
1827 1832 /*
1828 1833 * We search the thread pool, looking for a server thread
1829 1834 * ready to take an invocation (i.e. one which is still
1830 1835 * sleeping on a shuttle object). If none are available,
1831 1836 * we sleep on the pool's CV, and will be signaled when a
1832 1837 * thread is added to the pool.
1833 1838 *
1834 1839 * This relies on the fact that once a thread in the thread
1835 1840 * pool wakes up, it *must* remove and add itself to the pool
1836 1841 * before it can receive door calls.
1837 1842 */
1838 1843 if (DOOR_INVALID(dp))
1839 1844 return (NULL); /* Target has become invalid */
1840 1845
1841 1846 for (ktp = &pool->dp_threads;
1842 1847 (server_t = *ktp) != NULL;
1843 1848 ktp = &st->d_servers) {
1844 1849 st = DOOR_SERVER(server_t->t_door);
1845 1850
1846 1851 thread_lock(server_t);
1847 1852 if (server_t->t_state == TS_SLEEP &&
1848 1853 SOBJ_TYPE(server_t->t_sobj_ops) == SOBJ_SHUTTLE)
1849 1854 break;
1850 1855 thread_unlock(server_t);
1851 1856 }
1852 1857 if (server_t != NULL)
1853 1858 break; /* we've got a live one! */
1854 1859
1855 1860 if (!cv_wait_sig_swap_core(&pool->dp_cv, &door_knob,
1856 1861 &signalled)) {
1857 1862 /*
1858 1863 * If we were signaled and the door is still
1859 1864 * valid, pass the signal on to another waiter.
1860 1865 */
1861 1866 if (signalled && !DOOR_INVALID(dp))
1862 1867 cv_signal(&pool->dp_cv);
1863 1868 return (NULL); /* Got a signal */
1864 1869 }
1865 1870 }
1866 1871
1867 1872 /*
1868 1873 * We've got a thread_lock()ed thread which is still on the
1869 1874 * shuttle. Take it off the list of available server threads
1870 1875 * and mark it as ONPROC. We are committed to resuming this
1871 1876 * thread now.
1872 1877 */
1873 1878 tlp = server_t->t_lockp;
1874 1879 cp = CPU;
1875 1880
1876 1881 *ktp = st->d_servers;
1877 1882 st->d_servers = NULL;
1878 1883 /*
1879 1884 * Setting t_disp_queue prevents erroneous preemptions
1880 1885 * if this thread is still in execution on another processor
1881 1886 */
1882 1887 server_t->t_disp_queue = cp->cpu_disp;
1883 1888 CL_ACTIVE(server_t);
1884 1889 /*
1885 1890 * We are calling thread_onproc() instead of
1886 1891 * THREAD_ONPROC() because compiler can reorder
1887 1892 * the two stores of t_state and t_lockp in
1888 1893 * THREAD_ONPROC().
1889 1894 */
1890 1895 thread_onproc(server_t, cp);
1891 1896 disp_lock_exit(tlp);
1892 1897 return (server_t);
1893 1898 }
1894 1899
1895 1900 /*
1896 1901 * Put a server thread back in the pool.
1897 1902 */
1898 1903 static void
1899 1904 door_release_server(door_node_t *dp, kthread_t *t)
1900 1905 {
1901 1906 door_server_t *st = DOOR_SERVER(t->t_door);
1902 1907 door_pool_t *pool;
1903 1908
1904 1909 ASSERT(MUTEX_HELD(&door_knob));
1905 1910 st->d_active = NULL;
1906 1911 st->d_caller = NULL;
1907 1912 st->d_layout_done = 0;
1908 1913 if (dp && (dp->door_flags & DOOR_PRIVATE)) {
1909 1914 ASSERT(dp->door_target == NULL ||
1910 1915 dp->door_target == ttoproc(t));
1911 1916 pool = &dp->door_servers;
1912 1917 } else {
1913 1918 pool = &ttoproc(t)->p_server_threads;
1914 1919 }
1915 1920
1916 1921 st->d_servers = pool->dp_threads;
1917 1922 pool->dp_threads = t;
1918 1923
1919 1924 /* If someone is waiting for a server thread, wake him up */
1920 1925 cv_signal(&pool->dp_cv);
1921 1926 }
1922 1927
1923 1928 /*
1924 1929 * Remove a server thread from the pool if present.
1925 1930 */
1926 1931 static void
1927 1932 door_server_exit(proc_t *p, kthread_t *t)
1928 1933 {
1929 1934 door_pool_t *pool;
1930 1935 kthread_t **next;
1931 1936 door_server_t *st = DOOR_SERVER(t->t_door);
1932 1937
1933 1938 ASSERT(MUTEX_HELD(&door_knob));
1934 1939 if (st->d_pool != NULL) {
1935 1940 ASSERT(st->d_pool->door_flags & DOOR_PRIVATE);
1936 1941 pool = &st->d_pool->door_servers;
1937 1942 } else {
1938 1943 pool = &p->p_server_threads;
1939 1944 }
1940 1945
1941 1946 next = &pool->dp_threads;
1942 1947 while (*next != NULL) {
1943 1948 if (*next == t) {
1944 1949 *next = DOOR_SERVER(t->t_door)->d_servers;
1945 1950 return;
1946 1951 }
1947 1952 next = &(DOOR_SERVER((*next)->t_door)->d_servers);
1948 1953 }
1949 1954 }
1950 1955
1951 1956 /*
1952 1957 * Lookup the door descriptor. Caller must call releasef when finished
1953 1958 * with associated door.
1954 1959 */
1955 1960 static door_node_t *
1956 1961 door_lookup(int did, file_t **fpp)
1957 1962 {
1958 1963 vnode_t *vp;
1959 1964 file_t *fp;
1960 1965
1961 1966 ASSERT(MUTEX_NOT_HELD(&door_knob));
1962 1967 if ((fp = getf(did)) == NULL)
1963 1968 return (NULL);
1964 1969 /*
1965 1970 * Use the underlying vnode (we may be namefs mounted)
1966 1971 */
1967 1972 if (VOP_REALVP(fp->f_vnode, &vp, NULL))
1968 1973 vp = fp->f_vnode;
1969 1974
1970 1975 if (vp == NULL || vp->v_type != VDOOR) {
1971 1976 releasef(did);
1972 1977 return (NULL);
1973 1978 }
1974 1979
1975 1980 if (fpp)
1976 1981 *fpp = fp;
1977 1982
1978 1983 return (VTOD(vp));
1979 1984 }
1980 1985
1981 1986 /*
1982 1987 * The current thread is exiting, so clean up any pending
1983 1988 * invocation details
1984 1989 */
1985 1990 void
1986 1991 door_slam(void)
1987 1992 {
1988 1993 door_node_t *dp;
1989 1994 door_data_t *dt;
1990 1995 door_client_t *ct;
1991 1996 door_server_t *st;
1992 1997
1993 1998 /*
1994 1999 * If we are an active door server, notify our
1995 2000 * client that we are exiting and revoke our door.
1996 2001 */
1997 2002 if ((dt = door_my_data(0)) == NULL)
1998 2003 return;
1999 2004 ct = DOOR_CLIENT(dt);
2000 2005 st = DOOR_SERVER(dt);
2001 2006
2002 2007 mutex_enter(&door_knob);
2003 2008 for (;;) {
2004 2009 if (DOOR_T_HELD(ct))
2005 2010 cv_wait(&ct->d_cv, &door_knob);
2006 2011 else if (DOOR_T_HELD(st))
2007 2012 cv_wait(&st->d_cv, &door_knob);
2008 2013 else
2009 2014 break; /* neither flag is set */
2010 2015 }
2011 2016 curthread->t_door = NULL;
2012 2017 if ((dp = st->d_active) != NULL) {
2013 2018 kthread_t *t = st->d_caller;
2014 2019 proc_t *p = curproc;
2015 2020
2016 2021 /* Revoke our door if the process is exiting */
2017 2022 if (dp->door_target == p && (p->p_flag & SEXITING)) {
2018 2023 door_list_delete(dp);
2019 2024 dp->door_target = NULL;
2020 2025 dp->door_flags |= DOOR_REVOKED;
2021 2026 if (dp->door_flags & DOOR_PRIVATE)
2022 2027 cv_broadcast(&dp->door_servers.dp_cv);
2023 2028 else
2024 2029 cv_broadcast(&p->p_server_threads.dp_cv);
2025 2030 }
2026 2031
2027 2032 if (t != NULL) {
2028 2033 /*
2029 2034 * Let the caller know we are gone
2030 2035 */
2031 2036 DOOR_CLIENT(t->t_door)->d_error = DOOR_EXIT;
2032 2037 thread_lock(t);
2033 2038 if (t->t_state == TS_SLEEP &&
2034 2039 SOBJ_TYPE(t->t_sobj_ops) == SOBJ_SHUTTLE)
2035 2040 setrun_locked(t);
2036 2041 thread_unlock(t);
2037 2042 }
2038 2043 }
2039 2044 mutex_exit(&door_knob);
2040 2045 if (st->d_pool)
2041 2046 door_unbind_thread(st->d_pool); /* Implicit door_unbind */
2042 2047 kmem_free(dt, sizeof (door_data_t));
2043 2048 }
2044 2049
2045 2050 /*
2046 2051 * Set DOOR_REVOKED for all doors of the current process. This is called
2047 2052 * on exit before all lwp's are being terminated so that door calls will
2048 2053 * return with an error.
2049 2054 */
2050 2055 void
2051 2056 door_revoke_all()
2052 2057 {
2053 2058 door_node_t *dp;
2054 2059 proc_t *p = ttoproc(curthread);
2055 2060
2056 2061 mutex_enter(&door_knob);
2057 2062 for (dp = p->p_door_list; dp != NULL; dp = dp->door_list) {
2058 2063 ASSERT(dp->door_target == p);
2059 2064 dp->door_flags |= DOOR_REVOKED;
2060 2065 if (dp->door_flags & DOOR_PRIVATE)
2061 2066 cv_broadcast(&dp->door_servers.dp_cv);
2062 2067 }
2063 2068 cv_broadcast(&p->p_server_threads.dp_cv);
2064 2069 mutex_exit(&door_knob);
2065 2070 }
2066 2071
2067 2072 /*
2068 2073 * The process is exiting, and all doors it created need to be revoked.
2069 2074 */
2070 2075 void
2071 2076 door_exit(void)
2072 2077 {
2073 2078 door_node_t *dp;
2074 2079 proc_t *p = ttoproc(curthread);
2075 2080
2076 2081 ASSERT(p->p_lwpcnt == 1);
2077 2082 /*
2078 2083 * Walk the list of active doors created by this process and
2079 2084 * revoke them all.
2080 2085 */
2081 2086 mutex_enter(&door_knob);
2082 2087 for (dp = p->p_door_list; dp != NULL; dp = dp->door_list) {
2083 2088 dp->door_target = NULL;
2084 2089 dp->door_flags |= DOOR_REVOKED;
2085 2090 if (dp->door_flags & DOOR_PRIVATE)
2086 2091 cv_broadcast(&dp->door_servers.dp_cv);
2087 2092 }
2088 2093 cv_broadcast(&p->p_server_threads.dp_cv);
2089 2094 /* Clear the list */
2090 2095 p->p_door_list = NULL;
2091 2096
2092 2097 /* Clean up the unref list */
2093 2098 while ((dp = p->p_unref_list) != NULL) {
2094 2099 p->p_unref_list = dp->door_ulist;
2095 2100 dp->door_ulist = NULL;
2096 2101 mutex_exit(&door_knob);
2097 2102 VN_RELE(DTOV(dp));
2098 2103 mutex_enter(&door_knob);
2099 2104 }
2100 2105 mutex_exit(&door_knob);
2101 2106 }
2102 2107
2103 2108
2104 2109 /*
2105 2110 * The process is executing forkall(), and we need to flag threads that
2106 2111 * are bound to a door in the child. This will make the child threads
2107 2112 * return an error to door_return unless they call door_unbind first.
2108 2113 */
2109 2114 void
2110 2115 door_fork(kthread_t *parent, kthread_t *child)
2111 2116 {
2112 2117 door_data_t *pt = parent->t_door;
2113 2118 door_server_t *st = DOOR_SERVER(pt);
2114 2119 door_data_t *dt;
2115 2120
2116 2121 ASSERT(MUTEX_NOT_HELD(&door_knob));
2117 2122 if (pt != NULL && (st->d_pool != NULL || st->d_invbound)) {
2118 2123 /* parent thread is bound to a door */
2119 2124 dt = child->t_door =
2120 2125 kmem_zalloc(sizeof (door_data_t), KM_SLEEP);
2121 2126 DOOR_SERVER(dt)->d_invbound = 1;
2122 2127 }
2123 2128 }
2124 2129
2125 2130 /*
2126 2131 * Deliver queued unrefs to appropriate door server.
2127 2132 */
2128 2133 static int
2129 2134 door_unref(void)
2130 2135 {
2131 2136 door_node_t *dp;
2132 2137 static door_arg_t unref_args = { DOOR_UNREF_DATA, 0, 0, 0, 0, 0 };
2133 2138 proc_t *p = ttoproc(curthread);
2134 2139
2135 2140 /* make sure there's only one unref thread per process */
2136 2141 mutex_enter(&door_knob);
2137 2142 if (p->p_unref_thread) {
2138 2143 mutex_exit(&door_knob);
2139 2144 return (set_errno(EALREADY));
2140 2145 }
2141 2146 p->p_unref_thread = 1;
2142 2147 mutex_exit(&door_knob);
2143 2148
2144 2149 (void) door_my_data(1); /* create info, if necessary */
2145 2150
2146 2151 for (;;) {
2147 2152 mutex_enter(&door_knob);
2148 2153
2149 2154 /* Grab a queued request */
2150 2155 while ((dp = p->p_unref_list) == NULL) {
2151 2156 if (!cv_wait_sig(&p->p_unref_cv, &door_knob)) {
2152 2157 /*
2153 2158 * Interrupted.
2154 2159 * Return so we can finish forkall() or exit().
2155 2160 */
2156 2161 p->p_unref_thread = 0;
2157 2162 mutex_exit(&door_knob);
2158 2163 return (set_errno(EINTR));
2159 2164 }
2160 2165 }
2161 2166 p->p_unref_list = dp->door_ulist;
2162 2167 dp->door_ulist = NULL;
2163 2168 dp->door_flags |= DOOR_UNREF_ACTIVE;
2164 2169 mutex_exit(&door_knob);
2165 2170
2166 2171 (void) door_upcall(DTOV(dp), &unref_args, NULL, SIZE_MAX, 0);
2167 2172
2168 2173 if (unref_args.rbuf != 0) {
2169 2174 kmem_free(unref_args.rbuf, unref_args.rsize);
2170 2175 unref_args.rbuf = NULL;
2171 2176 unref_args.rsize = 0;
2172 2177 }
2173 2178
2174 2179 mutex_enter(&door_knob);
2175 2180 ASSERT(dp->door_flags & DOOR_UNREF_ACTIVE);
2176 2181 dp->door_flags &= ~DOOR_UNREF_ACTIVE;
2177 2182 mutex_exit(&door_knob);
2178 2183 VN_RELE(DTOV(dp));
2179 2184 }
2180 2185 }
2181 2186
2182 2187
2183 2188 /*
2184 2189 * Deliver queued unrefs to kernel door server.
2185 2190 */
2186 2191 /* ARGSUSED */
2187 2192 static void
2188 2193 door_unref_kernel(caddr_t arg)
2189 2194 {
2190 2195 door_node_t *dp;
2191 2196 static door_arg_t unref_args = { DOOR_UNREF_DATA, 0, 0, 0, 0, 0 };
2192 2197 proc_t *p = ttoproc(curthread);
2193 2198 callb_cpr_t cprinfo;
2194 2199
2195 2200 /* should only be one of these */
2196 2201 mutex_enter(&door_knob);
2197 2202 if (p->p_unref_thread) {
2198 2203 mutex_exit(&door_knob);
2199 2204 return;
2200 2205 }
2201 2206 p->p_unref_thread = 1;
2202 2207 mutex_exit(&door_knob);
2203 2208
2204 2209 (void) door_my_data(1); /* make sure we have a door_data_t */
2205 2210
2206 2211 CALLB_CPR_INIT(&cprinfo, &door_knob, callb_generic_cpr, "door_unref");
2207 2212 for (;;) {
2208 2213 mutex_enter(&door_knob);
2209 2214 /* Grab a queued request */
2210 2215 while ((dp = p->p_unref_list) == NULL) {
2211 2216 CALLB_CPR_SAFE_BEGIN(&cprinfo);
2212 2217 cv_wait(&p->p_unref_cv, &door_knob);
2213 2218 CALLB_CPR_SAFE_END(&cprinfo, &door_knob);
2214 2219 }
2215 2220 p->p_unref_list = dp->door_ulist;
2216 2221 dp->door_ulist = NULL;
2217 2222 dp->door_flags |= DOOR_UNREF_ACTIVE;
2218 2223 mutex_exit(&door_knob);
2219 2224
2220 2225 (*(dp->door_pc))(dp->door_data, &unref_args, NULL, NULL, NULL);
2221 2226
2222 2227 mutex_enter(&door_knob);
2223 2228 ASSERT(dp->door_flags & DOOR_UNREF_ACTIVE);
2224 2229 dp->door_flags &= ~DOOR_UNREF_ACTIVE;
2225 2230 mutex_exit(&door_knob);
2226 2231 VN_RELE(DTOV(dp));
2227 2232 }
2228 2233 }
2229 2234
2230 2235
2231 2236 /*
2232 2237 * Queue an unref invocation for processing for the current process
2233 2238 * The door may or may not be revoked at this point.
2234 2239 */
2235 2240 void
2236 2241 door_deliver_unref(door_node_t *d)
2237 2242 {
2238 2243 struct proc *server = d->door_target;
2239 2244
2240 2245 ASSERT(MUTEX_HELD(&door_knob));
2241 2246 ASSERT(d->door_active == 0);
2242 2247
2243 2248 if (server == NULL)
2244 2249 return;
2245 2250 /*
2246 2251 * Create a lwp to deliver unref calls if one isn't already running.
2247 2252 *
2248 2253 * A separate thread is used to deliver unrefs since the current
2249 2254 * thread may be holding resources (e.g. locks) in user land that
2250 2255 * may be needed by the unref processing. This would cause a
2251 2256 * deadlock.
2252 2257 */
2253 2258 if (d->door_flags & DOOR_UNREF_MULTI) {
2254 2259 /* multiple unrefs */
2255 2260 d->door_flags &= ~DOOR_DELAY;
2256 2261 } else {
2257 2262 /* Only 1 unref per door */
2258 2263 d->door_flags &= ~(DOOR_UNREF|DOOR_DELAY);
2259 2264 }
2260 2265 mutex_exit(&door_knob);
2261 2266
2262 2267 /*
2263 2268 * Need to bump the vnode count before putting the door on the
2264 2269 * list so it doesn't get prematurely released by door_unref.
2265 2270 */
2266 2271 VN_HOLD(DTOV(d));
2267 2272
2268 2273 mutex_enter(&door_knob);
2269 2274 /* is this door already on the unref list? */
2270 2275 if (d->door_flags & DOOR_UNREF_MULTI) {
2271 2276 door_node_t *dp;
2272 2277 for (dp = server->p_unref_list; dp != NULL;
2273 2278 dp = dp->door_ulist) {
2274 2279 if (d == dp) {
2275 2280 /* already there, don't need to add another */
2276 2281 mutex_exit(&door_knob);
2277 2282 VN_RELE(DTOV(d));
2278 2283 mutex_enter(&door_knob);
2279 2284 return;
2280 2285 }
2281 2286 }
2282 2287 }
2283 2288 ASSERT(d->door_ulist == NULL);
2284 2289 d->door_ulist = server->p_unref_list;
2285 2290 server->p_unref_list = d;
2286 2291 cv_broadcast(&server->p_unref_cv);
2287 2292 }
2288 2293
2289 2294 /*
2290 2295 * The callers buffer isn't big enough for all of the data/fd's. Allocate
2291 2296 * space in the callers address space for the results and copy the data
2292 2297 * there.
2293 2298 *
2294 2299 * For EOVERFLOW, we must clean up the server's door descriptors.
2295 2300 */
2296 2301 static int
2297 2302 door_overflow(
2298 2303 kthread_t *caller,
2299 2304 caddr_t data_ptr, /* data location */
2300 2305 size_t data_size, /* data size */
2301 2306 door_desc_t *desc_ptr, /* descriptor location */
2302 2307 uint_t desc_num) /* descriptor size */
2303 2308 {
2304 2309 proc_t *callerp = ttoproc(caller);
2305 2310 struct as *as = callerp->p_as;
2306 2311 door_client_t *ct = DOOR_CLIENT(caller->t_door);
2307 2312 caddr_t addr; /* Resulting address in target */
2308 2313 size_t rlen; /* Rounded len */
2309 2314 size_t len;
2310 2315 uint_t i;
2311 2316 size_t ds = desc_num * sizeof (door_desc_t);
2312 2317
2313 2318 ASSERT(MUTEX_NOT_HELD(&door_knob));
2314 2319 ASSERT(DOOR_T_HELD(ct) || ct->d_kernel);
2315 2320
2316 2321 /* Do initial overflow check */
2317 2322 if (!ufcanalloc(callerp, desc_num))
2318 2323 return (EMFILE);
2319 2324
2320 2325 /*
2321 2326 * Allocate space for this stuff in the callers address space
2322 2327 */
2323 2328 rlen = roundup(data_size + ds, PAGESIZE);
2324 2329 as_rangelock(as);
2325 2330 map_addr_proc(&addr, rlen, 0, 1, as->a_userlimit, ttoproc(caller), 0);
2326 2331 if (addr == NULL ||
2327 2332 as_map(as, addr, rlen, segvn_create, zfod_argsp) != 0) {
2328 2333 /* No virtual memory available, or anon mapping failed */
2329 2334 as_rangeunlock(as);
2330 2335 if (!ct->d_kernel && desc_num > 0) {
2331 2336 int error = door_release_fds(desc_ptr, desc_num);
2332 2337 if (error)
2333 2338 return (error);
2334 2339 }
2335 2340 return (EOVERFLOW);
2336 2341 }
2337 2342 as_rangeunlock(as);
2338 2343
2339 2344 if (ct->d_kernel)
2340 2345 goto out;
2341 2346
2342 2347 if (data_size != 0) {
2343 2348 caddr_t src = data_ptr;
2344 2349 caddr_t saddr = addr;
2345 2350
2346 2351 /* Copy any data */
2347 2352 len = data_size;
2348 2353 while (len != 0) {
2349 2354 int amount;
2350 2355 int error;
2351 2356
2352 2357 amount = len > PAGESIZE ? PAGESIZE : len;
2353 2358 if ((error = door_copy(as, src, saddr, amount)) != 0) {
2354 2359 (void) as_unmap(as, addr, rlen);
2355 2360 return (error);
2356 2361 }
2357 2362 saddr += amount;
2358 2363 src += amount;
2359 2364 len -= amount;
2360 2365 }
2361 2366 }
2362 2367 /* Copy any fd's */
2363 2368 if (desc_num != 0) {
2364 2369 door_desc_t *didpp, *start;
2365 2370 struct file **fpp;
2366 2371 int fpp_size;
2367 2372
2368 2373 start = didpp = kmem_alloc(ds, KM_SLEEP);
2369 2374 if (copyin_nowatch(desc_ptr, didpp, ds)) {
2370 2375 kmem_free(start, ds);
2371 2376 (void) as_unmap(as, addr, rlen);
2372 2377 return (EFAULT);
2373 2378 }
2374 2379
2375 2380 fpp_size = desc_num * sizeof (struct file *);
2376 2381 if (fpp_size > ct->d_fpp_size) {
2377 2382 /* make more space */
2378 2383 if (ct->d_fpp_size)
2379 2384 kmem_free(ct->d_fpp, ct->d_fpp_size);
2380 2385 ct->d_fpp_size = fpp_size;
2381 2386 ct->d_fpp = kmem_alloc(ct->d_fpp_size, KM_SLEEP);
2382 2387 }
2383 2388 fpp = ct->d_fpp;
2384 2389
2385 2390 for (i = 0; i < desc_num; i++) {
2386 2391 struct file *fp;
2387 2392 int fd = didpp->d_data.d_desc.d_descriptor;
2388 2393
2389 2394 if (!(didpp->d_attributes & DOOR_DESCRIPTOR) ||
2390 2395 (fp = getf(fd)) == NULL) {
2391 2396 /* close translated references */
2392 2397 door_fp_close(ct->d_fpp, fpp - ct->d_fpp);
2393 2398 /* close untranslated references */
2394 2399 door_fd_rele(didpp, desc_num - i, 0);
2395 2400 kmem_free(start, ds);
2396 2401 (void) as_unmap(as, addr, rlen);
2397 2402 return (EINVAL);
2398 2403 }
2399 2404 mutex_enter(&fp->f_tlock);
2400 2405 fp->f_count++;
2401 2406 mutex_exit(&fp->f_tlock);
2402 2407
2403 2408 *fpp = fp;
2404 2409 releasef(fd);
2405 2410
2406 2411 if (didpp->d_attributes & DOOR_RELEASE) {
2407 2412 /* release passed reference */
2408 2413 (void) closeandsetf(fd, NULL);
2409 2414 }
2410 2415
2411 2416 fpp++; didpp++;
2412 2417 }
2413 2418 kmem_free(start, ds);
2414 2419 }
2415 2420
2416 2421 out:
2417 2422 ct->d_overflow = 1;
2418 2423 ct->d_args.rbuf = addr;
2419 2424 ct->d_args.rsize = rlen;
2420 2425 return (0);
2421 2426 }
2422 2427
2423 2428 /*
2424 2429 * Transfer arguments from the client to the server.
2425 2430 */
2426 2431 static int
2427 2432 door_args(kthread_t *server, int is_private)
2428 2433 {
2429 2434 door_server_t *st = DOOR_SERVER(server->t_door);
2430 2435 door_client_t *ct = DOOR_CLIENT(curthread->t_door);
2431 2436 uint_t ndid;
2432 2437 size_t dsize;
2433 2438 int error;
2434 2439
2435 2440 ASSERT(DOOR_T_HELD(st));
2436 2441 ASSERT(MUTEX_NOT_HELD(&door_knob));
2437 2442
2438 2443 ndid = ct->d_args.desc_num;
2439 2444 if (ndid > door_max_desc)
2440 2445 return (E2BIG);
2441 2446
2442 2447 /*
2443 2448 * Get the stack layout, and fail now if it won't fit.
2444 2449 */
2445 2450 error = door_layout(server, ct->d_args.data_size, ndid, is_private);
2446 2451 if (error != 0)
2447 2452 return (error);
2448 2453
2449 2454 dsize = ndid * sizeof (door_desc_t);
2450 2455 if (ct->d_args.data_size != 0) {
2451 2456 if (ct->d_args.data_size <= door_max_arg) {
2452 2457 /*
2453 2458 * Use a 2 copy method for small amounts of data
2454 2459 *
2455 2460 * Allocate a little more than we need for the
2456 2461 * args, in the hope that the results will fit
2457 2462 * without having to reallocate a buffer
2458 2463 */
2459 2464 ASSERT(ct->d_buf == NULL);
2460 2465 ct->d_bufsize = roundup(ct->d_args.data_size,
2461 2466 DOOR_ROUND);
2462 2467 ct->d_buf = kmem_alloc(ct->d_bufsize, KM_SLEEP);
2463 2468 if (copyin_nowatch(ct->d_args.data_ptr,
2464 2469 ct->d_buf, ct->d_args.data_size) != 0) {
2465 2470 kmem_free(ct->d_buf, ct->d_bufsize);
2466 2471 ct->d_buf = NULL;
2467 2472 ct->d_bufsize = 0;
2468 2473 return (EFAULT);
2469 2474 }
2470 2475 } else {
2471 2476 struct as *as;
2472 2477 caddr_t src;
2473 2478 caddr_t dest;
2474 2479 size_t len = ct->d_args.data_size;
2475 2480 uintptr_t base;
2476 2481
2477 2482 /*
2478 2483 * Use a 1 copy method
2479 2484 */
2480 2485 as = ttoproc(server)->p_as;
2481 2486 src = ct->d_args.data_ptr;
2482 2487
2483 2488 dest = st->d_layout.dl_datap;
2484 2489 base = (uintptr_t)dest;
2485 2490
2486 2491 /*
2487 2492 * Copy data directly into server. We proceed
2488 2493 * downward from the top of the stack, to mimic
2489 2494 * normal stack usage. This allows the guard page
2490 2495 * to stop us before we corrupt anything.
2491 2496 */
2492 2497 while (len != 0) {
2493 2498 uintptr_t start;
2494 2499 uintptr_t end;
2495 2500 uintptr_t offset;
2496 2501 size_t amount;
2497 2502
2498 2503 /*
2499 2504 * Locate the next part to copy.
2500 2505 */
2501 2506 end = base + len;
2502 2507 start = P2ALIGN(end - 1, PAGESIZE);
2503 2508
2504 2509 /*
2505 2510 * if we are on the final (first) page, fix
2506 2511 * up the start position.
2507 2512 */
2508 2513 if (P2ALIGN(base, PAGESIZE) == start)
2509 2514 start = base;
2510 2515
2511 2516 offset = start - base; /* the copy offset */
2512 2517 amount = end - start; /* # bytes to copy */
2513 2518
2514 2519 ASSERT(amount > 0 && amount <= len &&
2515 2520 amount <= PAGESIZE);
2516 2521
2517 2522 error = door_copy(as, src + offset,
2518 2523 dest + offset, amount);
2519 2524 if (error != 0)
2520 2525 return (error);
2521 2526 len -= amount;
2522 2527 }
2523 2528 }
2524 2529 }
2525 2530 /*
2526 2531 * Copyin the door args and translate them into files
2527 2532 */
2528 2533 if (ndid != 0) {
2529 2534 door_desc_t *didpp;
2530 2535 door_desc_t *start;
2531 2536 struct file **fpp;
2532 2537
2533 2538 start = didpp = kmem_alloc(dsize, KM_SLEEP);
2534 2539
2535 2540 if (copyin_nowatch(ct->d_args.desc_ptr, didpp, dsize)) {
2536 2541 kmem_free(start, dsize);
2537 2542 return (EFAULT);
2538 2543 }
2539 2544 ct->d_fpp_size = ndid * sizeof (struct file *);
2540 2545 ct->d_fpp = kmem_alloc(ct->d_fpp_size, KM_SLEEP);
2541 2546 fpp = ct->d_fpp;
2542 2547 while (ndid--) {
2543 2548 struct file *fp;
2544 2549 int fd = didpp->d_data.d_desc.d_descriptor;
2545 2550
2546 2551 /* We only understand file descriptors as passed objs */
2547 2552 if (!(didpp->d_attributes & DOOR_DESCRIPTOR) ||
2548 2553 (fp = getf(fd)) == NULL) {
2549 2554 /* close translated references */
2550 2555 door_fp_close(ct->d_fpp, fpp - ct->d_fpp);
2551 2556 /* close untranslated references */
2552 2557 door_fd_rele(didpp, ndid + 1, 0);
2553 2558 kmem_free(start, dsize);
2554 2559 kmem_free(ct->d_fpp, ct->d_fpp_size);
2555 2560 ct->d_fpp = NULL;
2556 2561 ct->d_fpp_size = 0;
2557 2562 return (EINVAL);
2558 2563 }
2559 2564 /* Hold the fp */
2560 2565 mutex_enter(&fp->f_tlock);
2561 2566 fp->f_count++;
2562 2567 mutex_exit(&fp->f_tlock);
2563 2568
2564 2569 *fpp = fp;
2565 2570 releasef(fd);
2566 2571
2567 2572 if (didpp->d_attributes & DOOR_RELEASE) {
2568 2573 /* release passed reference */
2569 2574 (void) closeandsetf(fd, NULL);
2570 2575 }
2571 2576
2572 2577 fpp++; didpp++;
2573 2578 }
2574 2579 kmem_free(start, dsize);
2575 2580 }
2576 2581 return (0);
2577 2582 }
2578 2583
2579 2584 /*
2580 2585 * Transfer arguments from a user client to a kernel server. This copies in
2581 2586 * descriptors and translates them into door handles. It doesn't touch the
2582 2587 * other data, letting the kernel server deal with that (to avoid needing
2583 2588 * to copy the data twice).
2584 2589 */
2585 2590 static int
2586 2591 door_translate_in(void)
2587 2592 {
2588 2593 door_client_t *ct = DOOR_CLIENT(curthread->t_door);
2589 2594 uint_t ndid;
2590 2595
2591 2596 ASSERT(MUTEX_NOT_HELD(&door_knob));
2592 2597 ndid = ct->d_args.desc_num;
2593 2598 if (ndid > door_max_desc)
2594 2599 return (E2BIG);
2595 2600 /*
2596 2601 * Copyin the door args and translate them into door handles.
2597 2602 */
2598 2603 if (ndid != 0) {
2599 2604 door_desc_t *didpp;
2600 2605 door_desc_t *start;
2601 2606 size_t dsize = ndid * sizeof (door_desc_t);
2602 2607 struct file *fp;
2603 2608
2604 2609 start = didpp = kmem_alloc(dsize, KM_SLEEP);
2605 2610
2606 2611 if (copyin_nowatch(ct->d_args.desc_ptr, didpp, dsize)) {
2607 2612 kmem_free(start, dsize);
2608 2613 return (EFAULT);
2609 2614 }
2610 2615 while (ndid--) {
2611 2616 vnode_t *vp;
2612 2617 int fd = didpp->d_data.d_desc.d_descriptor;
2613 2618
2614 2619 /*
2615 2620 * We only understand file descriptors as passed objs
2616 2621 */
2617 2622 if ((didpp->d_attributes & DOOR_DESCRIPTOR) &&
2618 2623 (fp = getf(fd)) != NULL) {
2619 2624 didpp->d_data.d_handle = FTODH(fp);
2620 2625 /* Hold the door */
2621 2626 door_ki_hold(didpp->d_data.d_handle);
2622 2627
2623 2628 releasef(fd);
2624 2629
2625 2630 if (didpp->d_attributes & DOOR_RELEASE) {
2626 2631 /* release passed reference */
2627 2632 (void) closeandsetf(fd, NULL);
2628 2633 }
2629 2634
2630 2635 if (VOP_REALVP(fp->f_vnode, &vp, NULL))
2631 2636 vp = fp->f_vnode;
2632 2637
2633 2638 /* Set attributes */
2634 2639 didpp->d_attributes = DOOR_HANDLE |
2635 2640 (VTOD(vp)->door_flags & DOOR_ATTR_MASK);
2636 2641 } else {
2637 2642 /* close translated references */
2638 2643 door_fd_close(start, didpp - start);
2639 2644 /* close untranslated references */
2640 2645 door_fd_rele(didpp, ndid + 1, 0);
2641 2646 kmem_free(start, dsize);
2642 2647 return (EINVAL);
2643 2648 }
2644 2649 didpp++;
2645 2650 }
2646 2651 ct->d_args.desc_ptr = start;
2647 2652 }
2648 2653 return (0);
2649 2654 }
2650 2655
2651 2656 /*
2652 2657 * Translate door arguments from kernel to user. This copies the passed
2653 2658 * door handles. It doesn't touch other data. It is used by door_upcall,
2654 2659 * and for data returned by a door_call to a kernel server.
2655 2660 */
2656 2661 static int
2657 2662 door_translate_out(void)
2658 2663 {
2659 2664 door_client_t *ct = DOOR_CLIENT(curthread->t_door);
2660 2665 uint_t ndid;
2661 2666
2662 2667 ASSERT(MUTEX_NOT_HELD(&door_knob));
2663 2668 ndid = ct->d_args.desc_num;
2664 2669 if (ndid > door_max_desc) {
2665 2670 door_fd_rele(ct->d_args.desc_ptr, ndid, 1);
2666 2671 return (E2BIG);
2667 2672 }
2668 2673 /*
2669 2674 * Translate the door args into files
2670 2675 */
2671 2676 if (ndid != 0) {
2672 2677 door_desc_t *didpp = ct->d_args.desc_ptr;
2673 2678 struct file **fpp;
2674 2679
2675 2680 ct->d_fpp_size = ndid * sizeof (struct file *);
2676 2681 fpp = ct->d_fpp = kmem_alloc(ct->d_fpp_size, KM_SLEEP);
2677 2682 while (ndid--) {
2678 2683 struct file *fp = NULL;
2679 2684 int fd = -1;
2680 2685
2681 2686 /*
2682 2687 * We understand file descriptors and door
2683 2688 * handles as passed objs.
2684 2689 */
2685 2690 if (didpp->d_attributes & DOOR_DESCRIPTOR) {
2686 2691 fd = didpp->d_data.d_desc.d_descriptor;
2687 2692 fp = getf(fd);
2688 2693 } else if (didpp->d_attributes & DOOR_HANDLE)
2689 2694 fp = DHTOF(didpp->d_data.d_handle);
2690 2695 if (fp != NULL) {
2691 2696 /* Hold the fp */
2692 2697 mutex_enter(&fp->f_tlock);
2693 2698 fp->f_count++;
2694 2699 mutex_exit(&fp->f_tlock);
2695 2700
2696 2701 *fpp = fp;
2697 2702 if (didpp->d_attributes & DOOR_DESCRIPTOR)
2698 2703 releasef(fd);
2699 2704 if (didpp->d_attributes & DOOR_RELEASE) {
2700 2705 /* release passed reference */
2701 2706 if (fd >= 0)
2702 2707 (void) closeandsetf(fd, NULL);
2703 2708 else
2704 2709 (void) closef(fp);
2705 2710 }
2706 2711 } else {
2707 2712 /* close translated references */
2708 2713 door_fp_close(ct->d_fpp, fpp - ct->d_fpp);
2709 2714 /* close untranslated references */
2710 2715 door_fd_rele(didpp, ndid + 1, 1);
2711 2716 kmem_free(ct->d_fpp, ct->d_fpp_size);
2712 2717 ct->d_fpp = NULL;
2713 2718 ct->d_fpp_size = 0;
2714 2719 return (EINVAL);
2715 2720 }
2716 2721 fpp++; didpp++;
2717 2722 }
2718 2723 }
2719 2724 return (0);
2720 2725 }
2721 2726
2722 2727 /*
2723 2728 * Move the results from the server to the client
2724 2729 */
2725 2730 static int
2726 2731 door_results(kthread_t *caller, caddr_t data_ptr, size_t data_size,
2727 2732 door_desc_t *desc_ptr, uint_t desc_num)
2728 2733 {
2729 2734 door_client_t *ct = DOOR_CLIENT(caller->t_door);
2730 2735 door_upcall_t *dup = ct->d_upcall;
2731 2736 size_t dsize;
2732 2737 size_t rlen;
2733 2738 size_t result_size;
2734 2739
2735 2740 ASSERT(DOOR_T_HELD(ct));
2736 2741 ASSERT(MUTEX_NOT_HELD(&door_knob));
2737 2742
2738 2743 if (ct->d_noresults)
2739 2744 return (E2BIG); /* No results expected */
2740 2745
2741 2746 if (desc_num > door_max_desc)
2742 2747 return (E2BIG); /* Too many descriptors */
2743 2748
2744 2749 dsize = desc_num * sizeof (door_desc_t);
2745 2750 /*
2746 2751 * Check if the results are bigger than the clients buffer
2747 2752 */
2748 2753 if (dsize)
2749 2754 rlen = roundup(data_size, sizeof (door_desc_t));
2750 2755 else
2751 2756 rlen = data_size;
2752 2757 if ((result_size = rlen + dsize) == 0)
2753 2758 return (0);
2754 2759
2755 2760 if (dup != NULL) {
2756 2761 if (desc_num > dup->du_max_descs)
2757 2762 return (EMFILE);
2758 2763
2759 2764 if (data_size > dup->du_max_data)
2760 2765 return (E2BIG);
2761 2766
2762 2767 /*
2763 2768 * Handle upcalls
2764 2769 */
2765 2770 if (ct->d_args.rbuf == NULL || ct->d_args.rsize < result_size) {
2766 2771 /*
2767 2772 * If there's no return buffer or the buffer is too
2768 2773 * small, allocate a new one. The old buffer (if it
2769 2774 * exists) will be freed by the upcall client.
2770 2775 */
2771 2776 if (result_size > door_max_upcall_reply)
2772 2777 return (E2BIG);
2773 2778 ct->d_args.rsize = result_size;
2774 2779 ct->d_args.rbuf = kmem_alloc(result_size, KM_SLEEP);
2775 2780 }
2776 2781 ct->d_args.data_ptr = ct->d_args.rbuf;
2777 2782 if (data_size != 0 &&
2778 2783 copyin_nowatch(data_ptr, ct->d_args.data_ptr,
2779 2784 data_size) != 0)
2780 2785 return (EFAULT);
2781 2786 } else if (result_size > ct->d_args.rsize) {
2782 2787 return (door_overflow(caller, data_ptr, data_size,
2783 2788 desc_ptr, desc_num));
2784 2789 } else if (data_size != 0) {
2785 2790 if (data_size <= door_max_arg) {
2786 2791 /*
2787 2792 * Use a 2 copy method for small amounts of data
2788 2793 */
2789 2794 if (ct->d_buf == NULL) {
2790 2795 ct->d_bufsize = data_size;
2791 2796 ct->d_buf = kmem_alloc(ct->d_bufsize, KM_SLEEP);
2792 2797 } else if (ct->d_bufsize < data_size) {
2793 2798 kmem_free(ct->d_buf, ct->d_bufsize);
2794 2799 ct->d_bufsize = data_size;
2795 2800 ct->d_buf = kmem_alloc(ct->d_bufsize, KM_SLEEP);
2796 2801 }
2797 2802 if (copyin_nowatch(data_ptr, ct->d_buf, data_size) != 0)
2798 2803 return (EFAULT);
2799 2804 } else {
2800 2805 struct as *as = ttoproc(caller)->p_as;
2801 2806 caddr_t dest = ct->d_args.rbuf;
2802 2807 caddr_t src = data_ptr;
2803 2808 size_t len = data_size;
2804 2809
2805 2810 /* Copy data directly into client */
2806 2811 while (len != 0) {
2807 2812 uint_t amount;
2808 2813 uint_t max;
2809 2814 uint_t off;
2810 2815 int error;
2811 2816
2812 2817 off = (uintptr_t)dest & PAGEOFFSET;
2813 2818 if (off)
2814 2819 max = PAGESIZE - off;
2815 2820 else
2816 2821 max = PAGESIZE;
2817 2822 amount = len > max ? max : len;
2818 2823 error = door_copy(as, src, dest, amount);
2819 2824 if (error != 0)
2820 2825 return (error);
2821 2826 dest += amount;
2822 2827 src += amount;
2823 2828 len -= amount;
2824 2829 }
2825 2830 }
2826 2831 }
2827 2832
2828 2833 /*
2829 2834 * Copyin the returned door ids and translate them into door_node_t
2830 2835 */
2831 2836 if (desc_num != 0) {
2832 2837 door_desc_t *start;
2833 2838 door_desc_t *didpp;
2834 2839 struct file **fpp;
2835 2840 size_t fpp_size;
2836 2841 uint_t i;
2837 2842
2838 2843 /* First, check if we would overflow client */
2839 2844 if (!ufcanalloc(ttoproc(caller), desc_num))
2840 2845 return (EMFILE);
2841 2846
2842 2847 start = didpp = kmem_alloc(dsize, KM_SLEEP);
2843 2848 if (copyin_nowatch(desc_ptr, didpp, dsize)) {
2844 2849 kmem_free(start, dsize);
2845 2850 return (EFAULT);
2846 2851 }
2847 2852 fpp_size = desc_num * sizeof (struct file *);
2848 2853 if (fpp_size > ct->d_fpp_size) {
2849 2854 /* make more space */
2850 2855 if (ct->d_fpp_size)
2851 2856 kmem_free(ct->d_fpp, ct->d_fpp_size);
2852 2857 ct->d_fpp_size = fpp_size;
2853 2858 ct->d_fpp = kmem_alloc(fpp_size, KM_SLEEP);
2854 2859 }
2855 2860 fpp = ct->d_fpp;
2856 2861
2857 2862 for (i = 0; i < desc_num; i++) {
2858 2863 struct file *fp;
2859 2864 int fd = didpp->d_data.d_desc.d_descriptor;
2860 2865
2861 2866 /* Only understand file descriptor results */
2862 2867 if (!(didpp->d_attributes & DOOR_DESCRIPTOR) ||
2863 2868 (fp = getf(fd)) == NULL) {
2864 2869 /* close translated references */
2865 2870 door_fp_close(ct->d_fpp, fpp - ct->d_fpp);
2866 2871 /* close untranslated references */
2867 2872 door_fd_rele(didpp, desc_num - i, 0);
2868 2873 kmem_free(start, dsize);
2869 2874 return (EINVAL);
2870 2875 }
2871 2876
2872 2877 mutex_enter(&fp->f_tlock);
2873 2878 fp->f_count++;
2874 2879 mutex_exit(&fp->f_tlock);
2875 2880
2876 2881 *fpp = fp;
2877 2882 releasef(fd);
2878 2883
2879 2884 if (didpp->d_attributes & DOOR_RELEASE) {
2880 2885 /* release passed reference */
2881 2886 (void) closeandsetf(fd, NULL);
2882 2887 }
2883 2888
2884 2889 fpp++; didpp++;
2885 2890 }
2886 2891 kmem_free(start, dsize);
2887 2892 }
2888 2893 return (0);
2889 2894 }
2890 2895
2891 2896 /*
2892 2897 * Close all the descriptors.
2893 2898 */
2894 2899 static void
2895 2900 door_fd_close(door_desc_t *d, uint_t n)
2896 2901 {
2897 2902 uint_t i;
2898 2903
2899 2904 ASSERT(MUTEX_NOT_HELD(&door_knob));
2900 2905 for (i = 0; i < n; i++) {
2901 2906 if (d->d_attributes & DOOR_DESCRIPTOR) {
2902 2907 (void) closeandsetf(
2903 2908 d->d_data.d_desc.d_descriptor, NULL);
2904 2909 } else if (d->d_attributes & DOOR_HANDLE) {
2905 2910 door_ki_rele(d->d_data.d_handle);
2906 2911 }
2907 2912 d++;
2908 2913 }
2909 2914 }
2910 2915
2911 2916 /*
2912 2917 * Close descriptors that have the DOOR_RELEASE attribute set.
2913 2918 */
2914 2919 void
2915 2920 door_fd_rele(door_desc_t *d, uint_t n, int from_kernel)
2916 2921 {
2917 2922 uint_t i;
2918 2923
2919 2924 ASSERT(MUTEX_NOT_HELD(&door_knob));
2920 2925 for (i = 0; i < n; i++) {
2921 2926 if (d->d_attributes & DOOR_RELEASE) {
2922 2927 if (d->d_attributes & DOOR_DESCRIPTOR) {
2923 2928 (void) closeandsetf(
2924 2929 d->d_data.d_desc.d_descriptor, NULL);
2925 2930 } else if (from_kernel &&
2926 2931 (d->d_attributes & DOOR_HANDLE)) {
2927 2932 door_ki_rele(d->d_data.d_handle);
2928 2933 }
2929 2934 }
2930 2935 d++;
2931 2936 }
2932 2937 }
2933 2938
2934 2939 /*
2935 2940 * Copy descriptors into the kernel so we can release any marked
2936 2941 * DOOR_RELEASE.
2937 2942 */
2938 2943 int
2939 2944 door_release_fds(door_desc_t *desc_ptr, uint_t ndesc)
2940 2945 {
2941 2946 size_t dsize;
2942 2947 door_desc_t *didpp;
2943 2948 uint_t desc_num;
2944 2949
2945 2950 ASSERT(MUTEX_NOT_HELD(&door_knob));
2946 2951 ASSERT(ndesc != 0);
2947 2952
2948 2953 desc_num = MIN(ndesc, door_max_desc);
2949 2954
2950 2955 dsize = desc_num * sizeof (door_desc_t);
2951 2956 didpp = kmem_alloc(dsize, KM_SLEEP);
2952 2957
2953 2958 while (ndesc > 0) {
2954 2959 uint_t count = MIN(ndesc, desc_num);
2955 2960
2956 2961 if (copyin_nowatch(desc_ptr, didpp,
2957 2962 count * sizeof (door_desc_t))) {
2958 2963 kmem_free(didpp, dsize);
2959 2964 return (EFAULT);
2960 2965 }
2961 2966 door_fd_rele(didpp, count, 0);
2962 2967
2963 2968 ndesc -= count;
2964 2969 desc_ptr += count;
2965 2970 }
2966 2971 kmem_free(didpp, dsize);
2967 2972 return (0);
2968 2973 }
2969 2974
2970 2975 /*
2971 2976 * Decrement ref count on all the files passed
2972 2977 */
2973 2978 static void
2974 2979 door_fp_close(struct file **fp, uint_t n)
2975 2980 {
2976 2981 uint_t i;
2977 2982
2978 2983 ASSERT(MUTEX_NOT_HELD(&door_knob));
2979 2984
2980 2985 for (i = 0; i < n; i++)
2981 2986 (void) closef(fp[i]);
2982 2987 }
2983 2988
2984 2989 /*
2985 2990 * Copy data from 'src' in current address space to 'dest' in 'as' for 'len'
2986 2991 * bytes.
2987 2992 *
2988 2993 * Performs this using 1 mapin and 1 copy operation.
2989 2994 *
2990 2995 * We really should do more than 1 page at a time to improve
2991 2996 * performance, but for now this is treated as an anomalous condition.
2992 2997 */
2993 2998 static int
2994 2999 door_copy(struct as *as, caddr_t src, caddr_t dest, uint_t len)
2995 3000 {
2996 3001 caddr_t kaddr;
2997 3002 caddr_t rdest;
2998 3003 uint_t off;
2999 3004 page_t **pplist;
3000 3005 page_t *pp = NULL;
3001 3006 int error = 0;
3002 3007
3003 3008 ASSERT(len <= PAGESIZE);
3004 3009 off = (uintptr_t)dest & PAGEOFFSET; /* offset within the page */
3005 3010 rdest = (caddr_t)((uintptr_t)dest &
3006 3011 (uintptr_t)PAGEMASK); /* Page boundary */
3007 3012 ASSERT(off + len <= PAGESIZE);
3008 3013
3009 3014 /*
3010 3015 * Lock down destination page.
3011 3016 */
3012 3017 if (as_pagelock(as, &pplist, rdest, PAGESIZE, S_WRITE))
3013 3018 return (E2BIG);
3014 3019 /*
3015 3020 * Check if we have a shadow page list from as_pagelock. If not,
3016 3021 * we took the slow path and have to find our page struct the hard
3017 3022 * way.
3018 3023 */
3019 3024 if (pplist == NULL) {
3020 3025 pfn_t pfnum;
3021 3026
3022 3027 /* MMU mapping is already locked down */
3023 3028 AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
3024 3029 pfnum = hat_getpfnum(as->a_hat, rdest);
3025 3030 AS_LOCK_EXIT(as, &as->a_lock);
3026 3031
3027 3032 /*
3028 3033 * TODO: The pfn step should not be necessary - need
3029 3034 * a hat_getpp() function.
3030 3035 */
3031 3036 if (pf_is_memory(pfnum)) {
3032 3037 pp = page_numtopp_nolock(pfnum);
3033 3038 ASSERT(pp == NULL || PAGE_LOCKED(pp));
3034 3039 } else
3035 3040 pp = NULL;
3036 3041 if (pp == NULL) {
3037 3042 as_pageunlock(as, pplist, rdest, PAGESIZE, S_WRITE);
3038 3043 return (E2BIG);
3039 3044 }
3040 3045 } else {
3041 3046 pp = *pplist;
3042 3047 }
3043 3048 /*
3044 3049 * Map destination page into kernel address
3045 3050 */
3046 3051 if (kpm_enable)
3047 3052 kaddr = (caddr_t)hat_kpm_mapin(pp, (struct kpme *)NULL);
3048 3053 else
3049 3054 kaddr = (caddr_t)ppmapin(pp, PROT_READ | PROT_WRITE,
3050 3055 (caddr_t)-1);
3051 3056
3052 3057 /*
3053 3058 * Copy from src to dest
3054 3059 */
3055 3060 if (copyin_nowatch(src, kaddr + off, len) != 0)
3056 3061 error = EFAULT;
3057 3062 /*
3058 3063 * Unmap destination page from kernel
3059 3064 */
3060 3065 if (kpm_enable)
3061 3066 hat_kpm_mapout(pp, (struct kpme *)NULL, kaddr);
3062 3067 else
3063 3068 ppmapout(kaddr);
3064 3069 /*
3065 3070 * Unlock destination page
3066 3071 */
3067 3072 as_pageunlock(as, pplist, rdest, PAGESIZE, S_WRITE);
3068 3073 return (error);
3069 3074 }
3070 3075
3071 3076 /*
3072 3077 * General kernel upcall using doors
3073 3078 * Returns 0 on success, errno for failures.
3074 3079 * Caller must have a hold on the door based vnode, and on any
3075 3080 * references passed in desc_ptr. The references are released
3076 3081 * in the event of an error, and passed without duplication
3077 3082 * otherwise. Note that param->rbuf must be 64-bit aligned in
3078 3083 * a 64-bit kernel, since it may be used to store door descriptors
3079 3084 * if they are returned by the server. The caller is responsible
3080 3085 * for holding a reference to the cred passed in.
3081 3086 */
3082 3087 int
3083 3088 door_upcall(vnode_t *vp, door_arg_t *param, struct cred *cred,
3084 3089 size_t max_data, uint_t max_descs)
3085 3090 {
3086 3091 /* Locals */
3087 3092 door_upcall_t *dup;
3088 3093 door_node_t *dp;
3089 3094 kthread_t *server_thread;
3090 3095 int error = 0;
3091 3096 klwp_t *lwp;
3092 3097 door_client_t *ct; /* curthread door_data */
3093 3098 door_server_t *st; /* server thread door_data */
3094 3099 int gotresults = 0;
3095 3100 int cancel_pending;
3096 3101
3097 3102 if (vp->v_type != VDOOR) {
3098 3103 if (param->desc_num)
3099 3104 door_fd_rele(param->desc_ptr, param->desc_num, 1);
3100 3105 return (EINVAL);
3101 3106 }
3102 3107
3103 3108 lwp = ttolwp(curthread);
3104 3109 ct = door_my_client(1);
3105 3110 dp = VTOD(vp); /* Convert to a door_node_t */
3106 3111
3107 3112 dup = kmem_zalloc(sizeof (*dup), KM_SLEEP);
3108 3113 dup->du_cred = (cred != NULL) ? cred : curthread->t_cred;
3109 3114 dup->du_max_data = max_data;
3110 3115 dup->du_max_descs = max_descs;
3111 3116
3112 3117 /*
3113 3118 * This should be done in shuttle_resume(), just before going to
3114 3119 * sleep, but we want to avoid overhead while holding door_knob.
3115 3120 * prstop() is just a no-op if we don't really go to sleep.
3116 3121 * We test not-kernel-address-space for the sake of clustering code.
3117 3122 */
3118 3123 if (lwp && lwp->lwp_nostop == 0 && curproc->p_as != &kas)
3119 3124 prstop(PR_REQUESTED, 0);
3120 3125
3121 3126 mutex_enter(&door_knob);
3122 3127 if (DOOR_INVALID(dp)) {
3123 3128 mutex_exit(&door_knob);
3124 3129 if (param->desc_num)
3125 3130 door_fd_rele(param->desc_ptr, param->desc_num, 1);
3126 3131 error = EBADF;
3127 3132 goto out;
3128 3133 }
3129 3134
3130 3135 if (dp->door_target == &p0) {
3131 3136 /* Can't do an upcall to a kernel server */
3132 3137 mutex_exit(&door_knob);
3133 3138 if (param->desc_num)
3134 3139 door_fd_rele(param->desc_ptr, param->desc_num, 1);
3135 3140 error = EINVAL;
3136 3141 goto out;
3137 3142 }
3138 3143
3139 3144 error = door_check_limits(dp, param, 1);
3140 3145 if (error != 0) {
3141 3146 mutex_exit(&door_knob);
3142 3147 if (param->desc_num)
3143 3148 door_fd_rele(param->desc_ptr, param->desc_num, 1);
3144 3149 goto out;
3145 3150 }
3146 3151
3147 3152 /*
3148 3153 * Get a server thread from the target domain
3149 3154 */
3150 3155 if ((server_thread = door_get_server(dp)) == NULL) {
3151 3156 if (DOOR_INVALID(dp))
3152 3157 error = EBADF;
3153 3158 else
3154 3159 error = EAGAIN;
3155 3160 mutex_exit(&door_knob);
3156 3161 if (param->desc_num)
3157 3162 door_fd_rele(param->desc_ptr, param->desc_num, 1);
3158 3163 goto out;
3159 3164 }
3160 3165
3161 3166 st = DOOR_SERVER(server_thread->t_door);
3162 3167 ct->d_buf = param->data_ptr;
3163 3168 ct->d_bufsize = param->data_size;
3164 3169 ct->d_args = *param; /* structure assignment */
3165 3170
3166 3171 if (ct->d_args.desc_num) {
3167 3172 /*
3168 3173 * Move data from client to server
3169 3174 */
3170 3175 DOOR_T_HOLD(st);
3171 3176 mutex_exit(&door_knob);
3172 3177 error = door_translate_out();
3173 3178 mutex_enter(&door_knob);
3174 3179 DOOR_T_RELEASE(st);
3175 3180 if (error) {
3176 3181 /*
3177 3182 * We're not going to resume this thread after all
3178 3183 */
3179 3184 door_release_server(dp, server_thread);
3180 3185 shuttle_sleep(server_thread);
3181 3186 mutex_exit(&door_knob);
3182 3187 goto out;
3183 3188 }
3184 3189 }
3185 3190
3186 3191 ct->d_upcall = dup;
3187 3192 if (param->rsize == 0)
3188 3193 ct->d_noresults = 1;
3189 3194 else
3190 3195 ct->d_noresults = 0;
3191 3196
3192 3197 dp->door_active++;
3193 3198
3194 3199 ct->d_error = DOOR_WAIT;
3195 3200 st->d_caller = curthread;
3196 3201 st->d_active = dp;
3197 3202
3198 3203 shuttle_resume(server_thread, &door_knob);
3199 3204
3200 3205 mutex_enter(&door_knob);
3201 3206 shuttle_return:
3202 3207 if ((error = ct->d_error) < 0) { /* DOOR_WAIT or DOOR_EXIT */
3203 3208 /*
3204 3209 * Premature wakeup. Find out why (stop, forkall, sig, exit ...)
3205 3210 */
3206 3211 mutex_exit(&door_knob); /* May block in ISSIG */
3207 3212 cancel_pending = 0;
3208 3213 if (lwp && (ISSIG(curthread, FORREAL) || lwp->lwp_sysabort ||
3209 3214 MUSTRETURN(curproc, curthread) ||
3210 3215 (cancel_pending = schedctl_cancel_pending()) != 0)) {
3211 3216 /* Signal, forkall, ... */
3212 3217 if (cancel_pending)
3213 3218 schedctl_cancel_eintr();
3214 3219 lwp->lwp_sysabort = 0;
3215 3220 mutex_enter(&door_knob);
3216 3221 error = EINTR;
3217 3222 /*
3218 3223 * If the server has finished processing our call,
3219 3224 * or exited (calling door_slam()), then d_error
3220 3225 * will have changed. If the server hasn't finished
3221 3226 * yet, d_error will still be DOOR_WAIT, and we
3222 3227 * let it know we are not interested in any
3223 3228 * results by sending a SIGCANCEL, unless the door
3224 3229 * is marked with DOOR_NO_CANCEL.
3225 3230 */
3226 3231 if (ct->d_error == DOOR_WAIT &&
3227 3232 st->d_caller == curthread) {
3228 3233 proc_t *p = ttoproc(server_thread);
3229 3234
3230 3235 st->d_active = NULL;
3231 3236 st->d_caller = NULL;
3232 3237 if (!(dp->door_flags & DOOR_NO_CANCEL)) {
3233 3238 DOOR_T_HOLD(st);
3234 3239 mutex_exit(&door_knob);
3235 3240
3236 3241 mutex_enter(&p->p_lock);
3237 3242 sigtoproc(p, server_thread, SIGCANCEL);
3238 3243 mutex_exit(&p->p_lock);
3239 3244
3240 3245 mutex_enter(&door_knob);
3241 3246 DOOR_T_RELEASE(st);
3242 3247 }
3243 3248 }
3244 3249 } else {
3245 3250 /*
3246 3251 * Return from stop(), server exit...
3247 3252 *
3248 3253 * Note that the server could have done a
3249 3254 * door_return while the client was in stop state
3250 3255 * (ISSIG), in which case the error condition
3251 3256 * is updated by the server.
3252 3257 */
3253 3258 mutex_enter(&door_knob);
3254 3259 if (ct->d_error == DOOR_WAIT) {
3255 3260 /* Still waiting for a reply */
3256 3261 shuttle_swtch(&door_knob);
3257 3262 mutex_enter(&door_knob);
3258 3263 if (lwp)
3259 3264 lwp->lwp_asleep = 0;
3260 3265 goto shuttle_return;
3261 3266 } else if (ct->d_error == DOOR_EXIT) {
3262 3267 /* Server exit */
3263 3268 error = EINTR;
3264 3269 } else {
3265 3270 /* Server did a door_return during ISSIG */
3266 3271 error = ct->d_error;
3267 3272 }
3268 3273 }
3269 3274 /*
3270 3275 * Can't exit if the server is currently copying
3271 3276 * results for me
3272 3277 */
3273 3278 while (DOOR_T_HELD(ct))
3274 3279 cv_wait(&ct->d_cv, &door_knob);
3275 3280
3276 3281 /*
3277 3282 * Find out if results were successfully copied.
3278 3283 */
3279 3284 if (ct->d_error == 0)
3280 3285 gotresults = 1;
3281 3286 }
3282 3287 if (lwp) {
3283 3288 lwp->lwp_asleep = 0; /* /proc */
3284 3289 lwp->lwp_sysabort = 0; /* /proc */
3285 3290 }
3286 3291 if (--dp->door_active == 0 && (dp->door_flags & DOOR_DELAY))
3287 3292 door_deliver_unref(dp);
3288 3293 mutex_exit(&door_knob);
3289 3294
3290 3295 /*
3291 3296 * Translate returned doors (if any)
3292 3297 */
3293 3298
3294 3299 if (ct->d_noresults)
3295 3300 goto out;
3296 3301
3297 3302 if (error) {
3298 3303 /*
3299 3304 * If server returned results successfully, then we've
3300 3305 * been interrupted and may need to clean up.
3301 3306 */
3302 3307 if (gotresults) {
3303 3308 ASSERT(error == EINTR);
3304 3309 door_fp_close(ct->d_fpp, ct->d_args.desc_num);
3305 3310 }
3306 3311 goto out;
3307 3312 }
3308 3313
3309 3314 if (ct->d_args.desc_num) {
3310 3315 struct file **fpp;
3311 3316 door_desc_t *didpp;
3312 3317 vnode_t *vp;
3313 3318 uint_t n = ct->d_args.desc_num;
3314 3319
3315 3320 didpp = ct->d_args.desc_ptr = (door_desc_t *)(ct->d_args.rbuf +
3316 3321 roundup(ct->d_args.data_size, sizeof (door_desc_t)));
3317 3322 fpp = ct->d_fpp;
3318 3323
3319 3324 while (n--) {
3320 3325 struct file *fp;
3321 3326
3322 3327 fp = *fpp;
3323 3328 if (VOP_REALVP(fp->f_vnode, &vp, NULL))
3324 3329 vp = fp->f_vnode;
3325 3330
3326 3331 didpp->d_attributes = DOOR_HANDLE |
3327 3332 (VTOD(vp)->door_flags & DOOR_ATTR_MASK);
3328 3333 didpp->d_data.d_handle = FTODH(fp);
3329 3334
3330 3335 fpp++; didpp++;
3331 3336 }
3332 3337 }
3333 3338
3334 3339 /* on return data is in rbuf */
3335 3340 *param = ct->d_args; /* structure assignment */
3336 3341
3337 3342 out:
3338 3343 kmem_free(dup, sizeof (*dup));
3339 3344
3340 3345 if (ct->d_fpp) {
3341 3346 kmem_free(ct->d_fpp, ct->d_fpp_size);
3342 3347 ct->d_fpp = NULL;
3343 3348 ct->d_fpp_size = 0;
3344 3349 }
3345 3350
3346 3351 ct->d_upcall = NULL;
3347 3352 ct->d_noresults = 0;
3348 3353 ct->d_buf = NULL;
3349 3354 ct->d_bufsize = 0;
3350 3355 return (error);
3351 3356 }
3352 3357
3353 3358 /*
3354 3359 * Add a door to the per-process list of active doors for which the
3355 3360 * process is a server.
3356 3361 */
3357 3362 static void
3358 3363 door_list_insert(door_node_t *dp)
3359 3364 {
3360 3365 proc_t *p = dp->door_target;
3361 3366
3362 3367 ASSERT(MUTEX_HELD(&door_knob));
3363 3368 dp->door_list = p->p_door_list;
3364 3369 p->p_door_list = dp;
3365 3370 }
3366 3371
3367 3372 /*
3368 3373 * Remove a door from the per-process list of active doors.
3369 3374 */
3370 3375 void
3371 3376 door_list_delete(door_node_t *dp)
3372 3377 {
3373 3378 door_node_t **pp;
3374 3379
3375 3380 ASSERT(MUTEX_HELD(&door_knob));
3376 3381 /*
3377 3382 * Find the door in the list. If the door belongs to another process,
3378 3383 * it's OK to use p_door_list since that process can't exit until all
3379 3384 * doors have been taken off the list (see door_exit).
3380 3385 */
3381 3386 pp = &(dp->door_target->p_door_list);
3382 3387 while (*pp != dp)
3383 3388 pp = &((*pp)->door_list);
3384 3389
3385 3390 /* found it, take it off the list */
3386 3391 *pp = dp->door_list;
3387 3392 }
3388 3393
3389 3394
3390 3395 /*
3391 3396 * External kernel interfaces for doors. These functions are available
3392 3397 * outside the doorfs module for use in creating and using doors from
3393 3398 * within the kernel.
3394 3399 */
3395 3400
3396 3401 /*
3397 3402 * door_ki_upcall invokes a user-level door server from the kernel, with
3398 3403 * the credentials associated with curthread.
3399 3404 */
3400 3405 int
3401 3406 door_ki_upcall(door_handle_t dh, door_arg_t *param)
3402 3407 {
3403 3408 return (door_ki_upcall_limited(dh, param, NULL, SIZE_MAX, UINT_MAX));
3404 3409 }
3405 3410
3406 3411 /*
3407 3412 * door_ki_upcall_limited invokes a user-level door server from the
3408 3413 * kernel with the given credentials and reply limits. If the "cred"
3409 3414 * argument is NULL, uses the credentials associated with current
3410 3415 * thread. max_data limits the maximum length of the returned data (the
3411 3416 * client will get E2BIG if they go over), and max_desc limits the
3412 3417 * number of returned descriptors (the client will get EMFILE if they
3413 3418 * go over).
3414 3419 */
3415 3420 int
3416 3421 door_ki_upcall_limited(door_handle_t dh, door_arg_t *param, struct cred *cred,
3417 3422 size_t max_data, uint_t max_desc)
3418 3423 {
3419 3424 file_t *fp = DHTOF(dh);
3420 3425 vnode_t *realvp;
3421 3426
3422 3427 if (VOP_REALVP(fp->f_vnode, &realvp, NULL))
3423 3428 realvp = fp->f_vnode;
3424 3429 return (door_upcall(realvp, param, cred, max_data, max_desc));
3425 3430 }
3426 3431
3427 3432 /*
3428 3433 * Function call to create a "kernel" door server. A kernel door
3429 3434 * server provides a way for a user-level process to invoke a function
3430 3435 * in the kernel through a door_call. From the caller's point of
3431 3436 * view, a kernel door server looks the same as a user-level one
3432 3437 * (except the server pid is 0). Unlike normal door calls, the
3433 3438 * kernel door function is invoked via a normal function call in the
3434 3439 * same thread and context as the caller.
3435 3440 */
3436 3441 int
3437 3442 door_ki_create(void (*pc_cookie)(), void *data_cookie, uint_t attributes,
3438 3443 door_handle_t *dhp)
3439 3444 {
3440 3445 int err;
3441 3446 file_t *fp;
3442 3447
3443 3448 /* no DOOR_PRIVATE */
3444 3449 if ((attributes & ~DOOR_KI_CREATE_MASK) ||
3445 3450 (attributes & (DOOR_UNREF | DOOR_UNREF_MULTI)) ==
3446 3451 (DOOR_UNREF | DOOR_UNREF_MULTI))
3447 3452 return (EINVAL);
3448 3453
3449 3454 err = door_create_common(pc_cookie, data_cookie, attributes,
3450 3455 1, NULL, &fp);
3451 3456 if (err == 0 && (attributes & (DOOR_UNREF | DOOR_UNREF_MULTI)) &&
3452 3457 p0.p_unref_thread == 0) {
3453 3458 /* need to create unref thread for process 0 */
3454 3459 (void) thread_create(NULL, 0, door_unref_kernel, NULL, 0, &p0,
3455 3460 TS_RUN, minclsyspri);
3456 3461 }
3457 3462 if (err == 0) {
3458 3463 *dhp = FTODH(fp);
3459 3464 }
3460 3465 return (err);
3461 3466 }
3462 3467
3463 3468 void
3464 3469 door_ki_hold(door_handle_t dh)
3465 3470 {
3466 3471 file_t *fp = DHTOF(dh);
3467 3472
3468 3473 mutex_enter(&fp->f_tlock);
3469 3474 fp->f_count++;
3470 3475 mutex_exit(&fp->f_tlock);
3471 3476 }
3472 3477
3473 3478 void
3474 3479 door_ki_rele(door_handle_t dh)
3475 3480 {
3476 3481 file_t *fp = DHTOF(dh);
3477 3482
3478 3483 (void) closef(fp);
3479 3484 }
3480 3485
3481 3486 int
3482 3487 door_ki_open(char *pathname, door_handle_t *dhp)
3483 3488 {
3484 3489 file_t *fp;
3485 3490 vnode_t *vp;
3486 3491 int err;
3487 3492
3488 3493 if ((err = lookupname(pathname, UIO_SYSSPACE, FOLLOW, NULL, &vp)) != 0)
3489 3494 return (err);
3490 3495 if (err = VOP_OPEN(&vp, FREAD, kcred, NULL)) {
3491 3496 VN_RELE(vp);
3492 3497 return (err);
3493 3498 }
3494 3499 if (vp->v_type != VDOOR) {
3495 3500 VN_RELE(vp);
3496 3501 return (EINVAL);
3497 3502 }
3498 3503 if ((err = falloc(vp, FREAD | FWRITE, &fp, NULL)) != 0) {
3499 3504 VN_RELE(vp);
3500 3505 return (err);
3501 3506 }
3502 3507 /* falloc returns with f_tlock held on success */
3503 3508 mutex_exit(&fp->f_tlock);
3504 3509 *dhp = FTODH(fp);
3505 3510 return (0);
3506 3511 }
3507 3512
3508 3513 int
3509 3514 door_ki_info(door_handle_t dh, struct door_info *dip)
3510 3515 {
3511 3516 file_t *fp = DHTOF(dh);
3512 3517 vnode_t *vp;
3513 3518
3514 3519 if (VOP_REALVP(fp->f_vnode, &vp, NULL))
3515 3520 vp = fp->f_vnode;
3516 3521 if (vp->v_type != VDOOR)
3517 3522 return (EINVAL);
3518 3523 door_info_common(VTOD(vp), dip, fp);
3519 3524 return (0);
3520 3525 }
3521 3526
3522 3527 door_handle_t
3523 3528 door_ki_lookup(int did)
3524 3529 {
3525 3530 file_t *fp;
3526 3531 door_handle_t dh;
3527 3532
3528 3533 /* is the descriptor really a door? */
3529 3534 if (door_lookup(did, &fp) == NULL)
3530 3535 return (NULL);
3531 3536 /* got the door, put a hold on it and release the fd */
3532 3537 dh = FTODH(fp);
3533 3538 door_ki_hold(dh);
3534 3539 releasef(did);
3535 3540 return (dh);
3536 3541 }
3537 3542
3538 3543 int
3539 3544 door_ki_setparam(door_handle_t dh, int type, size_t val)
3540 3545 {
3541 3546 file_t *fp = DHTOF(dh);
3542 3547 vnode_t *vp;
3543 3548
3544 3549 if (VOP_REALVP(fp->f_vnode, &vp, NULL))
3545 3550 vp = fp->f_vnode;
3546 3551 if (vp->v_type != VDOOR)
3547 3552 return (EINVAL);
3548 3553 return (door_setparam_common(VTOD(vp), 1, type, val));
3549 3554 }
3550 3555
3551 3556 int
3552 3557 door_ki_getparam(door_handle_t dh, int type, size_t *out)
3553 3558 {
3554 3559 file_t *fp = DHTOF(dh);
3555 3560 vnode_t *vp;
3556 3561
3557 3562 if (VOP_REALVP(fp->f_vnode, &vp, NULL))
3558 3563 vp = fp->f_vnode;
3559 3564 if (vp->v_type != VDOOR)
3560 3565 return (EINVAL);
3561 3566 return (door_getparam_common(VTOD(vp), type, out));
3562 3567 }
↓ open down ↓ |
1766 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX