1 /* $OpenBSD: rthread_np.c,v 1.5 2007/07/08 01:53:46 kurt Exp $ */ 2 /* 3 * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org> 4 * Copyright (c) 2005 Otto Moerbeek <otto@openbsd.org> 5 * All Rights Reserved. 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/param.h> 21 #include <sys/time.h> 22 #include <sys/lock.h> 23 #include <sys/resource.h> 24 #include <sys/queue.h> 25 26 #include <errno.h> 27 #include <pthread.h> 28 #include <pthread_np.h> 29 #include <stddef.h> 30 #include <string.h> 31 #include <unistd.h> 32 33 #include <uvm/uvm_extern.h> 34 #include <machine/spinlock.h> 35 36 #include "rthread.h" 37 38 void 39 pthread_set_name_np(pthread_t thread, const char *name) 40 { 41 strlcpy(thread->name, name, sizeof(thread->name)); 42 } 43 44 int 45 pthread_main_np(void) 46 { 47 return (!_threads_ready || getthrid() == _initial_thread.tid ? 1 : 0); 48 } 49 50 51 /* 52 * Return stack info from the given thread. Based upon the solaris 53 * thr_stksegment function. 54 * 55 * This function taken from the uthread library, with the following 56 * license: 57 * PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */ 58 int 59 pthread_stackseg_np(pthread_t thread, stack_t *sinfo) 60 { 61 char *base; 62 size_t pgsz; 63 int ret; 64 struct rlimit rl; 65 66 if (thread->stack) { 67 base = thread->stack->base; 68 #if !defined(MACHINE_STACK_GROWS_UP) 69 base += (ptrdiff_t)thread->stack->len; 70 #endif 71 sinfo->ss_sp = base; 72 sinfo->ss_size = thread->stack->len; 73 sinfo->ss_flags = 0; 74 ret = 0; 75 } else if (thread == &_initial_thread) { 76 if (getrlimit(RLIMIT_STACK, &rl) != 0) 77 return (EAGAIN); 78 pgsz = (size_t)sysconf(_SC_PAGESIZE); 79 if (pgsz == (size_t)-1) 80 return (EAGAIN); 81 /* 82 * round_page() stack rlim_cur and 83 * trunc_page() USRSTACK to be consistent with 84 * the way the kernel sets up the stack. 85 */ 86 sinfo->ss_size = (size_t)rl.rlim_cur; 87 sinfo->ss_size += (pgsz - 1); 88 sinfo->ss_size &= ~(pgsz - 1); 89 sinfo->ss_sp = (caddr_t) (USRSTACK & ~(pgsz - 1)); 90 sinfo->ss_flags = 0; 91 ret = 0; 92 93 } else 94 ret = EAGAIN; 95 96 return ret; 97 } 98