| 1 | /* Copyright (C) 1994-2024 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <https://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #include <mach.h> |
| 20 | #include <mach/mig_support.h> |
| 21 | #include <tls.h> |
| 22 | |
| 23 | /* These functions are called by MiG-generated code. */ |
| 24 | |
| 25 | #if !defined (SHARED) || IS_IN (rtld) |
| 26 | mach_port_t __hurd_reply_port0; |
| 27 | #endif |
| 28 | |
| 29 | static mach_port_t |
| 30 | get_reply_port (void) |
| 31 | { |
| 32 | #if !defined (SHARED) || IS_IN (rtld) |
| 33 | if (__LIBC_NO_TLS ()) |
| 34 | return __hurd_reply_port0; |
| 35 | #endif |
| 36 | return THREAD_GETMEM (THREAD_SELF, reply_port); |
| 37 | } |
| 38 | |
| 39 | static void |
| 40 | set_reply_port (mach_port_t port) |
| 41 | { |
| 42 | #if !defined (SHARED) || IS_IN (rtld) |
| 43 | if (__LIBC_NO_TLS ()) |
| 44 | __hurd_reply_port0 = port; |
| 45 | else |
| 46 | #endif |
| 47 | THREAD_SETMEM (THREAD_SELF, reply_port, port); |
| 48 | } |
| 49 | |
| 50 | /* Called by MiG to get a reply port. */ |
| 51 | mach_port_t |
| 52 | __mig_get_reply_port (void) |
| 53 | { |
| 54 | mach_port_t port = get_reply_port (); |
| 55 | if (__glibc_unlikely (port == MACH_PORT_NULL)) |
| 56 | { |
| 57 | port = __mach_reply_port (); |
| 58 | set_reply_port (port); |
| 59 | } |
| 60 | return port; |
| 61 | } |
| 62 | weak_alias (__mig_get_reply_port, mig_get_reply_port) |
| 63 | libc_hidden_def (__mig_get_reply_port) |
| 64 | |
| 65 | /* Called by MiG to deallocate the reply port. */ |
| 66 | void |
| 67 | __mig_dealloc_reply_port (mach_port_t arg) |
| 68 | { |
| 69 | error_t err; |
| 70 | mach_port_t port = get_reply_port (); |
| 71 | |
| 72 | set_reply_port (MACH_PORT_NULL); /* So the mod_refs RPC won't use it. */ |
| 73 | assert (port == arg); |
| 74 | if (!MACH_PORT_VALID (port)) |
| 75 | return; |
| 76 | |
| 77 | err = __mach_port_mod_refs (__mach_task_self (), port, |
| 78 | MACH_PORT_RIGHT_RECEIVE, -1); |
| 79 | if (err == KERN_INVALID_RIGHT) |
| 80 | /* It could be that during signal handling, the receive right had been |
| 81 | replaced with a dead name. */ |
| 82 | err = __mach_port_mod_refs (__mach_task_self (), port, |
| 83 | MACH_PORT_RIGHT_DEAD_NAME, -1); |
| 84 | |
| 85 | assert_perror (err); |
| 86 | } |
| 87 | weak_alias (__mig_dealloc_reply_port, mig_dealloc_reply_port) |
| 88 | libc_hidden_def (__mig_dealloc_reply_port) |
| 89 | |
| 90 | /* Called by mig interfaces when done with a port. Used to provide the |
| 91 | same interface as needed when a custom allocator is used. */ |
| 92 | void |
| 93 | __mig_put_reply_port(mach_port_t port) |
| 94 | { |
| 95 | /* Do nothing. */ |
| 96 | } |
| 97 | weak_alias (__mig_put_reply_port, mig_put_reply_port) |
| 98 | |
| 99 | /* Called at startup with STACK == NULL. When per-thread variables are set |
| 100 | up, this is called again with STACK set to the new stack being switched |
| 101 | to, where per-thread variables should be set up. */ |
| 102 | void |
| 103 | __mig_init (void *stack) |
| 104 | { |
| 105 | /* Do nothing. */ |
| 106 | } |
| 107 | weak_alias (__mig_init, mig_init) |
| 108 | libc_hidden_def (__mig_init) |
| 109 | |