2 * Copyright (c) 2014,2019 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "namespace.h"
36 #include <sys/cdefs.h>
37 #include <sys/types.h>
38 #include <sys/syscall.h>
39 #include <sys/upmap.h>
42 #include <sys/fcntl.h>
48 #include "un-namespace.h"
49 #include "libc_private.h"
53 * kpmap - Global user/kernel shared map (RO)
54 * upmap - Per-process user/kernel shared map (RW)
55 * lpmap - Per-thread user/kernel shared map (RW)
57 static pthread_mutex_t ukpmap_lock;
58 static ukpheader_t *__kpmap_headers;
59 static ukpheader_t *__upmap_headers;
60 __thread ukpheader_t *__lpmap_headers TLS_ATTRIBUTE;
61 __thread uint32_t *__lpmap_blockallsigs TLS_ATTRIBUTE;
63 static __thread int lpmap_ok;
66 static pthread_once_t upmap_once = PTHREAD_ONCE_INIT;
70 * Map the requested data item from the user-kernel global shared mmap
72 * *state is set to -1 on failure, else it is left alone.
73 * *datap is set to a pointer to the item on success, else it is left alone.
74 * If type == 0 this function finalizes state, setting it to 1 if it is 0.
77 __kpmap_map(void *datap, int *state, uint16_t type)
82 _pthread_mutex_lock(&ukpmap_lock);
89 fd = _open("/dev/kpmap", O_RDONLY);
94 __kpmap_headers = mmap(NULL, KPMAP_MAPSIZE,
95 PROT_READ, MAP_SHARED | MAP_FILE,
98 if ((void *)__kpmap_headers == MAP_FAILED) {
106 * Special case to finalize state
112 _pthread_mutex_unlock(&ukpmap_lock);
119 for (head = __kpmap_headers; head->type; ++head) {
120 if (head->type == type) {
121 *(void **)datap = (char *)__kpmap_headers +
124 _pthread_mutex_unlock(&ukpmap_lock);
131 _pthread_mutex_unlock(&ukpmap_lock);
135 * Map the requested data item from the user-kernel per-process shared mmap
137 * *state is set to -1 on failure, else it is left alone.
138 * *datap is set to a pointer to the item on success, else it is left alone.
139 * If type == 0 this function finalizes state, setting it to 1 if it is 0.
142 __upmap_map(void *datap, int *state, uint16_t type)
147 _pthread_mutex_lock(&ukpmap_lock);
154 fd = _open("/dev/upmap", O_RDWR);
159 __upmap_headers = mmap(NULL, UPMAP_MAPSIZE,
160 PROT_READ | PROT_WRITE,
161 MAP_SHARED | MAP_FILE,
164 if ((void *)__upmap_headers == MAP_FAILED) {
172 * Special case to finalize state
178 _pthread_mutex_unlock(&ukpmap_lock);
185 for (head = __upmap_headers; head->type; ++head) {
186 if (head->type == type) {
187 *(void **)datap = (char *)__upmap_headers +
190 _pthread_mutex_unlock(&ukpmap_lock);
197 _pthread_mutex_unlock(&ukpmap_lock);
201 * Map the requested data item from the user-kernel per-thread shared mmap
203 * *state is set to -1 on failure, else it is left alone.
204 * *datap is set to a pointer to the item on success, else it is left alone.
205 * If type == 0 this function finalizes state, setting it to 1 if it is 0.
207 * WARNING! This code is used all over pthreads and must NOT make any
208 * reentrant pthreads calls until after the mapping has been
211 static pthread_key_t lpmap_key;
213 static void lpmap_unmap(void **datap);
216 __lpmap_map(void *datap, int *state, uint16_t type)
225 fd = _open("/dev/lpmap", O_RDWR);
230 __lpmap_headers = mmap(NULL, LPMAP_MAPSIZE,
231 PROT_READ | PROT_WRITE,
232 MAP_SHARED | MAP_FILE,
235 if ((void *)__lpmap_headers == MAP_FAILED) {
240 _pthread_setspecific(lpmap_key, &__lpmap_headers);
244 * Special case to finalize state
255 for (head = __lpmap_headers; head->type; ++head) {
256 if (head->type == type) {
257 *(void **)datap = (char *)__lpmap_headers +
267 * Cleanup thread state
270 lpmap_unmap(void **datap)
272 ukpheader_t *lpmap = *datap;
276 __lpmap_blockallsigs = NULL;
278 munmap(lpmap, LPMAP_MAPSIZE);
283 * upmap initialization code, _upmap_thr_init() is called for the initial
284 * main thread by libc or pthreads, and on every thread create. We need
285 * the __lpmap_blockallsigs pointer ASAP because it is used everywhere in
288 * If pthreads is not linked in, _pthread_once() still runs via a stub in
289 * libc, and _pthread_key_create() is a NOP.
291 * NOTE: These pthreads calls are stubs when pthreads is not linked in.
292 * The once routine will still be run once regardless.
296 _upmap_init_once(void)
298 /* ignore error from stub if not threaded */
299 _pthread_key_create(&lpmap_key, (void (*)(void *))lpmap_unmap);
303 _upmap_thr_init(void)
307 _pthread_once(&upmap_once, _upmap_init_once);
308 __lpmap_map(&__lpmap_blockallsigs, &dummy_state, LPTYPE_BLOCKALLSIGS);