├── xcodeconfig ├── libsimple.xcconfig ├── libplatform.aliases ├── static.xcconfig ├── atomics.xcconfig ├── libplatform-exclavekit.xcconfig ├── perarch.xcconfig └── os.xcconfig ├── src ├── force_libplatform_to_build.c ├── os │ ├── resolver │ │ ├── resolver.h │ │ └── resolver_internal.h │ ├── apt_private.c │ ├── lock_internal.h │ └── semaphore.c ├── atomics │ ├── OSAtomicFifo.h │ ├── x86_64 │ │ ├── OSAtomicFifo.c │ │ └── pfz.s │ ├── init.c │ ├── i386 │ │ └── pfz.s │ ├── common │ │ └── MKGetTimeBaseInfo.c │ └── arm64 │ │ └── OSAtomic.c ├── timingsafe │ ├── x86_64 │ │ └── timingsafe.c │ └── arm64 │ │ └── timingsafe.c ├── ucontext │ ├── i386 │ │ ├── _setcontext.s │ │ ├── getcontext.s │ │ └── _ctx_start.s │ ├── arm64 │ │ ├── _ctx_start.s │ │ ├── asm_help.h │ │ └── mcontext.c │ ├── x86_64 │ │ ├── _setcontext.s │ │ ├── getcontext.s │ │ └── _ctx_start.s │ └── generic │ │ ├── setcontext.c │ │ └── swapcontext.c ├── setjmp │ ├── arm │ │ ├── _setjmp.s │ │ ├── _longjmp.s │ │ ├── _setjmp.h │ │ ├── longjmp.s │ │ └── setjmp.s │ ├── generic │ │ ├── sigaction.c │ │ └── setjmperr.c │ ├── x86_64 │ │ └── _setjmp.s │ └── i386 │ │ ├── _setjmp.s │ │ └── setjmp.s ├── cachecontrol │ ├── arm │ │ └── cache.s │ ├── generic │ │ └── cache.c │ ├── x86_64 │ │ └── cache.s │ ├── i386 │ │ └── cache.s │ └── arm64 │ │ └── cache.s ├── string │ └── generic │ │ ├── strcpy.c │ │ ├── strlcpy.c │ │ ├── memccpy.c │ │ ├── strlcat.c │ │ ├── strnlen.c │ │ ├── strncpy.c │ │ ├── strchr.c │ │ ├── bzero.c │ │ ├── strcmp.c │ │ ├── strncmp.c │ │ ├── memchr.c │ │ ├── memcmp.c │ │ ├── strstr.c │ │ ├── memset_pattern.c │ │ ├── memcmp_zero.c │ │ ├── ffsll.c │ │ ├── flsll.c │ │ └── strlen.c ├── simple │ └── getenv.c ├── init.c └── exclavekit │ └── lock.c ├── private ├── os │ ├── security_config_private.h │ ├── log_simple_private.h │ ├── once_private.h │ ├── apt_private.h │ ├── semaphore_private.h │ └── alloc_once_impl.h ├── libkern │ └── OSAtomicPrivate.h └── platform │ └── compat.h ├── man ├── cache.3 ├── atomic.3 ├── spinlock_deprecated.3 ├── manpages.lst ├── ffs.3 ├── ucontext.3 ├── makecontext.3 └── getcontext.3 ├── include ├── libkern │ ├── OSAtomic.h │ └── OSCacheControl.h ├── ucontext.h ├── string_x86.h ├── timingsafe.h └── setjmp.h └── internal └── os ├── yield.h ├── internal.h └── internal_asm.h /xcodeconfig/libsimple.xcconfig: -------------------------------------------------------------------------------- 1 | #include "libplatform.xcconfig" 2 | 3 | INSTALLHDRS_SCRIPT_PHASE = YES 4 | INSTALLHDRS_COPY_PHASE = YES 5 | -------------------------------------------------------------------------------- /xcodeconfig/libplatform.aliases: -------------------------------------------------------------------------------- 1 | __platform_bzero ___bzero 2 | __os_lock_type_spin __os_lock_type_eliding 3 | __os_lock_type_spin __os_lock_type_transactional 4 | -------------------------------------------------------------------------------- /xcodeconfig/static.xcconfig: -------------------------------------------------------------------------------- 1 | #include "libplatform.xcconfig" 2 | 3 | // pick _static in libplatform.xcconfig 4 | BUILD_VARIANTS = static 5 | 6 | INSTALLHDRS_SCRIPT_PHASE = NO 7 | EXECUTABLE_PREFIX = lib 8 | PRODUCT_NAME = platform 9 | INSTALL_PATH = /usr/local/lib/system 10 | SKIP_INSTALL = NO 11 | -------------------------------------------------------------------------------- /src/force_libplatform_to_build.c: -------------------------------------------------------------------------------- 1 | // XCode will not build a library unless it contains at least one module. 2 | // Absent this requirement, libm.dylib would be composed entirely by linking 3 | // the component static libraries together, but to satisfy it, we must have 4 | // a C file. 5 | 6 | typedef int theCStandardDoesNotAllowAnEmptyModule; 7 | -------------------------------------------------------------------------------- /xcodeconfig/atomics.xcconfig: -------------------------------------------------------------------------------- 1 | #include "libplatform.xcconfig" 2 | #include "perarch.xcconfig" 3 | 4 | // Make sure that OSAtomic isn't build unoptimised, otherwise the inlines 5 | // don't do what they are designed to do. 6 | COMPILER_CFLAGS = -momit-leaf-frame-pointer 7 | OTHER_CFLAGS_debug = 8 | 9 | OSATOMIC_PREPROCESSOR_DEFINITIONS = OSATOMIC_USE_INLINED=0 OSATOMIC_DEPRECATED=0 10 | 11 | PUBLIC_HEADERS_FOLDER_PATH = $(SDK_INSTALL_HEADERS_ROOT)/usr/include/libkern 12 | PRIVATE_HEADERS_FOLDER_PATH = $(SDK_INSTALL_HEADERS_ROOT)/usr/local/include/libkern 13 | -------------------------------------------------------------------------------- /src/os/resolver/resolver.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __OS_RESOLVER_H__ 22 | #define __OS_RESOLVER_H__ 23 | 24 | #include "resolver_internal.h" 25 | 26 | 27 | #endif // __OS_RESOLVER_H__ 28 | -------------------------------------------------------------------------------- /src/os/resolver/resolver_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __OS_RESOLVER_INTERNAL_H__ 22 | #define __OS_RESOLVER_INTERNAL_H__ 23 | 24 | #include "os/internal.h" 25 | 26 | 27 | #endif // __OS_RESOLVER_INTERNAL_H__ 28 | -------------------------------------------------------------------------------- /xcodeconfig/libplatform-exclavekit.xcconfig: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 Apple Inc. All rights reserved. 3 | // 4 | // @APPLE_APACHE_LICENSE_HEADER_START@ 5 | // 6 | // Licensed under the Apache License, Version 2.0 (the "License"); 7 | // you may not use this file except in compliance with the License. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // 12 | // Unless required by applicable law or agreed to in writing, software 13 | // distributed under the License is distributed on an "AS IS" BASIS, 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | // 18 | // @APPLE_APACHE_LICENSE_HEADER_END@ 19 | // 20 | SDKROOT = exclavekit.iphoneos.internal 21 | PRODUCT_NAME = platform 22 | BUILD_VARIANTS = normal 23 | SKIP_INSTALL = NO 24 | EXECUTABLE_PREFIX = libsystem_ 25 | 26 | -------------------------------------------------------------------------------- /private/os/security_config_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __OS_SECURITY_CONFIG_PRIVATE__ 22 | #define __OS_SECURITY_CONFIG_PRIVATE__ 23 | 24 | #include 25 | 26 | #endif // __OS_SECURITY_CONFIG_PRIVATE__ 27 | -------------------------------------------------------------------------------- /src/os/apt_private.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "os/apt_private.h" 22 | #include "os/internal.h" 23 | 24 | // A stub implementation is needed for unsupported configs. 25 | #define NEED_STUB 1 26 | 27 | 28 | #ifdef NEED_STUB 29 | void os_apt_msg_async_task_running_4swift(__unused uint64_t task_id) {} 30 | void os_apt_msg_async_task_waiting_on_4swift(__unused uint64_t task_id) {} 31 | #endif // NEED_STUB 32 | -------------------------------------------------------------------------------- /src/atomics/OSAtomicFifo.h: -------------------------------------------------------------------------------- 1 | // 2 | // OSAtomicFifo.h 3 | // libatomics 4 | // 5 | // Created by Rokhini Prabhu on 4/7/20. 6 | // 7 | 8 | #ifndef _OS_ATOMIC_FIFO_QUEUE_ 9 | #define _OS_ATOMIC_FIFO_QUEUE_ 10 | 11 | #if defined(__arm64e__) && __has_feature(ptrauth_calls) 12 | #include 13 | 14 | #define COMMPAGE_PFZ_BASE_AUTH_KEY ptrauth_key_process_independent_code 15 | #define COMMPAGE_PFZ_FN_AUTH_KEY ptrauth_key_function_pointer 16 | #define COMMPAGE_PFZ_BASE_DISCRIMINATOR ptrauth_string_discriminator("pfz") 17 | 18 | #define COMMPAGE_PFZ_BASE_PTR __ptrauth(COMMPAGE_PFZ_BASE_AUTH_KEY, 1, COMMPAGE_PFZ_BASE_DISCRIMINATOR) 19 | 20 | #define SIGN_PFZ_FUNCTION_PTR(ptr) ptrauth_sign_unauthenticated(ptr, COMMPAGE_PFZ_FN_AUTH_KEY, 0) 21 | 22 | #else /* defined(__arm64e__) && __has_feature(ptrauth_calls) */ 23 | 24 | #define COMMPAGE_PFZ_BASE_AUTH_KEY 0 25 | #define COMMPAGE_PFZ_FN_AUTH_KEY 0 26 | #define COMMPAGE_PFZ_BASE_DISCRIMINATOR 0 27 | 28 | #define COMMPAGE_PFZ_BASE_PTR 29 | 30 | #define SIGN_PFZ_FUNCTION_PTR(ptr) ptr 31 | #endif /* defined(__arm64e__) && __has_feature(ptrauth_calls) */ 32 | 33 | extern void *COMMPAGE_PFZ_BASE_PTR commpage_pfz_base; 34 | 35 | #endif /* _OS_ATOMIC_FIFO_QUEUE_ */ 36 | -------------------------------------------------------------------------------- /src/timingsafe/x86_64/timingsafe.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | #include "timingsafe.h" 24 | 25 | timingsafe_token_t timingsafe_enable_if_supported(void) { return 0; } 26 | 27 | void timingsafe_restore_if_supported(timingsafe_token_t token) { (void)token; } 28 | -------------------------------------------------------------------------------- /xcodeconfig/perarch.xcconfig: -------------------------------------------------------------------------------- 1 | ARCH_FAMILY = $(ARCH_FAMILY_$(CURRENT_ARCH)) 2 | ARCH_FAMILY_x86_64 = x86_64 3 | ARCH_FAMILY_i386 = i386 4 | ARCH_FAMILY_armv6 = arm 5 | ARCH_FAMILY_armv7 = arm 6 | ARCH_FAMILY_armv7s = arm 7 | ARCH_FAMILY_armv7f = arm 8 | ARCH_FAMILY_armv7k = arm 9 | ARCH_FAMILY_arm64 = arm64 10 | ARCH_FAMILY_undefined_arch = undefined_arch 11 | 12 | EXCLUDED_SOURCE_FILE_NAMES = * 13 | 14 | INCLUDED_SOURCE_FILE_NAMES = force_libplatform_to_build.c $(INCLUDED_SOURCE_FILE_NAMES_$(PERARCH_TARGET)_TARGET) 15 | INCLUDED_SOURCE_FILE_NAMES__TARGET = $(INCLUDED_SOURCE_FILE_NAMES_$(ARCH_FAMILY_THIS_SLICE)_$(ARCH_FAMILY)) 16 | 17 | INCLUDED_SOURCE_FILE_NAMES_arm_arm = * 18 | INCLUDED_SOURCE_FILE_NAMES__arm = * 19 | INCLUDED_SOURCE_FILE_NAMES_arm64_arm64 = * 20 | INCLUDED_SOURCE_FILE_NAMES__arm64 = * 21 | INCLUDED_SOURCE_FILE_NAMES_i386_i386 = * 22 | INCLUDED_SOURCE_FILE_NAMES__i386 = * 23 | INCLUDED_SOURCE_FILE_NAMES_x86_64_x86_64 = * 24 | INCLUDED_SOURCE_FILE_NAMES__x86_64 = * 25 | INCLUDED_SOURCE_FILE_NAMES_undefined_arch_undefined_arch = * 26 | INCLUDED_SOURCE_FILE_NAMES__undefined_arch = * 27 | INCLUDED_SOURCE_FILE_NAMES_undefined_arch_ = * 28 | INCLUDED_SOURCE_FILE_NAMES__ = * 29 | 30 | // To force fallback to generic C implementations for dyld_Sim 31 | INCLUDED_SOURCE_FILE_NAMES_x86_64_x86_64[sdk=iphonesimulator*] = 32 | INCLUDED_SOURCE_FILE_NAMES_i386_i386[sdk=iphonesimulator*] = 33 | -------------------------------------------------------------------------------- /xcodeconfig/os.xcconfig: -------------------------------------------------------------------------------- 1 | GCC_STRICT_ALIASING = YES 2 | GCC_SYMBOLS_PRIVATE_EXTERN = YES 3 | GCC_WARN_SHADOW = YES 4 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES 5 | GCC_WARN_ABOUT_RETURN_TYPE = YES 6 | GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES 7 | GCC_WARN_ABOUT_MISSING_NEWLINE = YES 8 | GCC_WARN_UNUSED_VARIABLE = YES 9 | GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES 10 | GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = YES 11 | GCC_WARN_SIGN_COMPARE = YES 12 | GCC_WARN_UNINITIALIZED_AUTOS = YES 13 | CLANG_WARN_EMPTY_BODY = YES 14 | CLANG_WARN_IMPLICIT_SIGN_CONVERSION = YES 15 | CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES 16 | GCC_TREAT_WARNINGS_AS_ERRORS = YES 17 | WARNING_CFLAGS = -Wall -Wextra -Waggregate-return -Wfloat-equal -Wpacked -Wmissing-declarations -Wstrict-overflow=4 -Wstrict-aliasing=2 -Wno-unknown-warning-option -Wno-atomic-implicit-seq-cst -Wformat=2 18 | 19 | COMPILER_CFLAGS = -momit-leaf-frame-pointer 20 | OTHER_CFLAGS_debug = 21 | 22 | SRCROOT_SEARCH_PATHS = $(inherited) $(SRCROOT)/src/os/resolver 23 | OSATOMIC_PREPROCESSOR_DEFINITIONS = OSATOMIC_USE_INLINED=0 OSATOMIC_DEPRECATED=0 OSSPINLOCK_USE_INLINED=0 OSSPINLOCK_DEPRECATED=0 24 | 25 | PUBLIC_HEADERS_FOLDER_PATH = $(SDK_INSTALL_HEADERS_ROOT)/usr/include/os 26 | PRIVATE_HEADERS_FOLDER_PATH = $(SDK_INSTALL_HEADERS_ROOT)/usr/local/include/os 27 | OS_INTERNAL_HEADERS_FOLDER_PATH = $(SDK_INSTALL_HEADERS_ROOT)/usr/local/include/os/internal 28 | 29 | -------------------------------------------------------------------------------- /private/libkern/OSAtomicPrivate.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2004-2023 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #ifndef _OSATOMICPRIVATE_H_ 25 | #define _OSATOMICPRIVATE_H_ 26 | 27 | #include 28 | 29 | #if __has_include() && \ 30 | __has_include() && \ 31 | __has_include() 32 | 33 | 34 | #endif 35 | 36 | #endif // _OSATOMICPRIVATE_H_ 37 | -------------------------------------------------------------------------------- /src/ucontext/i386/_setcontext.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #if defined(__i386__) 25 | 26 | #include 27 | 28 | TEXT 29 | .private_extern __setcontext 30 | LABEL(__setcontext) 31 | movl 4(%esp), %ecx 32 | movl 28(%ecx), %ecx 33 | movl 16(%ecx), %ebx 34 | movl 28(%ecx), %edi 35 | movl 32(%ecx), %esi 36 | movl 36(%ecx), %ebp 37 | movl 40(%ecx), %esp 38 | pushl 48(%ecx) 39 | popfl 40 | movl 12(%ecx), %eax 41 | jmp *52(%ecx) 42 | 43 | #endif /* __i386__ */ 44 | -------------------------------------------------------------------------------- /src/setjmp/arm/_setjmp.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1999-2018 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | /* 25 | * Copyright (c) 1998-2008 Apple Inc. All rights reserved. 26 | * 27 | * Implements _setjmp() 28 | * 29 | */ 30 | 31 | #include 32 | #include "_setjmp.h" 33 | #include 34 | #include 35 | 36 | ENTRY_POINT(__setjmp) 37 | _OS_PTR_MUNGE_TOKEN(r12, r12) 38 | _OS_PTR_MUNGE(r1, r7, r12) // fp 39 | _OS_PTR_MUNGE(r2, lr, r12) 40 | _OS_PTR_MUNGE(r3, sp, r12) 41 | stmia r0!, { r1-r6, r8, r10-r11 } 42 | vstmia r0, { d8-d15 } 43 | mov r0, #0 44 | bx lr 45 | -------------------------------------------------------------------------------- /src/cachecontrol/arm/cache.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | .text 24 | .align 2 25 | 26 | #include 27 | 28 | /* void sys_icache_invalidate(addr_t start, int length) */ 29 | .globl _sys_icache_invalidate 30 | _sys_icache_invalidate: 31 | /* fast trap for icache_invalidate */ 32 | mov r3, #0 33 | mov r12, #0x80000000 34 | swi #SWI_SYSCALL 35 | bx lr 36 | 37 | /* void sys_dcache_flush(addr_t start, int length) */ 38 | .globl _sys_dcache_flush 39 | _sys_dcache_flush: 40 | /* fast trap for dcache_flush */ 41 | mov r3, #1 42 | mov r12, #0x80000000 43 | swi #SWI_SYSCALL 44 | bx lr 45 | -------------------------------------------------------------------------------- /src/cachecontrol/generic/cache.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2006 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | /* cache control */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | int 31 | sys_cache_control(int function, void *start, size_t len) 32 | { 33 | int status = 0; 34 | 35 | switch( function ) { 36 | 37 | case kCacheFunctionPrepareForExecution: 38 | sys_icache_invalidate(start, len); 39 | break; 40 | 41 | case kCacheFunctionFlushDcache: 42 | sys_dcache_flush(start, len); 43 | break; 44 | 45 | default: 46 | status = ENOTSUP; 47 | } 48 | 49 | return status; 50 | } 51 | -------------------------------------------------------------------------------- /src/setjmp/arm/_longjmp.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1999-2018 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | /* 25 | * Copyright (c) 1998-2008 Apple Inc. All rights reserved. 26 | * 27 | * Implements _longjmp() 28 | * 29 | */ 30 | 31 | #include 32 | #include "_setjmp.h" 33 | #include 34 | #include 35 | 36 | /* int _longjmp(jmp_buf env, int val); */ 37 | 38 | ENTRY_POINT(__longjmp) 39 | movs r12, r1 40 | ldmia r0!, { r1-r6, r8, r10-r11 } 41 | vldmia r0, { d8-d15 } 42 | movs r0, r12 43 | moveq r0, #1 44 | _OS_PTR_MUNGE_TOKEN(r12, r12) 45 | _OS_PTR_UNMUNGE(r7, r1, r12) // fp 46 | _OS_PTR_UNMUNGE(lr, r2, r12) 47 | _OS_PTR_UNMUNGE(sp, r3, r12) 48 | bx lr 49 | -------------------------------------------------------------------------------- /src/string/generic/strcpy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Apple, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | 26 | #if !_PLATFORM_OPTIMIZED_STRCPY 27 | 28 | char * 29 | _platform_strcpy(char * restrict dst, const char * restrict src) { 30 | const size_t length = _platform_strlen(src); 31 | // The stpcpy() and strcpy() functions copy the string src to dst 32 | // (including the terminating '\0' character). 33 | _platform_memmove(dst, src, length+1); 34 | // The strcpy() and strncpy() functions return dst. 35 | return dst; 36 | } 37 | 38 | #if VARIANT_STATIC 39 | char * 40 | strcpy(char * restrict dst, const char * restrict src) { 41 | return _platform_strcpy(dst, src); 42 | } 43 | #endif 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /src/ucontext/i386/getcontext.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #if defined(__i386__) 25 | 26 | #include 27 | 28 | TEXT 29 | LABEL(_getcontext) 30 | subl $28, %esp 31 | movl 32(%esp), %eax 32 | movl %eax, (%esp) 33 | movl %esp, 4(%esp) 34 | CALL_EXTERN(_getmcontext) 35 | movl %eax, %ecx 36 | addl $28, %esp 37 | movl %ebx, 16(%ecx) 38 | movl %edi, 28(%ecx) 39 | movl %esi, 32(%ecx) 40 | movl %ebp, 36(%ecx) 41 | movl (%esp), %eax 42 | movl %eax, 52(%ecx) 43 | movl %esp, %eax 44 | addl $4, %eax 45 | movl %eax, 40(%ecx) 46 | pushf 47 | popl %eax 48 | movl %eax, 48(%ecx) 49 | xorl %eax, %eax 50 | movl %eax, 12(%ecx) 51 | ret 52 | 53 | #endif /* __i386__ */ 54 | -------------------------------------------------------------------------------- /src/string/generic/strlcpy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Apple, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | 26 | #if !_PLATFORM_OPTIMIZED_STRLCPY 27 | 28 | size_t 29 | _platform_strlcpy(char * restrict dst, const char * restrict src, size_t maxlen) { 30 | const size_t srclen = _platform_strlen(src); 31 | if (srclen < maxlen) { 32 | _platform_memmove(dst, src, srclen+1); 33 | } else if (maxlen != 0) { 34 | _platform_memmove(dst, src, maxlen-1); 35 | dst[maxlen-1] = '\0'; 36 | } 37 | return srclen; 38 | } 39 | 40 | #if VARIANT_STATIC 41 | size_t 42 | strlcpy(char * restrict dst, const char * restrict src, size_t maxlen) { 43 | return _platform_strlcpy(dst, src, maxlen); 44 | } 45 | #endif 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /src/string/generic/memccpy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | 26 | #if !_PLATFORM_OPTIMIZED_MEMCCPY 27 | 28 | #include 29 | 30 | void * 31 | _platform_memccpy(void *t, const void *f, int c, size_t n) 32 | { 33 | void *last; 34 | 35 | if (n == 0) { 36 | return NULL; 37 | } 38 | 39 | last = _platform_memchr(f, c, n); 40 | 41 | if (last == NULL) { 42 | _platform_memmove(t, f, n); 43 | return NULL; 44 | } else { 45 | n = (char *)last - (char *)f + 1; 46 | _platform_memmove(t, f, n); 47 | return (void *)((char *)t + n); 48 | } 49 | } 50 | 51 | #if VARIANT_STATIC 52 | void * 53 | memccpy(void *t, const void *f, int c, size_t n) 54 | { 55 | return _platform_memccpy(t, f, c, n); 56 | } 57 | #endif 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /src/setjmp/arm/_setjmp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | /* 24 | * Copyright (c) 1998, Apple Computer Inc. All rights reserved. 25 | * 26 | * File: _setjmp.h 27 | * 28 | * Defines for register offsets in the save area. 29 | * 30 | */ 31 | 32 | #if defined(__arm__) 33 | 34 | #define JMP_r4 0x00 35 | #define JMP_r5 0x04 36 | #define JMP_r6 0x08 37 | #define JMP_r7 0x0c 38 | #define JMP_r8 0x10 39 | #define JMP_r10 0x14 40 | #define JMP_fp 0x18 41 | #define JMP_sp 0x1c 42 | #define JMP_lr 0x20 43 | 44 | #define JMP_VFP 0x24 45 | 46 | #define JMP_sigmask 0x68 47 | #define JMP_sigonstack 0x6C 48 | 49 | #define STACK_SSFLAGS 8 // offsetof(stack_t, ss_flags) 50 | 51 | 52 | #define JMP_SIGFLAG 0x70 53 | 54 | #else 55 | #error architecture not supported 56 | #endif 57 | -------------------------------------------------------------------------------- /man/cache.3: -------------------------------------------------------------------------------- 1 | .Dd September 21, 2006 2 | .Dt CACHE 3 3 | .Os Darwin 4 | .Sh NAME 5 | .Nm sys_cache_control , 6 | .Nm sys_icache_invalidate , 7 | .Nm sys_dcache_flush 8 | .Nd cache control 9 | .Sh LIBRARY 10 | .Lb libc 11 | .Sh SYNOPSIS 12 | .In libkern/OSCacheControl.h 13 | .Ft int 14 | .Fn sys_cache_control "int function" "void *start" "size_t len" 15 | .Ft void 16 | .Fn sys_icache_invalidate "void *start" "size_t len" 17 | .Ft void 18 | .Fn sys_dcache_flush "void *start" "size_t len" 19 | .Sh DESCRIPTION 20 | .Pp 21 | These functions operate on every cache line containing one of the 22 | .Fa len 23 | bytes of memory pointed to by 24 | .Fa start . 25 | Normally the operations apply to every 26 | processor in the system, but the exact semantics of these 27 | operations is platform dependent. They should be used with caution. 28 | .Pp 29 | .Fn sys_cache_control 30 | performs the operation specified by 31 | .Fa function . 32 | Refer to the header file for a list of currently supported functions. 33 | .Pp 34 | .Fn sys_icache_invalidate 35 | prepares memory for execution, typically by invalidating the instruction 36 | cache for the indicated range. This should be called 37 | after writing machine instructions to memory, and before 38 | executing them. On IA32 processors this function is a NOP, because 39 | their instruction caches are coherent. 40 | .Pp 41 | .Fn sys_dcache_flush 42 | writes modified data cache lines to main memory, 43 | and then invalidates all lines in the range being operated on. 44 | It can be useful when dealing with cache incoherent 45 | devices or DMA. 46 | .Sh RETURN VALUES 47 | .Fn sys_cache_control 48 | returns zero on success, ENOTSUP if 49 | .Fa function 50 | is not valid. 51 | .Sh SEE ALSO 52 | .Xr atomic 3 , 53 | .Xr barrier 3 54 | .Sh HISTORY 55 | These functions first appeared in Mac OS 10.5 (Leopard). -------------------------------------------------------------------------------- /src/string/generic/strlcat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Apple, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | 26 | #if !_PLATFORM_OPTIMIZED_STRLCAT 27 | 28 | size_t 29 | _platform_strlcat(char * restrict dst, const char * restrict src, size_t maxlen) { 30 | const size_t srclen = _platform_strlen(src); 31 | const size_t dstlen = _platform_strnlen(dst, maxlen); 32 | if (dstlen == maxlen) return maxlen+srclen; 33 | if (srclen < maxlen-dstlen) { 34 | _platform_memmove(dst+dstlen, src, srclen+1); 35 | } else { 36 | _platform_memmove(dst+dstlen, src, maxlen-dstlen-1); 37 | dst[maxlen-1] = '\0'; 38 | } 39 | return dstlen + srclen; 40 | } 41 | 42 | #if VARIANT_STATIC 43 | size_t 44 | strlcat(char * restrict dst, const char * restrict src, size_t maxlen) { 45 | return _platform_strlcat(dst, src, maxlen); 46 | } 47 | #endif 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /include/libkern/OSAtomic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2004-2016 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #ifndef _OSATOMIC_H_ 25 | #define _OSATOMIC_H_ 26 | 27 | /*! @header 28 | * These are deprecated legacy interfaces for atomic and synchronization 29 | * operations. 30 | * 31 | * Define OSATOMIC_USE_INLINED=1 to get inline implementations of the 32 | * OSAtomic interfaces in terms of the primitives. 33 | * 34 | * Define OSSPINLOCK_USE_INLINED=1 to get inline implementations of the 35 | * OSSpinLock interfaces in terms of the primitives. 36 | * 37 | * These are intended as a transition convenience, direct use of those 38 | * primitives should be preferred. 39 | */ 40 | 41 | #include 42 | 43 | #if __has_include() 44 | #include 45 | #endif 46 | 47 | #include "OSAtomicDeprecated.h" 48 | #include "OSSpinLockDeprecated.h" 49 | #include "OSAtomicQueue.h" 50 | 51 | #endif /* _OSATOMIC_H_ */ 52 | -------------------------------------------------------------------------------- /man/atomic.3: -------------------------------------------------------------------------------- 1 | .Dd May 26, 2004 2 | .Dt ATOMIC 3 3 | .Os Darwin 4 | .Sh NAME 5 | .Nm OSAtomicEnqueue , 6 | .Nm OSAtomicDequeue 7 | .Nd atomic lockless queues 8 | .Sh SYNOPSIS 9 | .In libkern/OSAtomic.h 10 | .Ft void 11 | .Fn OSAtomicEnqueue "OSQueueHead *list" "void *new" "size_t offset" 12 | .Ft void* 13 | .Fn OSAtomicDequeue "OSQueueHead *list" "size_t offset" 14 | .Sh DESCRIPTION 15 | The routines 16 | .Fn OSAtomicEnqueue 17 | and 18 | .Fn OSAtomicDequeue 19 | operate on singly linked LIFO queues. Ie, a dequeue operation will return the 20 | most recently enqueued element, or NULL if the list is empty. The operations 21 | are lockless, and barriers are used as necessary to permit thread-safe access to 22 | the queue element. 23 | .Fa offset 24 | is the offset in bytes to the link field in the queue element. 25 | .Pp 26 | .Bf -symbolic 27 | Important: the memory backing the link field of a queue element must not be 28 | unmapped after 29 | .Fn OSAtomicDequeue 30 | returns until all concurrent calls to 31 | .Fn OSAtomicDequeue 32 | for the same list on other threads have also returned, as they may still be 33 | accessing that memory location. 34 | .Ef 35 | .Sh EXAMPLES 36 | .Bd -literal -offset indent 37 | typedef struct elem { 38 | long data1; 39 | struct elem *link; 40 | int data2; 41 | } elem_t; 42 | 43 | elem_t fred, mary, *p; 44 | 45 | OSQueueHead q = OS_ATOMIC_QUEUE_INIT; 46 | 47 | OSAtomicEnqueue( &q, &fred, offsetof(elem_t,link) ); 48 | OSAtomicEnqueue( &q, &mary, offsetof(elem_t,link) ); 49 | 50 | p = OSAtomicDequeue( &q, offsetof(elem_t,link) ); 51 | 52 | .Ed 53 | In this example, the call of 54 | .Fn OSAtomicDequeue 55 | will return a ptr to mary. 56 | .Sh RETURN VALUES 57 | The dequeue operation returns the most recently enqueued element, or NULL if the list in empty. 58 | .Sh SEE ALSO 59 | .Xr stdatomic 3 , 60 | .Xr atomic_deprecated 3 , 61 | .Xr spinlock_deprecated 3 62 | .Sh HISTORY 63 | These functions first appeared in Mac OS 10.5 (Leopard). 64 | -------------------------------------------------------------------------------- /src/cachecontrol/x86_64/cache.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 Apple Inc. All rights reserved. 3 | * Copyright (c) 2003-2006 Apple Computer, Inc. All rights reserved. 4 | * 5 | * @APPLE_LICENSE_HEADER_START@ 6 | * 7 | * This file contains Original Code and/or Modifications of Original Code 8 | * as defined in and that are subject to the Apple Public Source License 9 | * Version 2.0 (the 'License'). You may not use this file except in 10 | * compliance with the License. Please obtain a copy of the License at 11 | * http://www.opensource.apple.com/apsl/ and read it before using this 12 | * file. 13 | * 14 | * The Original Code and all software distributed under the License are 15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 19 | * Please see the License for the specific language governing rights and 20 | * limitations under the License. 21 | * 22 | * @APPLE_LICENSE_HEADER_END@ 23 | */ 24 | 25 | 26 | #include 27 | 28 | 29 | .text 30 | .align 4, 0x00 31 | 32 | /* void sys_icache_invalidate(addr_t start, int length) */ 33 | 34 | .globl _sys_icache_invalidate 35 | _sys_icache_invalidate: 36 | // This is a NOP on intel processors, since the intent of the API 37 | // is to make data executable, and Intel L1Is are coherent with L1D. 38 | ret 39 | 40 | 41 | /* void sys_dcache_flush(addr_t start, int length) */ 42 | 43 | .globl _sys_dcache_flush 44 | _sys_dcache_flush: 45 | testq %rsi,%rsi // length 0? 46 | jz 2f // yes 47 | mfence // ensure previous stores make it to memory 48 | clflush -1(%rdi,%rsi) // make sure last line is flushed 49 | 1: 50 | clflush (%rdi) // flush a line 51 | addq $64,%rdi 52 | subq $64,%rsi 53 | ja 1b 54 | mfence // make sure memory is updated before we return 55 | 2: 56 | ret 57 | -------------------------------------------------------------------------------- /include/ucontext.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002, 2008, 2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | /* 25 | * These routines are DEPRECATED and should not be used. 26 | */ 27 | #ifndef _UCONTEXT_H_ 28 | #define _UCONTEXT_H_ 29 | 30 | #include 31 | 32 | #ifdef _XOPEN_SOURCE 33 | #include 34 | #include 35 | 36 | __BEGIN_DECLS 37 | __API_DEPRECATED("No longer supported", macos(10.5, 10.6)) 38 | int getcontext(ucontext_t *); 39 | 40 | __API_DEPRECATED("No longer supported", macos(10.5, 10.6)) 41 | void makecontext(ucontext_t *, void (*)(), int, ...); 42 | 43 | __API_DEPRECATED("No longer supported", macos(10.5, 10.6)) 44 | int setcontext(const ucontext_t *); 45 | 46 | __API_DEPRECATED("No longer supported", macos(10.5, 10.6)) 47 | int swapcontext(ucontext_t * __restrict, const ucontext_t * __restrict); 48 | 49 | __END_DECLS 50 | #else /* !_XOPEN_SOURCE */ 51 | #error The deprecated ucontext routines require _XOPEN_SOURCE to be defined 52 | #endif /* _XOPEN_SOURCE */ 53 | 54 | #endif /* _UCONTEXT_H_ */ 55 | -------------------------------------------------------------------------------- /man/spinlock_deprecated.3: -------------------------------------------------------------------------------- 1 | .Dd May 26, 2004 2 | .Dt SPINLOCK_DEPRECATED 3 3 | .Os Darwin 4 | .Sh NAME 5 | .Nm OSSpinLockTry , 6 | .Nm OSSpinLockLock , 7 | .Nm OSSpinLockUnlock 8 | .Nd deprecated atomic spin lock synchronization primitives 9 | .Sh SYNOPSIS 10 | .In libkern/OSAtomic.h 11 | .Ft bool 12 | .Fn OSSpinLockTry "OSSpinLock *lock" 13 | .Ft void 14 | .Fn OSSpinLockLock "OSSpinLock *lock" 15 | .Ft void 16 | .Fn OSSpinLockUnlock "OSSpinLock *lock" 17 | .Sh DESCRIPTION 18 | .Bf -symbolic 19 | These are deprecated interfaces for userspace spinlocks, provided for 20 | compatibility with legacy code. These interfaces should no longer be used, 21 | particularily in situations where threads of differing priorities may contend 22 | on the same spinlock. 23 | .Pp 24 | The interfaces in 25 | .In os/lock.h 26 | should be used instead in cases where a very low-level lock primitive is 27 | required. In general however, using higher level synchronization primitives 28 | such as those provided by the pthread or dispatch subsystems are preferred. 29 | .Ef 30 | .Pp 31 | The OSSpinLock operations use memory barriers to synchronize access to shared 32 | memory protected by the lock. Preemption is possible while the lock is held. 33 | .Pp 34 | .Ft OSSpinLock 35 | is an integer type. The convention is that unlocked is zero, and locked is nonzero. 36 | Locks must be naturally aligned and cannot be in cache-inhibited memory. 37 | .Pp 38 | .Fn OSSpinLockLock 39 | will spin if the lock is already held, but employs various strategies to back 40 | off. Because it can spin, it will generally be less cpu and power efficient than 41 | other synchronization primitives. 42 | .Pp 43 | .Fn OSSpinLockTry 44 | immediately returns false if the lock was held, true if it took the lock. 45 | It does not spin. 46 | .Pp 47 | .Fn OSSpinLockUnlock 48 | unconditionally unlocks the lock by zeroing it. 49 | .Sh RETURN VALUES 50 | .Fn OSSpinLockTry 51 | returns true if it took the lock, false if the lock was already held. 52 | .Sh SEE ALSO 53 | .Xr atomic 3 , 54 | .Xr atomic_deprecated 3 55 | -------------------------------------------------------------------------------- /src/string/generic/strnlen.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2009 David Schultz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | __FBSDID("$FreeBSD: src/lib/libc/string/strnlen.c,v 1.1 2009/02/28 06:00:58 das Exp $"); 29 | 30 | #include 31 | 32 | #if !_PLATFORM_OPTIMIZED_STRNLEN 33 | 34 | size_t 35 | _platform_strnlen(const char *s, size_t maxlen) 36 | { 37 | size_t len; 38 | 39 | for (len = 0; len < maxlen; len++, s++) { 40 | if (!*s) 41 | break; 42 | } 43 | return (len); 44 | } 45 | 46 | #if VARIANT_STATIC 47 | size_t 48 | strnlen(const char *s, size_t maxlen) 49 | { 50 | return _platform_strnlen(s, maxlen); 51 | } 52 | #endif 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /private/os/log_simple_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #ifndef _OS_LOG_SIMPLE_PRIVATE_H_ 25 | #define _OS_LOG_SIMPLE_PRIVATE_H_ 26 | 27 | #include 28 | /** 29 | * These constants have the same value as os_log_type from os/log.h 30 | */ 31 | #define OS_LOG_SIMPLE_TYPE_DEFAULT 0x00 32 | #define OS_LOG_SIMPLE_TYPE_INFO 0x01 33 | #define OS_LOG_SIMPLE_TYPE_DEBUG 0x02 34 | #define OS_LOG_SIMPLE_TYPE_ERROR 0x10 35 | 36 | #define os_log_simple(fmt, ...)\ 37 | os_log_simple_with_type(OS_LOG_SIMPLE_TYPE_DEFAULT, (fmt), ##__VA_ARGS__) 38 | 39 | #define os_log_simple_error(fmt, ...)\ 40 | os_log_simple_with_type(OS_LOG_SIMPLE_TYPE_ERROR, (fmt), ##__VA_ARGS__) 41 | 42 | #define os_log_simple_with_type(type, fmt, ...)\ 43 | os_log_simple_with_subsystem((type), NULL, (fmt), ##__VA_ARGS__) 44 | 45 | #define os_log_simple_with_subsystem(type, subsystem, fmt, ...)\ 46 | __os_log_simple_impl((type), (subsystem), (fmt), ##__VA_ARGS__) 47 | 48 | #define os_log_simple_available() (&_os_log_simple != 0) 49 | 50 | #endif /* _OS_LOG_SIMPLE_PRIVATE_H_ */ 51 | -------------------------------------------------------------------------------- /src/cachecontrol/i386/cache.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 Apple Inc. All rights reserved. 3 | * Copyright (c) 2003-2006 Apple Computer, Inc. All rights reserved. 4 | * 5 | * @APPLE_LICENSE_HEADER_START@ 6 | * 7 | * This file contains Original Code and/or Modifications of Original Code 8 | * as defined in and that are subject to the Apple Public Source License 9 | * Version 2.0 (the 'License'). You may not use this file except in 10 | * compliance with the License. Please obtain a copy of the License at 11 | * http://www.opensource.apple.com/apsl/ and read it before using this 12 | * file. 13 | * 14 | * The Original Code and all software distributed under the License are 15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 19 | * Please see the License for the specific language governing rights and 20 | * limitations under the License. 21 | * 22 | * @APPLE_LICENSE_HEADER_END@ 23 | */ 24 | 25 | 26 | #include 27 | 28 | 29 | .text 30 | .align 4, 0x00 31 | 32 | /* void sys_icache_invalidate(addr_t start, int length) */ 33 | 34 | .globl _sys_icache_invalidate 35 | _sys_icache_invalidate: 36 | // This is a NOP on intel processors, since the intent of the API 37 | // is to make data executable, and Intel L1Is are coherent with L1D. 38 | ret 39 | 40 | 41 | /* void sys_dcache_flush(addr_t start, int length) */ 42 | 43 | .globl _sys_dcache_flush 44 | _sys_dcache_flush: 45 | movl 8(%esp),%ecx // get length 46 | movl 4(%esp),%edx // get ptr 47 | testl %ecx,%ecx // length 0? 48 | jz 2f // yes 49 | mfence // ensure previous stores make it to memory 50 | clflush -1(%edx,%ecx) // make sure last line is flushed 51 | 1: 52 | clflush (%edx) // flush a line 53 | addl $64,%edx 54 | subl $64,%ecx 55 | ja 1b 56 | mfence // make sure memory is updated before we return 57 | 2: 58 | ret 59 | -------------------------------------------------------------------------------- /src/string/generic/strncpy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Apple, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | 26 | #if !_PLATFORM_OPTIMIZED_STRNCPY 27 | 28 | char * 29 | _platform_strncpy(char * restrict dst, const char * restrict src, size_t maxlen) { 30 | const size_t srclen = _platform_strnlen(src, maxlen); 31 | if (srclen < maxlen) { 32 | // The stpncpy() and strncpy() functions copy at most maxlen 33 | // characters from src into dst. 34 | _platform_memmove(dst, src, srclen); 35 | // If src is less than maxlen characters long, the remainder 36 | // of dst is filled with '\0' characters. 37 | _platform_memset(dst+srclen, 0, maxlen-srclen); 38 | } else { 39 | // Otherwise, dst is not terminated. 40 | _platform_memmove(dst, src, maxlen); 41 | } 42 | // The strcpy() and strncpy() functions return dst. 43 | return dst; 44 | } 45 | 46 | #if VARIANT_STATIC 47 | char * 48 | strncpy(char * restrict dst, const char * restrict src, size_t maxlen) { 49 | return _platform_strncpy(dst, src, maxlen); 50 | } 51 | #endif 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /private/os/once_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __OS_ONCE_PRIVATE__ 22 | #define __OS_ONCE_PRIVATE__ 23 | 24 | #include 25 | #include 26 | 27 | OS_ASSUME_NONNULL_BEGIN 28 | 29 | __BEGIN_DECLS 30 | 31 | #define OS_ONCE_SPI_VERSION 20130313 32 | 33 | OS_SWIFT_UNAVAILABLE("Swift has lazy init") 34 | typedef long os_once_t; 35 | 36 | __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_7_0) 37 | OS_EXPORT OS_NONNULL1 OS_NONNULL3 OS_NOTHROW 38 | OS_SWIFT_UNAVAILABLE("Swift has lazy init") 39 | void 40 | _os_once(os_once_t *predicate, void *_Nullable context, os_function_t function); 41 | 42 | OS_NONNULL1 OS_NONNULL3 OS_NOTHROW 43 | __header_always_inline void 44 | os_once(os_once_t *predicate, void *_Nullable context, os_function_t function) 45 | { 46 | if (OS_EXPECT(*predicate, ~0l) != ~0l) { 47 | _os_once(predicate, context, function); 48 | OS_COMPILER_CAN_ASSUME(*predicate == ~0l); 49 | } else { 50 | os_compiler_barrier(); 51 | } 52 | } 53 | 54 | /* This SPI is *strictly* for the use of pthread_once only. This is not 55 | * safe in general use of os_once. 56 | */ 57 | __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0) 58 | OS_EXPORT OS_NONNULL1 OS_NOTHROW 59 | OS_SWIFT_UNAVAILABLE("Swift has lazy init") 60 | void 61 | __os_once_reset(os_once_t *val); 62 | 63 | __END_DECLS 64 | 65 | OS_ASSUME_NONNULL_END 66 | 67 | #endif // __OS_ONCE_PRIVATE__ 68 | -------------------------------------------------------------------------------- /src/simple/getenv.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | 26 | #include 27 | 28 | #include <_simple.h> 29 | 30 | #include 31 | #include 32 | 33 | // This file is built with -fno-builtin to prevent the compiler from turning 34 | // _simple_memcmp into a memcmp() call. 35 | 36 | static int 37 | _simple_memcmp(const void *s1, const void *s2, size_t n) 38 | { 39 | if (n != 0) { 40 | const unsigned char *p1 = s1, *p2 = s2; 41 | 42 | do { 43 | if (*p1++ != *p2++) 44 | return (*--p1 - *--p2); 45 | } while (--n != 0); 46 | } 47 | return (0); 48 | } 49 | 50 | const char * 51 | _simple_getenv(const char *envp[], const char *var) { 52 | const char **p; 53 | size_t var_len; 54 | 55 | var_len = strlen(var); 56 | 57 | for (p = envp; p && *p; p++) { 58 | size_t p_len = strlen(*p); 59 | 60 | if (p_len >= var_len && 61 | _simple_memcmp(*p, var, var_len) == 0 && 62 | (*p)[var_len] == '=') { 63 | return &(*p)[var_len + 1]; 64 | } 65 | } 66 | 67 | return NULL; 68 | } 69 | -------------------------------------------------------------------------------- /man/manpages.lst: -------------------------------------------------------------------------------- 1 | # manpage tables 2 | # [ ...] 3 | 4 | # man3 5 | atomic.3 atomic.3 OSAtomicDequeue.3 OSAtomicEnqueue.3 6 | atomic_deprecated.3 atomic_deprecated.3 OSAtomicAdd32.3 OSAtomicAdd32Barrier.3 OSAtomicAdd64.3 OSAtomicAdd64Barrier.3 OSAtomicAnd32.3 OSAtomicAnd32Barrier.3 OSAtomicAnd32Orig.3 OSAtomicAnd32OrigBarrier.3 OSAtomicCompareAndSwap32.3 OSAtomicCompareAndSwap32Barrier.3 OSAtomicCompareAndSwap64.3 OSAtomicCompareAndSwap64Barrier.3 OSAtomicCompareAndSwapInt.3 OSAtomicCompareAndSwapIntBarrier.3 OSAtomicCompareAndSwapLong.3 OSAtomicCompareAndSwapLongBarrier.3 OSAtomicCompareAndSwapPtr.3 OSAtomicCompareAndSwapPtrBarrier.3 OSAtomicDecrement32.3 OSAtomicDecrement32Barrier.3 OSAtomicDecrement64.3 OSAtomicDecrement64Barrier.3 OSAtomicIncrement32.3 OSAtomicIncrement32Barrier.3 OSAtomicIncrement64.3 OSAtomicIncrement64Barrier.3 OSAtomicOr32.3 OSAtomicOr32Barrier.3 OSAtomicOr32Orig.3 OSAtomicOr32OrigBarrier.3 OSAtomicTestAndClear.3 OSAtomicTestAndClearBarrier.3 OSAtomicTestAndSet.3 OSAtomicTestAndSetBarrier.3 OSAtomicXor32.3 OSAtomicXor32Barrier.3 OSAtomicXor32Orig.3 OSAtomicXor32OrigBarrier.3 OSMemoryBarrier.3 7 | cache.3 cache.3 sys_cache_control.3 sys_icache_invalidate.3 sys_dcache_flush.3 8 | ffs.3 ffs.3 ffsl.3 ffsll.3 fls.3 flsl.3 flsll.3 9 | getcontext.3 getcontext.3 setcontext.3 10 | makecontext.3 makecontext.3 swapcontext.3 11 | setjmp.3 setjmp.3 _longjmp.3 _setjmp.3 longjmp.3 longjmperr.3 longjmperror.3 siglongjmp.3 sigsetjmp.3 12 | spinlock_deprecated.3 spinlock_deprecated.3 OSSpinLockLock.3 OSSpinLockTry.3 OSSpinLockUnlock.3 13 | ucontext.3 ucontext.3 14 | stdatomic.3 stdatomic.3 ATOMIC_VAR_INIT.3 atomic_compare_exchange_strong.3 atomic_compare_exchange_strong_explicit.3 atomic_compare_exchange_weak.3 atomic_compare_exchange_weak_explicit.3 atomic_exchange.3 atomic_exchange_explicit.3 atomic_fetch_add.3 atomic_fetch_add_explicit.3 atomic_fetch_and.3 atomic_fetch_and_explicit.3 atomic_fetch_or.3 atomic_fetch_or_explicit.3 atomic_fetch_sub.3 atomic_fetch_sub_explicit.3 atomic_fetch_xor.3 atomic_fetch_xor_explicit.3 atomic_init.3 atomic_is_lock_free.3 atomic_load.3 atomic_load_explicit.3 atomic_store.3 atomic_store_explicit.3 15 | 16 | -------------------------------------------------------------------------------- /private/platform/compat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #ifndef _PLATFORM_COMPAT_H_ 25 | #define _PLATFORM_COMPAT_H_ 26 | 27 | #include 28 | 29 | /* Compat macros for primitives */ 30 | #define bzero _platform_bzero 31 | #define memchr _platform_memchr 32 | #define memcmp _platform_memcmp 33 | #define memmove _platform_memmove 34 | #define memccpy _platform_memccpy 35 | #define memset _platform_memset 36 | #define memset_pattern4 _platform_memset_pattern4 37 | #define memset_pattern8 _platform_memset_pattern8 38 | #define memset_pattern16 _platform_memset_pattern16 39 | #define strchr _platform_strchr 40 | #define strcmp _platform_strcmp 41 | #define strcpy _platform_strcpy 42 | #define strlcat _platform_strlcat 43 | #define strlcpy _platform_strlcpy 44 | #define strlen _platform_strlen 45 | #define strncmp _platform_strncmp 46 | #define strncpy _platform_strncpy 47 | #define strnlen _platform_strnlen 48 | #define strstr _platform_strstr 49 | 50 | #endif /* _PLATFORM_COMPAT_H_ */ 51 | -------------------------------------------------------------------------------- /src/string/generic/strchr.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1990, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 4. Neither the name of the University nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software 15 | * without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #include 31 | 32 | #if !_PLATFORM_OPTIMIZED_STRCHR 33 | 34 | #include 35 | 36 | char * 37 | _platform_strchr(const char *p, int ch) 38 | { 39 | char c; 40 | 41 | c = ch; 42 | for (;; ++p) { 43 | if (*p == c) 44 | return ((char *)p); 45 | if (*p == '\0') 46 | return (NULL); 47 | } 48 | /* NOTREACHED */ 49 | } 50 | 51 | #if VARIANT_STATIC 52 | char * 53 | strchr(const char *p, int ch) 54 | { 55 | return _platform_strchr(p, ch); 56 | } 57 | #endif 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /src/string/generic/bzero.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | 26 | #if !VARIANT_STATIC 27 | // to satisfy compiler-generated memset inside libplatform (e.g. makecontext) 28 | __attribute__((visibility("hidden"))) 29 | void * 30 | memset(void *b, int c, size_t len) 31 | { 32 | return _platform_memset(b, c, len); 33 | } 34 | #endif 35 | 36 | #if !_PLATFORM_OPTIMIZED_MEMSET 37 | 38 | void * 39 | _platform_memset(void *b, int c, size_t len) { 40 | unsigned char pattern[4]; 41 | 42 | pattern[0] = (unsigned char)c; 43 | pattern[1] = (unsigned char)c; 44 | pattern[2] = (unsigned char)c; 45 | pattern[3] = (unsigned char)c; 46 | 47 | _platform_memset_pattern4(b, pattern, len); 48 | return b; 49 | } 50 | 51 | #if VARIANT_STATIC 52 | void * 53 | memset(void *b, int c, size_t len) { 54 | return _platform_memset(b, c, len); 55 | } 56 | #endif 57 | 58 | #endif 59 | 60 | 61 | #if !_PLATFORM_OPTIMIZED_BZERO 62 | 63 | void 64 | _platform_bzero(void *s, size_t n) 65 | { 66 | _platform_memset(s, 0, n); 67 | } 68 | 69 | #if VARIANT_STATIC 70 | void 71 | bzero(void *s, size_t n) { 72 | _platform_bzero(s, n); 73 | } 74 | 75 | void 76 | __bzero(void *s, size_t n) { 77 | _platform_bzero(s, n); 78 | } 79 | #endif 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /src/string/generic/strcmp.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1990, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * This code is derived from software contributed to Berkeley by 6 | * Chris Torek. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 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 the 15 | * documentation and/or other materials provided with the distribution. 16 | * 4. Neither the name of the University nor the names of its contributors 17 | * may be used to endorse or promote products derived from this software 18 | * without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 | * SUCH DAMAGE. 31 | */ 32 | 33 | #include 34 | 35 | #if !_PLATFORM_OPTIMIZED_STRCMP 36 | 37 | int 38 | _platform_strcmp(const char *s1, const char *s2) 39 | { 40 | while (*s1 == *s2++) 41 | if (*s1++ == '\0') 42 | return (0); 43 | return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1)); 44 | } 45 | 46 | #if VARIANT_STATIC 47 | int 48 | strcmp(const char *s1, const char *s2) 49 | { 50 | return _platform_strcmp(s1, s2); 51 | } 52 | #endif 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /src/os/lock_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __OS_LOCK_INTERNAL__ 22 | #define __OS_LOCK_INTERNAL__ 23 | 24 | #define OS_LOCK_S_INTERNAL(type) _os_lock_##type##_s 25 | #define OS_LOCK_T_INTERNAL(type) _os_lock_##type##_t 26 | #define OS_LOCK_STRUCT_INTERNAL(type) struct OS_LOCK_S_INTERNAL(type) 27 | #define OS_LOCK_T_MEMBER(type) \ 28 | OS_LOCK_STRUCT(type) *_osl_opaque_##type; \ 29 | OS_LOCK_STRUCT_INTERNAL(type) *_osl_##type 30 | 31 | #define OS_LOCK_STRUCT_DECL_INTERNAL(type, ...) \ 32 | typedef struct OS_LOCK_S_INTERNAL(type) { \ 33 | OS_LOCK_TYPE_STRUCT(type) * osl_type; \ 34 | __VA_ARGS__ \ 35 | } OS_LOCK_S_INTERNAL(type); \ 36 | typedef OS_LOCK_STRUCT_INTERNAL(type) *OS_LOCK_T_INTERNAL(type) 37 | 38 | #define OS_LOCK_METHODS_DECL(type) \ 39 | void _os_lock_##type##_lock(OS_LOCK_T_INTERNAL(type)); \ 40 | bool _os_lock_##type##_trylock(OS_LOCK_T_INTERNAL(type)); \ 41 | void _os_lock_##type##_unlock(OS_LOCK_T_INTERNAL(type)) 42 | 43 | #define OS_LOCK_TYPE_STRUCT_DECL(type) \ 44 | OS_LOCK_TYPE_STRUCT(type) { \ 45 | const char *osl_kind; \ 46 | void (*osl_lock)(os_lock_t); \ 47 | bool (*osl_trylock)(os_lock_t); \ 48 | void (*osl_unlock)(os_lock_t); \ 49 | } OS_LOCK_TYPE_REF(type) 50 | 51 | #define OS_LOCK_TYPE_INSTANCE(type) \ 52 | OS_LOCK_TYPE_STRUCT_DECL(type) = { \ 53 | .osl_kind = #type, \ 54 | .osl_lock = _os_lock_##type##_lock, \ 55 | .osl_trylock = _os_lock_##type##_trylock, \ 56 | .osl_unlock = _os_lock_##type##_unlock, \ 57 | } 58 | 59 | #endif // __OS_LOCK_INTERNAL__ 60 | -------------------------------------------------------------------------------- /src/string/generic/strncmp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1989, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 4. Neither the name of the University nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software 15 | * without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #include 31 | 32 | #if !_PLATFORM_OPTIMIZED_STRNCMP 33 | 34 | int 35 | _platform_strncmp(const char *s1, const char *s2, size_t n) 36 | { 37 | 38 | if (n == 0) 39 | return (0); 40 | do { 41 | if (*s1 != *s2++) 42 | return (*(const unsigned char *)s1 - 43 | *(const unsigned char *)(s2 - 1)); 44 | if (*s1++ == '\0') 45 | break; 46 | } while (--n != 0); 47 | return (0); 48 | } 49 | 50 | #if VARIANT_STATIC 51 | int 52 | strncmp(const char *s1, const char *s2, size_t n) 53 | { 54 | return _platform_strncmp(s1, s2, n); 55 | } 56 | #endif 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /src/string/generic/memchr.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1990, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * This code is derived from software contributed to Berkeley by 6 | * Chris Torek. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 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 the 15 | * documentation and/or other materials provided with the distribution. 16 | * 4. Neither the name of the University nor the names of its contributors 17 | * may be used to endorse or promote products derived from this software 18 | * without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 | * SUCH DAMAGE. 31 | */ 32 | 33 | #include 34 | 35 | #if !_PLATFORM_OPTIMIZED_MEMCHR 36 | 37 | #include 38 | 39 | void * 40 | _platform_memchr(const void *s, int c, size_t n) 41 | { 42 | if (n != 0) { 43 | const unsigned char *p = s; 44 | 45 | do { 46 | if (*p++ == (unsigned char)c) 47 | return ((void *)(p - 1)); 48 | } while (--n != 0); 49 | } 50 | return (NULL); 51 | } 52 | 53 | #if VARIANT_STATIC 54 | void * 55 | memchr(const void *s, int c, size_t n) 56 | { 57 | return _platform_memchr(s, c, n); 58 | } 59 | #endif 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /src/atomics/x86_64/OSAtomicFifo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define OS_UNFAIR_LOCK_INLINE 1 5 | #include "os/lock_private.h" 6 | 7 | typedef volatile struct { 8 | void *first; 9 | void *last; 10 | os_unfair_lock lock; 11 | } __attribute__ ((aligned (16))) UnfairFifoQueueHead; 12 | 13 | #define set_next(element, offset, new) \ 14 | *((void**)(((uintptr_t)element) + offset)) = new; 15 | #define get_next(element, offset) \ 16 | *((void**)(((uintptr_t)element) + offset)); 17 | 18 | // This is a naive implementation using unfair locks to support translated 19 | // x86_64 apps only. Native x86_64 and arm64 apps will use the 20 | // PFZ implementations 21 | void OSAtomicFifoEnqueue$VARIANT$UnfairLock(UnfairFifoQueueHead *list, void *new, size_t offset) { 22 | set_next(new, offset, NULL); 23 | 24 | os_unfair_lock_lock_inline((os_unfair_lock_t)&list->lock); 25 | if (list->last == NULL) { 26 | list->first = new; 27 | } else { 28 | set_next(list->last, offset, new); 29 | } 30 | list->last = new; 31 | os_unfair_lock_unlock_inline((os_unfair_lock_t)&list->lock); 32 | } 33 | 34 | void* OSAtomicFifoDequeue$VARIANT$UnfairLock(UnfairFifoQueueHead *list, size_t offset) { 35 | os_unfair_lock_lock_inline((os_unfair_lock_t)&list->lock); 36 | void *element = list->first; 37 | if (element != NULL) { 38 | void *next = get_next(element, offset); 39 | if (next == NULL) { 40 | list->last = NULL; 41 | } 42 | list->first = next; 43 | } 44 | os_unfair_lock_unlock_inline((os_unfair_lock_t)&list->lock); 45 | 46 | return element; 47 | } 48 | 49 | #define MakeResolver(name) \ 50 | void * name ## Resolver(void) __asm__("_" #name); \ 51 | void * name ## Resolver(void) { \ 52 | __asm__(".symbol_resolver _" #name); \ 53 | uint64_t capabilities = *(uint64_t*)_COMM_PAGE_CPU_CAPABILITIES64; \ 54 | if (capabilities & kIsTranslated) { \ 55 | return name ## $VARIANT$UnfairLock; \ 56 | } else { \ 57 | return name ## $VARIANT$PFZ; \ 58 | } \ 59 | } 60 | 61 | void OSAtomicFifoEnqueue$VARIANT$PFZ(OSFifoQueueHead *, void *, size_t); 62 | void* OSAtomicFifoDequeue$VARIANT$PFZ(OSFifoQueueHead *, size_t); 63 | 64 | MakeResolver(OSAtomicFifoEnqueue) 65 | MakeResolver(OSAtomicFifoDequeue) 66 | -------------------------------------------------------------------------------- /src/ucontext/arm64/_ctx_start.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include "asm_help.h" 25 | #include 26 | #include 27 | 28 | .text 29 | 30 | #if TARGET_OS_OSX || TARGET_OS_DRIVERKIT 31 | 32 | /* Helper macro for unmunging pointers in place */ 33 | .macro PTR_UNMUNGE addr 34 | #if defined(__LP64__) 35 | _OS_PTR_MUNGE_TOKEN(x16, x16) 36 | #else 37 | _OS_PTR_MUNGE_TOKEN(x16, w16) 38 | #endif 39 | _OS_PTR_UNMUNGE(\addr, \addr, x16) 40 | .endmacro 41 | 42 | .macro CALL_USER_FUNC func 43 | // Populate the first 8 arguments in registers from the stack. Coordinated 44 | // with makecontext which populates the arguments on the stack 45 | ldp w0, w1, [sp], #32 46 | ldp w2, w3, [sp, #-24] 47 | ldp w4, w5, [sp, #-16] 48 | ldp w6, w7, [sp, #-8] 49 | 50 | PTR_UNMUNGE \func 51 | 52 | #if defined(__arm64e__) 53 | blraaz \func 54 | #else 55 | blr \func 56 | #endif 57 | .endmacro 58 | 59 | .private_extern __ctx_start 60 | .align 2 61 | __ctx_start: 62 | /* x20 = munged signed user func, 63 | * x19 = uctx, 64 | * fp = top of stack, 65 | * sp = where args end */ 66 | CALL_USER_FUNC x20 67 | 68 | /* user function returned, set up stack for _ctx_done */ 69 | 70 | /* Reset to top of stack */ 71 | mov sp, fp 72 | 73 | mov x0, x19 /* x0 = uctx */ 74 | bl __ctx_done 75 | 76 | brk #666 /* Should not get here */ 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /src/atomics/x86_64/pfz.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2013 Apple, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | .text 29 | 30 | /* Subroutine to make a preempt syscall. Called when we notice %ebx is 31 | * nonzero after returning from a PFZ subroutine. Not in PFZ. 32 | * 33 | * All registers preserved (but does clear the %ebx preemption flag). 34 | */ 35 | .align 2 36 | .private_extern _preempt 37 | .globl _preempt 38 | _preempt: 39 | pushq %rax 40 | pushq %rcx 41 | pushq %r11 42 | movl $(SYSCALL_CONSTRUCT_MACH(58)),%eax /* 58 = pfz_exit */ 43 | xorl %ebx,%ebx 44 | syscall 45 | popq %r11 46 | popq %rcx 47 | popq %rax 48 | ret 49 | 50 | /* Subroutine to back off if we cannot get the spinlock. Called 51 | * after a few attempts inline in the PFZ subroutines. This code is 52 | * not in the PFZ. 53 | * %rdi = ptr to queue head structure 54 | * %ebx = preemption flag (nonzero if preemption pending) 55 | * Uses: %rax. 56 | */ 57 | .align 2 58 | .private_extern _backoff 59 | .globl _backoff 60 | _backoff: 61 | testl %ebx,%ebx // does kernel want to preempt us? 62 | jz 1f // no 63 | call _preempt 64 | 1: 65 | pause // SMT-friendly backoff 66 | cmpl $0,16(%rdi) // sniff the lockword 67 | jnz 1b // loop if still taken 68 | ret // lockword is free, so reenter PFZ 69 | -------------------------------------------------------------------------------- /src/os/semaphore.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "os/internal.h" 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #define OS_VERIFY_MIG(x, msg) do { \ 28 | if (unlikely((x) == MIG_REPLY_MISMATCH)) { \ 29 | __LIBPLATFORM_CLIENT_CRASH__(x, msg); \ 30 | } \ 31 | } while (0) 32 | 33 | #define OS_SEMAPHORE_VERIFY_KR(x, msg) do { \ 34 | if (unlikely(x)) { \ 35 | __LIBPLATFORM_CLIENT_CRASH__(x, msg); \ 36 | } \ 37 | } while (0) 38 | 39 | os_semaphore_t 40 | _os_semaphore_create(void) 41 | { 42 | semaphore_t s4; 43 | kern_return_t kr; 44 | kr = semaphore_create(mach_task_self(), &s4, SYNC_POLICY_FIFO, 0); 45 | OS_VERIFY_MIG(kr, "Allocating semaphore failed with MIG_REPLY_MISMATCH"); 46 | OS_SEMAPHORE_VERIFY_KR(kr, "Creating semaphore failed, possible port leak"); 47 | return (os_semaphore_t)s4; 48 | } 49 | 50 | void 51 | _os_semaphore_dispose(os_semaphore_t sema) 52 | { 53 | semaphore_t s4 = (semaphore_t)sema; 54 | kern_return_t kr = semaphore_destroy(mach_task_self(), s4); 55 | OS_SEMAPHORE_VERIFY_KR(kr, "Destroying semaphore failed"); 56 | } 57 | 58 | void 59 | _os_semaphore_signal(os_semaphore_t sema) 60 | { 61 | semaphore_t s4 = (semaphore_t)sema; 62 | kern_return_t kr = semaphore_signal(s4); 63 | OS_SEMAPHORE_VERIFY_KR(kr, "Signaling semaphore failed"); 64 | } 65 | 66 | void 67 | _os_semaphore_wait(os_semaphore_t sema) 68 | { 69 | semaphore_t s4 = (semaphore_t)sema; 70 | kern_return_t kr; 71 | do { 72 | kr = semaphore_wait(s4); 73 | } while (unlikely(kr == KERN_ABORTED)); 74 | OS_SEMAPHORE_VERIFY_KR(kr, "Waiting on semaphore failed"); 75 | } 76 | -------------------------------------------------------------------------------- /internal/os/yield.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libplatform 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __OS_YIELD__ 28 | #define __OS_YIELD__ 29 | 30 | #include 31 | 32 | #pragma mark - 33 | #pragma mark _os_wait_until 34 | 35 | #if OS_ATOMIC_UP 36 | #define _os_wait_until(c) do { \ 37 | int _spins = 0; \ 38 | while (unlikely(!(c))) { \ 39 | _spins++; \ 40 | _os_preemption_yield(_spins); \ 41 | } } while (0) 42 | #else 43 | // 44 | #ifndef OS_WAIT_SPINS 45 | #define OS_WAIT_SPINS 1024 46 | #endif 47 | #define _os_wait_until(c) do { \ 48 | int _spins = -(OS_WAIT_SPINS); \ 49 | while (unlikely(!(c))) { \ 50 | if (unlikely(_spins++ >= 0)) { \ 51 | _os_preemption_yield(_spins); \ 52 | } else { \ 53 | os_hardware_pause(); \ 54 | } \ 55 | } } while (0) 56 | #endif 57 | 58 | #pragma mark - 59 | #pragma mark os_hardware_pause 60 | 61 | #if defined(__x86_64__) || defined(__i386__) 62 | #define os_hardware_pause() __asm__("pause") 63 | #elif (defined(__arm__) && defined(_ARM_ARCH_7) && defined(__thumb__)) || \ 64 | defined(__arm64__) 65 | #define os_hardware_pause() __asm__("yield") 66 | #define os_hardware_wfe() __asm__("wfe") 67 | #else 68 | #define os_hardware_pause() __asm__("") 69 | #endif 70 | 71 | #pragma mark - 72 | #pragma mark _os_preemption_yield 73 | 74 | #define _os_preemption_yield(n) thread_switch(MACH_PORT_NULL, \ 75 | SWITCH_OPTION_OSLOCK_DEPRESS, (mach_msg_timeout_t)(n)) 76 | 77 | #endif // __OS_YIELD__ 78 | -------------------------------------------------------------------------------- /include/string_x86.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | #ifndef _STRING_X86_H 24 | #define _STRING_X86_H 25 | 26 | #include 27 | 28 | #if defined(__x86_64__) 29 | 30 | __BEGIN_DECLS 31 | /* These SSE variants have the same behavior as their original functions. 32 | * SSE instructions are used in these variants instead of best possible 33 | * implementation. 34 | */ 35 | __OSX_AVAILABLE(10.16) __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE 36 | void *memmove_sse_np(void *__dst, const void *__src, size_t __len); 37 | 38 | __OSX_AVAILABLE(10.16) __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE 39 | void *memset_sse_np(void *__b, int __c, size_t __len); 40 | 41 | __OSX_AVAILABLE(10.16) __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE 42 | void bzero_sse_np(void *, size_t); 43 | 44 | __OSX_AVAILABLE(10.16) __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE 45 | void memset_pattern4_sse_np(void *__b, const void *__pattern4, size_t __len); 46 | 47 | __OSX_AVAILABLE(10.16) __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE 48 | void memset_pattern8_sse_np(void *__b, const void *__pattern8, size_t __len); 49 | 50 | __OSX_AVAILABLE(10.16) __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE 51 | void memset_pattern16_sse_np(void *__b, const void *__pattern16, size_t __len); 52 | __END_DECLS 53 | 54 | #endif /* __x86_64__ */ 55 | 56 | #endif /* _STRING_X86_H */ 57 | -------------------------------------------------------------------------------- /src/setjmp/generic/sigaction.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1999-2017 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | /* 24 | * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved 25 | * 26 | * @(#)sigaction.c 1.0 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | // keep in sync with BSD_KERNEL_PRIVATE value in xnu/bsd/sys/signal.h 36 | #define SA_VALIDATE_SIGRETURN_FROM_SIGTRAMP 0x0400 37 | 38 | /* 39 | * Intercept the sigaction syscall and use our signal trampoline 40 | * as the signal handler instead. The code here is derived 41 | * from sigvec in sys/kern_sig.c. 42 | */ 43 | extern int __sigaction (int, struct __sigaction * __restrict, 44 | struct sigaction * __restrict); 45 | 46 | int 47 | __platform_sigaction (int sig, const struct sigaction * __restrict nsv, 48 | struct sigaction * __restrict osv) 49 | { 50 | extern void _sigtramp(); 51 | struct __sigaction sa; 52 | struct __sigaction *sap; 53 | int ret; 54 | 55 | if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) { 56 | errno = EINVAL; 57 | return (-1); 58 | } 59 | sap = (struct __sigaction *)0; 60 | if (nsv) { 61 | sa.sa_handler = nsv->sa_handler; 62 | sa.sa_tramp = _sigtramp; 63 | sa.sa_mask = nsv->sa_mask; 64 | sa.sa_flags = nsv->sa_flags | SA_VALIDATE_SIGRETURN_FROM_SIGTRAMP; 65 | sap = &sa; 66 | } 67 | ret = __sigaction(sig, sap, osv); 68 | return ret; 69 | } 70 | -------------------------------------------------------------------------------- /src/string/generic/memcmp.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1990, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * This code is derived from software contributed to Berkeley by 6 | * Chris Torek. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 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 the 15 | * documentation and/or other materials provided with the distribution. 16 | * 4. Neither the name of the University nor the names of its contributors 17 | * may be used to endorse or promote products derived from this software 18 | * without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 | * SUCH DAMAGE. 31 | */ 32 | 33 | #include 34 | 35 | #if !_PLATFORM_OPTIMIZED_MEMCMP 36 | 37 | int 38 | _platform_memcmp(const void *s1, const void *s2, size_t n) 39 | { 40 | if (n != 0) { 41 | const unsigned char *p1 = s1, *p2 = s2; 42 | 43 | do { 44 | if (*p1++ != *p2++) 45 | return (*--p1 - *--p2); 46 | } while (--n != 0); 47 | } 48 | return (0); 49 | } 50 | 51 | #if VARIANT_STATIC 52 | int 53 | memcmp(const void *s1, const void *s2, size_t n) 54 | { 55 | return _platform_memcmp(s1, s2, n); 56 | } 57 | 58 | int 59 | bcmp(const void *s1, const void *s2, size_t n) 60 | { 61 | return _platform_memcmp(s1, s2, n); 62 | } 63 | #endif 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /src/atomics/init.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include <_simple.h> 30 | 31 | #include 32 | #include 33 | 34 | #include "OSAtomicFifo.h" 35 | 36 | __attribute__ ((visibility ("hidden"))) 37 | void *COMMPAGE_PFZ_BASE_PTR commpage_pfz_base = 0; 38 | 39 | __attribute__ ((visibility ("hidden"))) 40 | void 41 | __pfz_setup(const char *apple[]) 42 | { 43 | const char *p = _simple_getenv(apple, "pfz"); 44 | uintptr_t base = 0; 45 | if (p != NULL) { 46 | const char *q; 47 | 48 | /* We are given hex starting with 0x */ 49 | if (p[0] != '0' || p[1] != 'x') { 50 | goto __pfz_setup_clear; 51 | } 52 | 53 | for (q = p + 2; *q; q++) { 54 | base <<= 4; // *= 16 55 | 56 | if ('0' <= *q && *q <= '9') { 57 | base += *q - '0'; 58 | } else if ('a' <= *q && *q <= 'f') { 59 | base += *q - 'a' + 10; 60 | } else if ('A' <= *q && *q <= 'F') { 61 | base += *q - 'A' + 10; 62 | } else { 63 | base=0; 64 | goto __pfz_setup_clear; 65 | } 66 | } 67 | 68 | __pfz_setup_clear: 69 | bzero((void *)((uintptr_t)p - 4), strlen(p) + 4); 70 | } 71 | 72 | if (base != 0) { 73 | commpage_pfz_base = base; 74 | } 75 | } 76 | 77 | -------------------------------------------------------------------------------- /include/timingsafe.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | #ifndef timingsafe_h 24 | #define timingsafe_h 25 | #include 26 | #include 27 | #include 28 | 29 | /** 30 | Token used to track state from enable() to disable(). 31 | */ 32 | typedef uint64_t timingsafe_token_t; 33 | 34 | __BEGIN_DECLS 35 | 36 | /** 37 | @function timingsafe_enable_if_supported 38 | @abstract Unconditionally enable all supported timingsafe features. 39 | If timingsafe features aren't supported, they are ignored. If no features are 40 | supported, this is a no-op. 41 | 42 | @return The opaque token to use in timingsafe_restore_if_supported(). 43 | */ 44 | __API_AVAILABLE(macos(15.2), ios(18.2), tvos(18.2), watchos(11.2), visionos(2.2)) 45 | OS_EXPORT OS_WARN_RESULT 46 | OS_SWIFT_UNAVAILABLE_FROM_ASYNC("Not supported for async.") 47 | timingsafe_token_t timingsafe_enable_if_supported(void); 48 | 49 | /** 50 | @function timingsafe_restore_if_supported 51 | @abstract Restore timingsafe features to the state they were in before calling 52 | timingsafe_enable_if_supported and given the provided token. 53 | 54 | @param token 55 | The token returned by timingsafe_enable_if_supported. 56 | */ 57 | __API_AVAILABLE(macos(15.2), ios(18.2), tvos(18.2), watchos(11.2), visionos(2.2)) 58 | OS_EXPORT 59 | OS_SWIFT_UNAVAILABLE_FROM_ASYNC("Not supported for async.") 60 | void timingsafe_restore_if_supported(timingsafe_token_t token); 61 | 62 | __END_DECLS 63 | 64 | #endif /* timingsafe_h */ 65 | -------------------------------------------------------------------------------- /src/setjmp/arm/longjmp.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | /* 24 | * Copyright (c) 1998-2008 Apple Inc. All rights reserved. 25 | * 26 | * File: sys/arm/longjmp.s 27 | * 28 | * Implements siglongjmp(), longjmp(), _longjmp() 29 | * 30 | */ 31 | 32 | #include 33 | #include "_setjmp.h" 34 | 35 | /* 36 | * longjmp routines 37 | */ 38 | 39 | /* void siglongjmp(sigjmp_buf env, int val); */ 40 | 41 | ENTRY_POINT(_siglongjmp) 42 | ldr r2, [ r0, #JMP_SIGFLAG ] // load sigflag 43 | cmp r2, #0 // test if zero 44 | beq L__exit // if zero do _longjmp() 45 | // else *** fall through *** to longjmp() 46 | 47 | /* void longjmp(jmp_buf env, int val); */ 48 | 49 | ENTRY_POINT(_longjmp) 50 | #ifdef __ARM_ARCH_7K__ 51 | sub sp, sp, #16 // armv7k stack is 16-byte aligned. 52 | #else 53 | sub sp, sp, #4 54 | #endif 55 | mov r6, r0 // preserve args across _sigprocmask 56 | mov r8, r1 57 | ldr r0, [ r6, #JMP_sigmask ] // restore the signal mask 58 | mov r1, sp // set 59 | str r0, [sp] 60 | movs r0, #3 // SIG_SETMASK 61 | movs r2, #0 // oset 62 | CALL_EXTERNAL(_sigprocmask) 63 | 64 | // Restore the sigaltstack status 65 | ldr r0, [r6, JMP_sigonstack] // r0 = saved sigonstack info 66 | CALL_EXTERNAL(__sigunaltstack) 67 | 68 | mov r0, r6 69 | mov r1, r8 70 | #ifdef __ARM_ARCH_7K__ 71 | add sp, sp, #16 // armv7k stack is 16-byte aligned. 72 | #else 73 | add sp, sp, #4 74 | #endif 75 | L__exit: 76 | BRANCH_EXTERNAL(__longjmp) 77 | -------------------------------------------------------------------------------- /src/ucontext/arm64/asm_help.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | /* ASM Macro helpers */ 25 | #if defined(__ASSEMBLER__) 26 | 27 | .macro ARM64_STACK_PROLOG 28 | #if __has_feature(ptrauth_returns) 29 | pacibsp 30 | #endif 31 | .endmacro 32 | 33 | .macro ARM64_STACK_EPILOG 34 | #if __has_feature(ptrauth_returns) 35 | retab 36 | #else 37 | ret 38 | #endif 39 | .endmacro 40 | 41 | #define PUSH_FRAME \ 42 | stp fp, lr, [sp, #-16]! %% \ 43 | mov fp, sp %% 44 | 45 | #define POP_FRAME \ 46 | mov sp, fp %% \ 47 | ldp fp, lr, [sp], #16 %% 48 | #endif /* ASSEMBLER */ 49 | 50 | /* Offsets of the various register states inside of the mcontext data */ 51 | #define MCONTEXT_OFFSET_X0 16 52 | 53 | #define MCONTEXT_OFFSET_X19_X20 168 54 | #define MCONTEXT_OFFSET_X21_X22 184 55 | #define MCONTEXT_OFFSET_X23_X24 200 56 | 57 | #define MCONTEXT_OFFSET_X25_X26 216 58 | #define MCONTEXT_OFFSET_X27_X28 232 59 | 60 | #define MCONTEXT_OFFSET_FP_LR 248 61 | #define MCONTEXT_OFFSET_SP 264 62 | #define MCONTEXT_OFFSET_FLAGS 284 63 | 64 | #define MCONTEXT_OFFSET_D8 424 65 | #define MCONTEXT_OFFSET_D9 440 66 | #define MCONTEXT_OFFSET_D10 456 67 | #define MCONTEXT_OFFSET_D11 472 68 | #define MCONTEXT_OFFSET_D12 488 69 | #define MCONTEXT_OFFSET_D13 504 70 | #define MCONTEXT_OFFSET_D14 520 71 | #define MCONTEXT_OFFSET_D15 536 72 | 73 | #if __has_feature(ptrauth_calls) 74 | #define LR_SIGNED_WITH_IB 0x2 /* Copied from __DARWIN_ARM_THREAD_STATE64_FLAGS_IB_SIGNED_LR */ 75 | #define LR_SIGNED_WITH_IB_BIT 0x1 76 | #endif 77 | -------------------------------------------------------------------------------- /private/os/apt_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __OS_APT_PRIVATE__ 22 | #define __OS_APT_PRIVATE__ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #define OS_APT_SPI_VERSION 20241023 32 | 33 | #define OS_APT_MSG_AVAILABILITY \ 34 | __SPI_AVAILABLE(macos(15.4), ios(18.4), tvos(18.4), watchos(11.4), visionos(2.4)) 35 | 36 | __BEGIN_DECLS 37 | 38 | #pragma mark - Message namespaces and types 39 | 40 | OS_APT_MSG_AVAILABILITY 41 | OS_ENUM(apt_msg_namespace, uint8_t, 42 | apt_msg_ns_rsvd = 0, 43 | apt_msg_ns_swift = 1, 44 | ); 45 | 46 | OS_APT_MSG_AVAILABILITY 47 | OS_ENUM(apt_namespace_swift, uint8_t, 48 | apt_msg_ty_swift_rsvd = 0, 49 | apt_msg_ty_swift_task_running = 1, 50 | apt_msg_ty_swift_task_waiting_on = 2, 51 | ); 52 | 53 | #pragma mark - Private SPI for Swift Concurrency runtime 54 | 55 | /*! 56 | * @function os_apt_msg_async_task_running_4swift 57 | * 58 | * @abstract 59 | * Indicate that a Swift async task is running. 60 | * 61 | * @param task_id 62 | * ID of the Swift async task that is running. 63 | */ 64 | OS_APT_MSG_AVAILABILITY 65 | OS_EXPORT OS_NOTHROW 66 | void os_apt_msg_async_task_running_4swift(uint64_t task_id); 67 | 68 | /*! 69 | * @function os_apt_msg_async_task_waiting_on_4swift 70 | * 71 | * @abstract 72 | * Indicate that the current Swift async task is waiting on a result from another task. 73 | * 74 | * @param task_id 75 | * Task ID of the task that the current Swift async task is waiting on. 76 | */ 77 | OS_APT_MSG_AVAILABILITY 78 | OS_EXPORT OS_NOTHROW 79 | void os_apt_msg_async_task_waiting_on_4swift(uint64_t task_id); 80 | 81 | __END_DECLS 82 | 83 | #endif // __OS_APT_PRIVATE__ 84 | -------------------------------------------------------------------------------- /src/atomics/i386/pfz.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2013 Apple, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | #include 26 | 27 | .text 28 | 29 | /* Subroutine to make a preempt syscall. Called when we notice %ebx is 30 | * nonzero after returning from a PFZ subroutine. 31 | * When we enter kernel: 32 | * %edx = return address 33 | * %ecx = stack ptr 34 | * Destroys %eax, %ecx, and %edx. 35 | */ 36 | .align 4 37 | .private_extern _preempt 38 | .globl _preempt 39 | _preempt: 40 | popl %edx // get return address 41 | movl %esp,%ecx // save stack ptr here 42 | movl $(-58),%eax /* 58 = pfz_exit */ 43 | xorl %ebx,%ebx // clear "preemption pending" flag 44 | sysenter 45 | 46 | /* Subroutine to back off if we cannot get the spinlock. Called 47 | * after a few attempts inline in the PFZ subroutines. This code is 48 | * not in the PFZ. 49 | * %edi = ptr to queue head structure 50 | * %ebx = preemption flag (nonzero if preemption pending) 51 | * Destroys %eax. 52 | */ 53 | 54 | .align 4 55 | .private_extern _backoff 56 | .globl _backoff 57 | _backoff: 58 | testl %ebx,%ebx // does kernel want to preempt us? 59 | jz 1f // no 60 | xorl %ebx,%ebx // yes, clear flag 61 | pushl %edx // preserve regs used by preempt syscall 62 | pushl %ecx 63 | call _preempt 64 | popl %ecx 65 | popl %edx 66 | 1: 67 | pause // SMT-friendly backoff 68 | cmpl $0,8(%edi) // sniff the lockword 69 | jnz 1b // loop if still taken 70 | ret // lockword is free, so reenter PFZ 71 | -------------------------------------------------------------------------------- /src/ucontext/x86_64/_setcontext.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007,2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #if defined(__x86_64__) 25 | 26 | #include 27 | 28 | #define MCONTEXT_SS_RAX 16 29 | #define MCONTEXT_SS_RBX 24 30 | #define MCONTEXT_SS_RCX 32 31 | #define MCONTEXT_SS_RDX 40 32 | #define MCONTEXT_SS_RDI 48 33 | #define MCONTEXT_SS_RSI 56 34 | #define MCONTEXT_SS_RBP 64 35 | #define MCONTEXT_SS_RSP 72 36 | #define MCONTEXT_SS_R8 80 37 | #define MCONTEXT_SS_RIP 144 38 | #define MCONTEXT_SS_RFLAGS 152 39 | 40 | TEXT 41 | .private_extern __setcontext 42 | LABEL(__setcontext) 43 | /* struct mcontext_t * %rdi */ 44 | #if DEBUG 45 | movq MCONTEXT_SS_RSI(%rdi), %rsi 46 | movq MCONTEXT_SS_RCX(%rdi), %rcx 47 | movq MCONTEXT_SS_R8+0(%rdi), %r8 48 | movq MCONTEXT_SS_R8+8(%rdi), %r9 49 | movq MCONTEXT_SS_R8+16(%rdi), %r10 50 | movq MCONTEXT_SS_R8+24(%rdi), %r11 51 | #endif 52 | movq MCONTEXT_SS_RBX(%rdi), %rbx 53 | movq MCONTEXT_SS_R8+32(%rdi), %r12 54 | movq MCONTEXT_SS_R8+40(%rdi), %r13 55 | movq MCONTEXT_SS_R8+48(%rdi), %r14 56 | movq MCONTEXT_SS_R8+56(%rdi), %r15 57 | 58 | movq MCONTEXT_SS_RSP(%rdi), %rsp 59 | movq MCONTEXT_SS_RBP(%rdi), %rbp 60 | 61 | xorl %eax, %eax /* force x=getcontext(); ... setcontext(); to keep x==0 */ 62 | 63 | #if DEBUG 64 | movq MCONTEXT_SS_RIP(%rdi), %rdx 65 | movq MCONTEXT_SS_RDI(%rdi), %rdi 66 | jmp *%rdx 67 | #else 68 | jmp *MCONTEXT_SS_RIP(%rdi) 69 | #endif 70 | 71 | #endif /* __x86_64__ */ 72 | -------------------------------------------------------------------------------- /src/atomics/common/MKGetTimeBaseInfo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #if defined(i386) || defined(__arm__) 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | extern void spin_lock(int *); 31 | extern void spin_unlock(int *); 32 | 33 | /* deprecated function stub */ 34 | kern_return_t 35 | MKGetTimeBaseInfo( 36 | uint32_t *minAbsoluteTimeDelta, 37 | uint32_t *theAbsoluteTimeToNanosecondNumerator, 38 | uint32_t *theAbsoluteTimeToNanosecondDenominator, 39 | uint32_t *theProcessorToAbsoluteTimeNumerator, 40 | uint32_t *theProcessorToAbsoluteTimeDenominator 41 | ) { 42 | static struct mach_timebase_info mti = {0}; 43 | static int MKGetTimeBaseInfo_spin_lock = 0; 44 | 45 | if(mti.numer == 0) { 46 | kern_return_t err; 47 | spin_lock(&MKGetTimeBaseInfo_spin_lock); 48 | err = mach_timebase_info(&mti); 49 | spin_unlock(&MKGetTimeBaseInfo_spin_lock); 50 | if(err != KERN_SUCCESS) 51 | return err; 52 | } 53 | if(theAbsoluteTimeToNanosecondNumerator) 54 | *theAbsoluteTimeToNanosecondNumerator = mti.numer; 55 | if(theAbsoluteTimeToNanosecondDenominator) 56 | *theAbsoluteTimeToNanosecondDenominator = mti.denom; 57 | if(minAbsoluteTimeDelta) 58 | *minAbsoluteTimeDelta = 1; 59 | if(theProcessorToAbsoluteTimeNumerator) 60 | *theProcessorToAbsoluteTimeNumerator = 1; 61 | if(theProcessorToAbsoluteTimeDenominator) 62 | *theProcessorToAbsoluteTimeDenominator = 1; 63 | return KERN_SUCCESS; 64 | } 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /include/libkern/OSCacheControl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #ifndef _OS_CACHE_CONTROL_H_ 25 | #define _OS_CACHE_CONTROL_H_ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | __BEGIN_DECLS 33 | 34 | 35 | /* Functions performed by sys_cache_control(): */ 36 | 37 | /* Prepare memory for execution. This should be called 38 | * after writing machine instructions to memory, before 39 | * executing them. It syncs the dcache and icache. 40 | * On IA32 processors this function is a NOP, because 41 | * no synchronization is required. 42 | */ 43 | #define kCacheFunctionPrepareForExecution 1 44 | 45 | /* Flush data cache(s). This ensures that cached data 46 | * makes it all the way out to DRAM, and then removes 47 | * copies of the data from all processor caches. 48 | * It can be useful when dealing with cache incoherent 49 | * devices or DMA. 50 | */ 51 | #define kCacheFunctionFlushDcache 2 52 | 53 | 54 | /* perform one of the above cache functions: */ 55 | int sys_cache_control( int function, void *start, size_t len) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0); 56 | 57 | /* equivalent to sys_cache_control(kCacheFunctionPrepareForExecution): */ 58 | void sys_icache_invalidate( void *start, size_t len) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0); 59 | 60 | /* equivalent to sys_cache_control(kCacheFunctionFlushDcache): */ 61 | void sys_dcache_flush( void *start, size_t len) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0); 62 | 63 | 64 | __END_DECLS 65 | 66 | #endif /* _OS_CACHE_CONTROL_H_ */ 67 | -------------------------------------------------------------------------------- /src/setjmp/generic/setjmperr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | /* 24 | * Copyright (c) 1980 Regents of the University of California. 25 | * All rights reserved. 26 | * 27 | * Redistribution and use in source and binary forms are permitted 28 | * provided that the above copyright notice and this paragraph are 29 | * duplicated in all such forms and that any documentation, 30 | * advertising materials, and other materials related to such 31 | * distribution and use acknowledge that the software was developed 32 | * by the University of California, Berkeley. The name of the 33 | * University may not be used to endorse or promote products derived 34 | * from this software without specific prior written permission. 35 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 36 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 37 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 38 | */ 39 | 40 | #if defined(LIBC_SCCS) && !defined(lint) 41 | static char sccsid[] = "@(#)setjmperr.c 5.4 (Berkeley) 6/27/88"; 42 | #endif /* LIBC_SCCS and not lint */ 43 | 44 | #include 45 | #include 46 | 47 | #if !TARGET_OS_IPHONE && (defined(__i386__) || defined(__x86_64__)) 48 | /* 49 | * This routine is called from longjmp() when an error occurs. 50 | * Programs that wish to exit gracefully from this error may 51 | * write their own versions. 52 | * If this routine returns, the program is aborted. 53 | */ 54 | 55 | void 56 | longjmperror() 57 | { 58 | #define ERRMSG "longjmp botch\n" 59 | write(2, ERRMSG, sizeof(ERRMSG) - 1); 60 | } 61 | #endif 62 | -------------------------------------------------------------------------------- /src/ucontext/x86_64/getcontext.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007,2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #if defined(__x86_64__) 25 | 26 | #include 27 | 28 | #define MCONTEXT_SS_RAX 16 29 | #define MCONTEXT_SS_RBX 24 30 | #define MCONTEXT_SS_RCX 32 31 | #define MCONTEXT_SS_RDX 40 32 | #define MCONTEXT_SS_RDI 48 33 | #define MCONTEXT_SS_RSI 56 34 | #define MCONTEXT_SS_RBP 64 35 | #define MCONTEXT_SS_RSP 72 36 | #define MCONTEXT_SS_R8 80 37 | #define MCONTEXT_SS_RIP 144 38 | #define MCONTEXT_SS_RFLAGS 152 39 | 40 | TEXT 41 | LABEL(_getcontext) 42 | /* struct ucontext_t * $rdi */ 43 | push %rbp 44 | movq %rsp, %rbp 45 | movq %rsp, %rsi 46 | CALL_EXTERN(_getmcontext) /* getmcontext(uctx, sp) */ 47 | pop %rbp 48 | 49 | #if DEBUG 50 | movq $0, MCONTEXT_SS_RAX(%rax) 51 | movq $0, MCONTEXT_SS_RDX(%rax) 52 | movq $0, MCONTEXT_SS_RCX(%rax) 53 | movq $0, MCONTEXT_SS_RDI(%rax) 54 | movq $0, MCONTEXT_SS_RSI(%rax) 55 | movq $0, MCONTEXT_SS_R8(%rax) 56 | movq $0, MCONTEXT_SS_R8+8(%rax) 57 | movq $0, MCONTEXT_SS_R8+16(%rax) 58 | movq $0, MCONTEXT_SS_R8+24(%rax) 59 | movq $0, MCONTEXT_SS_RFLAGS(%rax) 60 | #endif 61 | 62 | movq %rbp, MCONTEXT_SS_RBP(%rax) 63 | movq %rbx, MCONTEXT_SS_RBX(%rax) 64 | movq %r12, MCONTEXT_SS_R8+32(%rax) 65 | movq %r13, MCONTEXT_SS_R8+40(%rax) 66 | movq %r14, MCONTEXT_SS_R8+48(%rax) 67 | movq %r15, MCONTEXT_SS_R8+56(%rax) 68 | movq (%rsp), %rcx /* return address */ 69 | movq %rcx, MCONTEXT_SS_RIP(%rax) 70 | leaq 8(%rsp), %rcx 71 | movq %rcx, MCONTEXT_SS_RSP(%rax) 72 | xorl %eax, %eax 73 | ret 74 | 75 | #endif /* __x86_64__ */ 76 | -------------------------------------------------------------------------------- /src/setjmp/arm/setjmp.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | /* 24 | * Copyright (c) 1998-2008 Apple Inc. All rights reserved. 25 | * 26 | * File: sys/arm/setjmp.s 27 | * 28 | * Implements sigsetjmp(), setjmp(), _setjmp() 29 | * 30 | */ 31 | 32 | #include 33 | #include "_setjmp.h" 34 | 35 | /* 36 | * setjmp routines 37 | */ 38 | 39 | /* int sigsetjmp(sigjmp_buf env, int savemask); */ 40 | 41 | ENTRY_POINT(_sigsetjmp) 42 | str r1, [ r0, #JMP_SIGFLAG ] // save sigflag 43 | cmp r1, #0 // test if r1 is 0 44 | beq L__exit // if r1 == 0 do _setjmp() 45 | // else *** fall through *** to setjmp() 46 | 47 | /* int setjmp(jmp_buf env); */ 48 | 49 | ENTRY_POINT(_setjmp) 50 | str lr, [ r0, #JMP_lr ] 51 | str r8, [ r0, #JMP_r8 ] 52 | mov r8, r0 // r8 = jmp_buf 53 | 54 | // Get previous sigmask 55 | mov r0, #1 // r0 = SIG_BLOCK 56 | mov r1, #0 // r1 = NULL 57 | add r2, r8, #JMP_sigmask // r2 = address to put the sigmask in 58 | CALL_EXTERNAL(_sigprocmask) // sigprocmask(SIGBLOCK, NULL, &old_mask); 59 | 60 | // Get altstack status 61 | sub sp, sp, #32 // Put a stack_t on the stack 62 | mov r0, #0 // r0 = ss = NULL 63 | mov r1, sp // r1 = oss = the place on the stack where stack_t is located 64 | CALL_EXTERNAL(___sigaltstack) // sigaltstack(NULL, oss) 65 | ldr r0, [sp, STACK_SSFLAGS] // r0 = ss flags from stack_t 66 | str r0, [r8, JMP_sigonstack] // *(r8 + JMP_sigonstack) = r0 67 | add sp, sp, #32 // reset sp 68 | 69 | // Do the remaining register stuff 70 | mov r0, r8 // restore jmp_buf ptr 71 | ldr r8, [ r0, #JMP_r8 ] 72 | ldr lr, [ r0, #JMP_lr ] 73 | L__exit: 74 | BRANCH_EXTERNAL(__setjmp) 75 | -------------------------------------------------------------------------------- /src/atomics/arm64/OSAtomic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | 26 | #if TARGET_OS_OSX || TARGET_OS_DRIVERKIT 27 | 28 | /* 29 | * This file implements the following functions for the arm64 architecture. 30 | * 31 | * void OSAtomicFifoEnqueue( OSFifoQueueHead *__list, void *__new, 32 | * size_t __offset); 33 | * void* OSAtomicFifoDequeue( OSFifoQueueHead *__list, size_t __offset); 34 | * 35 | */ 36 | 37 | #include 38 | #include 39 | 40 | #include "libkern/OSAtomic.h" 41 | #include "../OSAtomicFifo.h" 42 | #include "os/internal.h" 43 | 44 | typedef void (OSAtomicFifoEnqueue_t)(OSFifoQueueHead *, void *, size_t); 45 | typedef void *(OSAtomicFifoDequeue_t)(OSFifoQueueHead *, size_t); 46 | 47 | void OSAtomicFifoEnqueue(OSFifoQueueHead *__list, void *__new, size_t __offset) 48 | { 49 | if (unlikely(!commpage_pfz_base)) { 50 | __LIBPLATFORM_INTERNAL_CRASH__(commpage_pfz_base, 51 | "Invalid commpage pfz base."); 52 | } 53 | void *addr = commpage_pfz_base; 54 | addr += _COMM_PAGE_TEXT_ATOMIC_ENQUEUE; 55 | 56 | OSAtomicFifoEnqueue_t *OSAtomicFifoEnqueueInternal = SIGN_PFZ_FUNCTION_PTR(addr); 57 | 58 | return OSAtomicFifoEnqueueInternal(__list, __new, __offset); 59 | } 60 | 61 | void * OSAtomicFifoDequeue( OSFifoQueueHead *__list, size_t __offset) 62 | { 63 | if (unlikely(!commpage_pfz_base)) { 64 | __LIBPLATFORM_INTERNAL_CRASH__(commpage_pfz_base, 65 | "Invalid commpage pfz base."); 66 | } 67 | void *addr = commpage_pfz_base; 68 | addr += _COMM_PAGE_TEXT_ATOMIC_DEQUEUE; 69 | 70 | OSAtomicFifoDequeue_t *OSAtomicFifoDequeueInternal = SIGN_PFZ_FUNCTION_PTR(addr); 71 | 72 | return OSAtomicFifoDequeueInternal(__list, __offset); 73 | } 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /src/init.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include <_libkernel_init.h> 29 | #include <_simple.h> 30 | #include "os/internal.h" 31 | 32 | struct ProgramVars; /* forward reference */ 33 | 34 | extern void _simple_asl_init(const char *envp[], const struct ProgramVars *vars); 35 | extern void __pfz_setup(const char *apple[]); 36 | extern void __os_security_config_init(const char *apple[]); 37 | 38 | #if !VARIANT_STATIC 39 | static const struct _libkernel_string_functions _platform_string_functions = { 40 | .version = 1, 41 | .bzero = _platform_bzero, 42 | .memchr = _platform_memchr, 43 | .memcmp = _platform_memcmp, 44 | .memmove = _platform_memmove, 45 | .memccpy = _platform_memccpy, 46 | .memset = _platform_memset, 47 | .strchr = _platform_strchr, 48 | .strcmp = _platform_strcmp, 49 | .strcpy = _platform_strcpy, 50 | .strlcat = _platform_strlcat, 51 | .strlcpy = _platform_strlcpy, 52 | .strlen = _platform_strlen, 53 | .strncmp = _platform_strncmp, 54 | .strncpy = _platform_strncpy, 55 | .strnlen = _platform_strnlen, 56 | .strstr = _platform_strstr, 57 | }; 58 | #endif 59 | 60 | 61 | void 62 | __libplatform_init(void *future_use __unused, const char *envp[], 63 | const char *apple[], const struct ProgramVars *vars) 64 | { 65 | #if !VARIANT_STATIC 66 | __os_security_config_init(apple); 67 | #endif 68 | 69 | 70 | /* In the Simulator, we just provide _simple for dyld */ 71 | #if !TARGET_OS_SIMULATOR 72 | __pfz_setup(apple); 73 | #endif 74 | #if !TARGET_OS_DRIVERKIT 75 | _simple_asl_init(envp, vars); 76 | #endif 77 | 78 | #if !VARIANT_STATIC 79 | __libkernel_platform_init(&_platform_string_functions); 80 | #endif 81 | } 82 | -------------------------------------------------------------------------------- /private/os/semaphore_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __OS_SEMAPHORE_PRIVATE__ 22 | #define __OS_SEMAPHORE_PRIVATE__ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | OS_ASSUME_NONNULL_BEGIN 30 | 31 | __BEGIN_DECLS 32 | 33 | #define OS_SEMAPHORE_SPI_VERSION 20130313 34 | 35 | typedef uintptr_t os_semaphore_t; 36 | 37 | __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_7_0) 38 | OS_EXPORT OS_WARN_RESULT OS_NOTHROW 39 | os_semaphore_t _os_semaphore_create(void); 40 | 41 | __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_7_0) 42 | OS_EXPORT OS_NOTHROW 43 | void _os_semaphore_dispose(os_semaphore_t); 44 | 45 | __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_7_0) 46 | OS_EXPORT OS_NOTHROW 47 | void _os_semaphore_wait(os_semaphore_t); 48 | 49 | __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_7_0) 50 | OS_EXPORT OS_NOTHROW 51 | void _os_semaphore_signal(os_semaphore_t); 52 | 53 | OS_WARN_RESULT OS_NOTHROW 54 | __header_always_inline os_semaphore_t 55 | os_get_cached_semaphore(void) 56 | { 57 | os_semaphore_t sema; 58 | sema = (os_semaphore_t)_os_tsd_get_direct(__TSD_SEMAPHORE_CACHE); 59 | if (os_unlikely(!sema)) { 60 | return _os_semaphore_create(); 61 | } 62 | _os_tsd_set_direct(__TSD_SEMAPHORE_CACHE, 0); 63 | return sema; 64 | } 65 | 66 | OS_NOTHROW 67 | __header_always_inline void 68 | os_put_cached_semaphore(os_semaphore_t sema) 69 | { 70 | os_semaphore_t old_sema; 71 | old_sema = (os_semaphore_t)_os_tsd_get_direct(__TSD_SEMAPHORE_CACHE); 72 | _os_tsd_set_direct(__TSD_SEMAPHORE_CACHE, (void*)sema); 73 | if (os_unlikely(old_sema)) { 74 | return _os_semaphore_dispose(old_sema); 75 | } 76 | } 77 | 78 | OS_NOTHROW 79 | __header_always_inline void 80 | os_semaphore_wait(os_semaphore_t sema) 81 | { 82 | return _os_semaphore_wait(sema); 83 | } 84 | 85 | OS_NOTHROW 86 | __header_always_inline void 87 | os_semaphore_signal(os_semaphore_t sema) 88 | { 89 | return _os_semaphore_signal(sema); 90 | } 91 | 92 | __END_DECLS 93 | 94 | OS_ASSUME_NONNULL_END 95 | 96 | #endif // __OS_SEMAPHORE_PRIVATE__ 97 | -------------------------------------------------------------------------------- /src/string/generic/strstr.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1990, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * This code is derived from software contributed to Berkeley by 6 | * Chris Torek. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 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 the 15 | * documentation and/or other materials provided with the distribution. 16 | * 4. Neither the name of the University nor the names of its contributors 17 | * may be used to endorse or promote products derived from this software 18 | * without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 | * SUCH DAMAGE. 31 | */ 32 | 33 | #if defined(LIBC_SCCS) && !defined(lint) 34 | static char sccsid[] = "@(#)strstr.c 8.1 (Berkeley) 6/4/93"; 35 | #endif /* LIBC_SCCS and not lint */ 36 | #include 37 | __FBSDID("$FreeBSD: src/lib/libc/string/strstr.c,v 1.6 2009/02/03 17:58:20 danger Exp $"); 38 | 39 | #include 40 | #include 41 | 42 | #if !_PLATFORM_OPTIMIZED_STRSTR 43 | 44 | /* 45 | * Find the first occurrence of find in s. 46 | */ 47 | char * 48 | _platform_strstr(const char *s, const char *find) 49 | { 50 | char c, sc; 51 | size_t len; 52 | 53 | if ((c = *find++) != '\0') { 54 | len = _platform_strlen(find); 55 | do { 56 | do { 57 | if ((sc = *s++) == '\0') 58 | return (NULL); 59 | } while (sc != c); 60 | } while (_platform_strncmp(s, find, len) != 0); 61 | s--; 62 | } 63 | return ((char *)s); 64 | } 65 | 66 | #if VARIANT_STATIC 67 | char * 68 | strstr(const char *s, const char *find) 69 | { 70 | return _platform_strstr(s, find); 71 | } 72 | #endif 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /internal/os/internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __OS_INTERNAL_H__ 22 | #define __OS_INTERNAL_H__ 23 | 24 | #define __OS_ALLOC_INDIRECT__ 25 | 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #if defined(__arm__) 34 | #include 35 | #endif 36 | #include 37 | 38 | 39 | #include 40 | 41 | #include "os/base_private.h" 42 | #include "os/semaphore_private.h" 43 | #include "os/crashlog_private.h" 44 | #include "yield.h" 45 | 46 | #define likely(x) os_likely(x) 47 | #define unlikely(x) os_unlikely(x) 48 | 49 | #define __LIBPLATFORM_CLIENT_CRASH__(rc, msg) \ 50 | OS_BUG_CLIENT(rc, "LIBPLATFORM", msg) 51 | #define __LIBPLATFORM_INTERNAL_CRASH__(rc, msg) \ 52 | OS_BUG_INTERNAL(rc, "LIBPLATFORM", msg) 53 | 54 | #define OS_NOEXPORT extern __attribute__((__visibility__("hidden"))) 55 | 56 | #define OS_VARIANT(f, v) OS_CONCAT(f, OS_CONCAT($VARIANT$, v)) 57 | 58 | #define _OS_ATOMIC_ALIAS_PRIVATE_EXTERN(n) 59 | #define OS_ATOMIC_EXPORT OS_EXPORT 60 | #define _OS_ATOMIC_ALIAS_GLOBL(n) \ 61 | ".globl _" OS_STRINGIFY(n) "\n\t" 62 | #ifdef __thumb__ 63 | #define _OS_ATOMIC_ALIAS_THUMB(n) \ 64 | ".thumb_func _" OS_STRINGIFY(n) "\n\t" 65 | #else 66 | #define _OS_ATOMIC_ALIAS_THUMB(n) 67 | #endif 68 | #define _OS_ATOMIC_ALIAS_SET(n, o) \ 69 | ".set _" OS_STRINGIFY(n) ", _" OS_STRINGIFY(o) 70 | 71 | #define OS_ATOMIC_ALIAS(n, o) __asm__( \ 72 | _OS_ATOMIC_ALIAS_PRIVATE_EXTERN(n) \ 73 | _OS_ATOMIC_ALIAS_GLOBL(n) \ 74 | _OS_ATOMIC_ALIAS_THUMB(n) \ 75 | _OS_ATOMIC_ALIAS_SET(n, o)) 76 | 77 | #define OS_ATOMIC_EXPORT_ALIAS(n, o) __asm__( \ 78 | _OS_ATOMIC_ALIAS_GLOBL(n) \ 79 | _OS_ATOMIC_ALIAS_THUMB(n) \ 80 | _OS_ATOMIC_ALIAS_SET(n, o)) 81 | 82 | #define _OS_VARIANT_RESOLVER(s, v, ...) \ 83 | __attribute__((visibility(OS_STRINGIFY(v)))) extern void* s(void); \ 84 | void* s(void) { \ 85 | __asm__(".symbol_resolver _" OS_STRINGIFY(s)); \ 86 | __VA_ARGS__ \ 87 | } 88 | 89 | #endif // __OS_INTERNAL_H__ 90 | -------------------------------------------------------------------------------- /private/os/alloc_once_impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. The rights granted to you under the License 10 | * may not be used to create, or enable the creation or redistribution of, 11 | * unlawful or unlicensed copies of an Apple operating system, or to 12 | * circumvent, violate, or enable the circumvention or violation of, any 13 | * terms of an Apple operating system software license agreement. 14 | * 15 | * Please obtain a copy of the License at 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 | * 18 | * The Original Code and all software distributed under the License are 19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 | * Please see the License for the specific language governing rights and 24 | * limitations under the License. 25 | * 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 | */ 28 | 29 | #ifndef __OS_ALLOC_ONCE_IMPL__ 30 | #define __OS_ALLOC_ONCE_IMPL__ 31 | 32 | #ifndef __OS_ALLOC_INDIRECT__ 33 | #error "Please include instead of this file directly." 34 | #endif 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | __BEGIN_DECLS 42 | 43 | #define OS_ALLOC_SPI_VERSION 20120430 44 | 45 | #define OS_ALLOC_ONCE_KEY_MAX 100 46 | 47 | typedef os_once_t os_alloc_token_t; 48 | struct _os_alloc_once_s { 49 | os_alloc_token_t once; 50 | void *ptr; 51 | }; 52 | 53 | __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_6_0) 54 | extern struct _os_alloc_once_s _os_alloc_once_table[]; 55 | 56 | __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_6_0) 57 | OS_EXPORT OS_NONNULL1 58 | void* 59 | _os_alloc_once(struct _os_alloc_once_s *slot, size_t sz, os_function_t init); 60 | 61 | /* 62 | * The region allocated by os_alloc_once is 0-filled when initially 63 | * returned (or handed off to the initializer). 64 | */ 65 | OS_WARN_RESULT OS_NOTHROW OS_CONST 66 | __header_always_inline void* 67 | os_alloc_once(os_alloc_token_t token, size_t sz, os_function_t init) 68 | { 69 | struct _os_alloc_once_s *slot = &_os_alloc_once_table[token]; 70 | if (OS_EXPECT(slot->once, ~0l) != ~0l) { 71 | void *ptr = _os_alloc_once(slot, sz, init); 72 | OS_COMPILER_CAN_ASSUME(slot->once == ~0l); 73 | return ptr; 74 | } 75 | return slot->ptr; 76 | } 77 | 78 | __END_DECLS 79 | 80 | #endif // __OS_ALLOC_ONCE_IMPL__ 81 | -------------------------------------------------------------------------------- /src/ucontext/arm64/mcontext.c: -------------------------------------------------------------------------------- 1 | #include "asm_help.h" 2 | 3 | #define _XOPEN_SOURCE 600L 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | _Static_assert(offsetof(struct __darwin_mcontext64, __ss.__x[0]) == MCONTEXT_OFFSET_X0, 10 | "MCONTEXT_OFFSET_X0"); 11 | _Static_assert(offsetof(struct __darwin_mcontext64, __ss.__x[19]) == MCONTEXT_OFFSET_X19_X20, 12 | "MCONTEXT_OFFSET_X19_X20"); 13 | _Static_assert(offsetof(struct __darwin_mcontext64, __ss.__x[21]) == MCONTEXT_OFFSET_X21_X22, 14 | "MCONTEXT_OFFSET_X21_X22"); 15 | _Static_assert(offsetof(struct __darwin_mcontext64, __ss.__x[23]) == MCONTEXT_OFFSET_X23_X24, 16 | "MCONTEXT_OFFSET_X23_X24"); 17 | _Static_assert(offsetof(struct __darwin_mcontext64, __ss.__x[25]) == MCONTEXT_OFFSET_X25_X26, 18 | "MCONTEXT_OFFSET_X25_X26"); 19 | _Static_assert(offsetof(struct __darwin_mcontext64, __ss.__x[27]) == MCONTEXT_OFFSET_X27_X28, 20 | "MCONTEXT_OFFSET_X27_X28"); 21 | 22 | #if __has_feature(ptrauth_calls) 23 | _Static_assert(offsetof(struct __darwin_mcontext64, __ss.__opaque_fp) == MCONTEXT_OFFSET_FP_LR, 24 | "MCONTEXT_OFFSET_FP_LR"); 25 | _Static_assert(offsetof(struct __darwin_mcontext64, __ss.__opaque_sp) == MCONTEXT_OFFSET_SP, 26 | "MCONTEXT_OFFSET_SP"); 27 | _Static_assert(offsetof(struct __darwin_mcontext64, __ss.__opaque_flags) == MCONTEXT_OFFSET_FLAGS, 28 | "MCONTEXT_OFFSET_FLAGS"); 29 | #else 30 | _Static_assert(offsetof(struct __darwin_mcontext64, __ss.__fp) == MCONTEXT_OFFSET_FP_LR, 31 | "MCONTEXT_OFFSET_FP_LR"); 32 | _Static_assert(offsetof(struct __darwin_mcontext64, __ss.__sp) == MCONTEXT_OFFSET_SP, 33 | "MCONTEXT_OFFSET_SP"); 34 | #endif 35 | 36 | 37 | // Neon registers are 128 bits wide. d suffix refers to the last 64 bits of the 38 | // 128 bit register. Hence the -8 offset. 39 | _Static_assert(offsetof(struct __darwin_mcontext64, __ns.__v[8]) == (MCONTEXT_OFFSET_D8 - 8), 40 | "MCONTEXT_OFFSET_D8"); 41 | _Static_assert(offsetof(struct __darwin_mcontext64, __ns.__v[9]) == (MCONTEXT_OFFSET_D9 - 8), 42 | "MCONTEXT_OFFSET_D9"); 43 | _Static_assert(offsetof(struct __darwin_mcontext64, __ns.__v[10]) == (MCONTEXT_OFFSET_D10 - 8), 44 | "MCONTEXT_OFFSET_D10"); 45 | _Static_assert(offsetof(struct __darwin_mcontext64, __ns.__v[11]) == (MCONTEXT_OFFSET_D11 - 8), 46 | "MCONTEXT_OFFSET_D11"); 47 | _Static_assert(offsetof(struct __darwin_mcontext64, __ns.__v[12]) == (MCONTEXT_OFFSET_D12 - 8), 48 | "MCONTEXT_OFFSET_D12"); 49 | _Static_assert(offsetof(struct __darwin_mcontext64, __ns.__v[13]) == (MCONTEXT_OFFSET_D13 - 8), 50 | "MCONTEXT_OFFSET_D13"); 51 | _Static_assert(offsetof(struct __darwin_mcontext64, __ns.__v[14]) == (MCONTEXT_OFFSET_D14 - 8), 52 | "MCONTEXT_OFFSET_D14"); 53 | _Static_assert(offsetof(struct __darwin_mcontext64, __ns.__v[15]) == (MCONTEXT_OFFSET_D15 - 8), 54 | "MCONTEXT_OFFSET_D15"); 55 | 56 | #if __has_feature(ptrauth_calls) 57 | _Static_assert((1 << LR_SIGNED_WITH_IB_BIT) == LR_SIGNED_WITH_IB, "LR_SIGNED_WITH_IB_BIT"); 58 | #endif 59 | -------------------------------------------------------------------------------- /internal/os/internal_asm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. The rights granted to you under the License 10 | * may not be used to create, or enable the creation or redistribution of, 11 | * unlawful or unlicensed copies of an Apple operating system, or to 12 | * circumvent, violate, or enable the circumvention or violation of, any 13 | * terms of an Apple operating system software license agreement. 14 | * 15 | * Please obtain a copy of the License at 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 | * 18 | * The Original Code and all software distributed under the License are 19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 | * Please see the License for the specific language governing rights and 24 | * limitations under the License. 25 | * 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 | */ 28 | 29 | #ifndef __INTERNAL_ASM_H__ 30 | #define __INTERNAL_ASM_H__ 31 | 32 | #include 33 | 34 | #define OS_STRINGIFY1(s) #s 35 | #define OS_STRINGIFY(s) OS_STRINGIFY1(s) 36 | #define OS_CONCAT1(x, y) x ## y 37 | #define OS_CONCAT(x, y) OS_CONCAT1(x, y) 38 | 39 | #ifdef __ASSEMBLER__ 40 | #define OS_VARIANT(f, v) OS_CONCAT(_, OS_CONCAT(f, OS_CONCAT($VARIANT$, v))) 41 | #else 42 | #define OS_VARIANT(f, v) OS_CONCAT(f, OS_CONCAT($VARIANT$, v)) 43 | #endif 44 | 45 | #if defined(__ASSEMBLER__) 46 | 47 | #if defined(__i386__) || defined(__x86_64__) 48 | 49 | #define OS_VARIANT_FUNCTION_START(name, variant, alignment) \ 50 | .text ; \ 51 | .align alignment, 0x90 ; \ 52 | .private_extern OS_VARIANT(name, variant) ; \ 53 | OS_VARIANT(name, variant) ## : 54 | 55 | // GENERIC indicates that this function will be chosen as the generic 56 | // implementation (at compile time) when building targets which do not 57 | // support dyld variant resolves. 58 | #if defined(VARIANT_NO_RESOLVERS) || defined(VARIANT_DYLD) 59 | #define OS_VARIANT_FUNCTION_START_GENERIC(name, variant, alignment) \ 60 | OS_VARIANT_FUNCTION_START(name, variant, alignment) \ 61 | .globl _ ## name ; \ 62 | _ ## name ## : 63 | #else 64 | #define OS_VARIANT_FUNCTION_START_GENERIC OS_VARIANT_FUNCTION_START 65 | #endif 66 | 67 | #define OS_ATOMIC_FUNCTION_START(name, alignment) \ 68 | .text ; \ 69 | .align alignment, 0x90 ; \ 70 | .globl _ ## name ; \ 71 | _ ## name ## : 72 | 73 | #endif // defined(__i386__) || defined(__x86_64__) 74 | 75 | #endif // defined(__ASSEMBLER__) 76 | 77 | #endif // __INTERNAL_ASM_H__ 78 | -------------------------------------------------------------------------------- /src/string/generic/memset_pattern.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | 26 | #if !_PLATFORM_OPTIMIZED_MEMSET_PATTERN4 27 | 28 | void 29 | _platform_memset_pattern4(void *b, const void *pattern4, size_t len) 30 | { 31 | char * start = (char *)b; 32 | char * p = (char *)b; 33 | while ((start + len) - p >= 4) { 34 | _platform_memmove(p, pattern4, 4); 35 | p += 4; 36 | } 37 | if ((start + len) - p != 0) { 38 | _platform_memmove(p, pattern4, (start + len) - p); 39 | } 40 | } 41 | 42 | #if VARIANT_STATIC 43 | void 44 | memset_pattern4(void *b, const void *pattern4, size_t len) 45 | { 46 | return _platform_memset_pattern4(b, pattern4, len); 47 | } 48 | #endif 49 | 50 | #endif 51 | 52 | 53 | #if !_PLATFORM_OPTIMIZED_MEMSET_PATTERN8 54 | 55 | void 56 | _platform_memset_pattern8(void *b, const void *pattern8, size_t len) 57 | { 58 | char * start = (char *)b; 59 | char * p = (char *)b; 60 | while ((start + len) - p >= 8) { 61 | _platform_memmove(p, pattern8, 8); 62 | p += 8; 63 | } 64 | if ((start + len) - p != 0) { 65 | _platform_memmove(p, pattern8, (start + len) - p); 66 | } 67 | } 68 | 69 | #if VARIANT_STATIC 70 | void 71 | memset_pattern8(void *b, const void *pattern8, size_t len) 72 | { 73 | return _platform_memset_pattern8(b, pattern8, len); 74 | } 75 | #endif 76 | 77 | #endif 78 | 79 | 80 | #if !_PLATFORM_OPTIMIZED_MEMSET_PATTERN16 81 | 82 | void 83 | _platform_memset_pattern16(void *b, const void *pattern16, size_t len) 84 | { 85 | char * start = (char *)b; 86 | char * p = (char *)b; 87 | while ((start + len) - p >= 16) { 88 | _platform_memmove(p, pattern16, 16); 89 | p += 16; 90 | } 91 | if ((start + len) - p != 0) { 92 | _platform_memmove(p, pattern16, (start + len) - p); 93 | } 94 | } 95 | 96 | #if VARIANT_STATIC 97 | void 98 | memset_pattern16(void *b, const void *pattern16, size_t len) 99 | { 100 | return _platform_memset_pattern16(b, pattern16, len); 101 | } 102 | #endif 103 | 104 | #endif 105 | -------------------------------------------------------------------------------- /src/ucontext/generic/setcontext.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007, 2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #define _XOPEN_SOURCE 600L 25 | #include 26 | #include 27 | #include 28 | 29 | #if ((TARGET_OS_OSX || TARGET_OS_DRIVERKIT) && (defined(__LP64__) || defined(__i386__))) 30 | 31 | #include 32 | #include 33 | 34 | extern int _setcontext(const void *); 35 | 36 | /* This is a macro to capture all the code added in here that is purely to make 37 | * conformance tests pass and seems to have no functional reason nor is it 38 | * required by the standard */ 39 | #define CONFORMANCE_SPECIFIC_HACK 1 40 | 41 | int 42 | setcontext(const ucontext_t *uctx) 43 | { 44 | if (uctx->uc_mcsize == 0) { /* Invalid context */ 45 | errno = EINVAL; 46 | return -1; 47 | } 48 | 49 | sigprocmask(SIG_SETMASK, &uctx->uc_sigmask, NULL); 50 | 51 | mcontext_t mctx = uctx->uc_mcontext; 52 | #if CONFORMANCE_SPECIFIC_HACK 53 | // There is a conformance test which initialized a ucontext A by memcpy-ing 54 | // a ucontext B that was previously initialized with getcontext. 55 | // getcontext(B) modified B such that B.uc_mcontext = &B.__mcontext_data; 56 | // But by doing the memcpy of B to A, A.uc_mcontext = &B.__mcontext_data 57 | // when that's not necessarily what we want. We therefore have to 58 | // unfortunately ignore A.uc_mccontext and use &A.__mcontext_data even though we 59 | // don't know if A.__mcontext_data was properly initialized. This is really 60 | // because the conformance test doesn't initialize properly with multiple 61 | // getcontexts and instead copies contexts around. 62 | // 63 | // 64 | // Note that this hack, is causing us to fail when restoring a ucontext from 65 | // a signal. See Restoring context from signal 66 | // fails on intel and arm64 platforms 67 | mctx = (mcontext_t) &uctx->__mcontext_data; 68 | #endif 69 | 70 | #if defined(__x86_64__) || defined(__arm64__) 71 | return _setcontext(mctx); 72 | #else 73 | return _setcontext(uctx); 74 | #endif 75 | } 76 | 77 | #else /* TARGET_OS_OSX || TARGET_OS_DRIVERKIT */ 78 | 79 | int 80 | setcontext(const ucontext_t *uctx) 81 | { 82 | errno = ENOTSUP; 83 | return -1; 84 | } 85 | 86 | #endif /* TARGET_OS_OSX || TARGET_OS_DRIVERKIT */ 87 | -------------------------------------------------------------------------------- /src/ucontext/i386/_ctx_start.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | /* 25 | * Copyright (c) 2001 Daniel Eischen 26 | * All rights reserved. 27 | * 28 | * Redistribution and use in source and binary forms, with or without 29 | * modification, are permitted provided that the following conditions 30 | * are met: 31 | * 1. Redistributions of source code must retain the above copyright 32 | * notice, this list of conditions and the following disclaimer. 33 | * 2. Neither the name of the author nor the names of its contributors 34 | * may be used to endorse or promote products derived from this software 35 | * without specific prior written permission. 36 | * 37 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 38 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 40 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 41 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 42 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 43 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 45 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 46 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 | * SUCH DAMAGE. 48 | */ 49 | 50 | #if defined(__i386__) 51 | 52 | #include 53 | 54 | /* 55 | * _ctx_start((void *func)(int arg1, ..., argn), 56 | * int arg1, ..., argn, ucontext_t *ucp) 57 | * 58 | * 0(%esp) - func 59 | * 4(%esp) - arg1 60 | * 8(%esp) - arg2 61 | * ... 62 | * (4*n)(%esp) - argn 63 | * (4*(n + 1))(%esp) - ucp, %ebp setup to point here (base of stack) 64 | */ 65 | TEXT 66 | .private_extern __ctx_start 67 | LABEL(__ctx_start) 68 | popl %eax /* get start function */ 69 | call *%eax /* call start function */ 70 | movl %esi, %esp /* 71 | * setup stack for completion routine; 72 | * ucp is now at top of stack 73 | */ 74 | CALL_EXTERN(__ctx_done) /* should never return */ 75 | int $5 /* trap */ 76 | 77 | #endif /* __i386__ */ 78 | -------------------------------------------------------------------------------- /src/string/generic/memcmp_zero.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Apple, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | * 23 | * This file implements the following function generically: 24 | * 25 | * unsigned long memcmp_zero_aligned8(const void *s, size_t n); 26 | * 27 | * memcmp_zero_aligned8() checks whether string s of n bytes contains all 28 | * zeros. Address and size of the string s must be 8 byte-aligned. Returns 0 29 | * if true, 1 otherwise. Also returns 0 if n is 0. 30 | */ 31 | 32 | #include 33 | 34 | #if !_PLATFORM_OPTIMIZED_MEMCMP_ZERO_ALIGNED8 35 | 36 | #if defined(__LP64__) 37 | 38 | unsigned long 39 | _platform_memcmp_zero_aligned8(const void *s, size_t n) 40 | { 41 | size_t size = n; 42 | 43 | if (size == 0) { 44 | return 0; 45 | } 46 | 47 | const uint64_t *p = (const uint64_t *)s; 48 | uint64_t a = p[0]; 49 | 50 | _Static_assert(sizeof(unsigned long) == sizeof(uint64_t), ""); 51 | 52 | if (size < 4 * sizeof(uint64_t)) { 53 | if (size > 1 * sizeof(uint64_t)) { 54 | a |= p[1]; 55 | if (size > 2 * sizeof(uint64_t)) { 56 | a |= p[2]; 57 | } 58 | } 59 | } else { 60 | size_t count = size / sizeof(uint64_t); 61 | uint64_t b = p[1]; 62 | uint64_t c = p[2]; 63 | uint64_t d = p[3]; 64 | 65 | /* 66 | * note: for sizes not a multiple of 32 bytes, this will load 67 | * the bytes [size % 32 .. 32) twice which is ok 68 | */ 69 | while (count > 4) { 70 | count -= 4; 71 | a |= p[count + 0]; 72 | b |= p[count + 1]; 73 | c |= p[count + 2]; 74 | d |= p[count + 3]; 75 | } 76 | 77 | a |= b | c | d; 78 | } 79 | 80 | return (a != 0); 81 | } 82 | 83 | #else // defined(__LP64__) 84 | 85 | unsigned long 86 | _platform_memcmp_zero_aligned8(const void *s, size_t n) 87 | { 88 | uintptr_t p = (uintptr_t)s; 89 | uintptr_t end = (uintptr_t)s + n; 90 | uint32_t a, b; 91 | 92 | _Static_assert(sizeof(unsigned long) == sizeof(uint32_t), ""); 93 | 94 | a = 0; 95 | b = 0; 96 | 97 | for (; p < end; p += sizeof(uint64_t)) { 98 | uint64_t v = *(const uint64_t *)p; 99 | a |= (uint32_t)v; 100 | b |= (uint32_t)(v >> 32); 101 | } 102 | 103 | return (a | b) != 0; 104 | } 105 | 106 | #endif // defined(__LP64__) 107 | 108 | #endif // !_PLATFORM_OPTIMIZED_MEMCMP_ZERO_ALIGNED8 109 | -------------------------------------------------------------------------------- /src/string/generic/ffsll.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1990, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 4. Neither the name of the University nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software 15 | * without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #include 31 | #include 32 | __FBSDID("$FreeBSD: src/lib/libc/string/ffsll.c,v 1.1 2008/11/03 10:22:19 kib Exp $"); 33 | 34 | #include 35 | 36 | /* 37 | * Find First Set bit 38 | */ 39 | int 40 | ffsll(long long mask) 41 | { 42 | #if __has_builtin(__builtin_ffsll) 43 | return __builtin_ffsll(mask); 44 | #elif __has_builtin(__builtin_ctzll) 45 | if (mask == 0) 46 | return (0); 47 | 48 | return __builtin_ctzll(mask) + 1; 49 | #else 50 | int bit; 51 | 52 | if (mask == 0) 53 | return (0); 54 | for (bit = 1; !(mask & 1); bit++) 55 | mask = (unsigned long long)mask >> 1; 56 | return (bit); 57 | #endif 58 | } 59 | 60 | #if VARIANT_DYLD && TARGET_OS_SIMULATOR 61 | int 62 | ffsl(long mask) 63 | { 64 | #if __has_builtin(__builtin_ffsl) 65 | return __builtin_ffsl(mask); 66 | #elif __has_builtin(__builtin_ctzl) 67 | if (mask == 0) 68 | return (0); 69 | 70 | return __builtin_ctzl(mask) + 1; 71 | #else 72 | int bit; 73 | 74 | if (mask == 0) 75 | return (0); 76 | for (bit = 1; !(mask & 1); bit++) 77 | mask = (unsigned long)mask >> 1; 78 | return (bit); 79 | #endif 80 | } 81 | 82 | int 83 | ffs(int mask) 84 | { 85 | #if __has_builtin(__builtin_ffs) 86 | return __builtin_ffs(mask); 87 | #elif __has_builtin(__builtin_ctz) 88 | if (mask == 0) 89 | return (0); 90 | 91 | return __builtin_ctz(mask) + 1; 92 | #else 93 | int bit; 94 | 95 | if (mask == 0) 96 | return (0); 97 | for (bit = 1; !(mask & 1); bit++) 98 | mask = (unsigned)mask >> 1; 99 | return (bit); 100 | #endif 101 | } 102 | #endif 103 | 104 | -------------------------------------------------------------------------------- /src/string/generic/flsll.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1990, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 4. Neither the name of the University nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software 15 | * without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #include 31 | #include 32 | __FBSDID("$FreeBSD: src/lib/libc/string/flsll.c,v 1.1 2008/11/03 10:22:19 kib Exp $"); 33 | 34 | #include 35 | 36 | /* 37 | * Find Last Set bit 38 | */ 39 | int 40 | flsll(long long mask) 41 | { 42 | #if __has_builtin(__builtin_flsll) 43 | return __builtin_flsll(mask); 44 | #elif __has_builtin(__builtin_clzll) 45 | if (mask == 0) 46 | return (0); 47 | 48 | return (sizeof(mask) << 3) - __builtin_clzll(mask); 49 | #else 50 | int bit; 51 | 52 | if (mask == 0) 53 | return (0); 54 | for (bit = 1; mask != 1; bit++) 55 | mask = (unsigned long long)mask >> 1; 56 | return (bit); 57 | #endif 58 | } 59 | 60 | #if VARIANT_DYLD && TARGET_OS_SIMULATOR 61 | int 62 | flsl(long mask) 63 | { 64 | #if __has_builtin(__builtin_flsl) 65 | return __builtin_flsl(mask); 66 | #elif __has_builtin(__builtin_clzl) 67 | if (mask == 0) 68 | return (0); 69 | 70 | return (sizeof(mask) << 3) - __builtin_clzl(mask); 71 | #else 72 | int bit; 73 | 74 | if (mask == 0) 75 | return (0); 76 | for (bit = 1; mask != 1; bit++) 77 | mask = (unsigned long)mask >> 1; 78 | return (bit); 79 | #endif 80 | } 81 | 82 | int 83 | fls(int mask) 84 | { 85 | #if __has_builtin(__builtin_fls) 86 | return __builtin_fls(mask); 87 | #elif __has_builtin(__builtin_clz) 88 | if (mask == 0) 89 | return (0); 90 | 91 | return (sizeof(mask) << 3) - __builtin_clz(mask); 92 | #else 93 | int bit; 94 | 95 | if (mask == 0) 96 | return (0); 97 | for (bit = 1; mask != 1; bit++) 98 | mask = (unsigned)mask >> 1; 99 | return (bit); 100 | #endif 101 | } 102 | #endif 103 | -------------------------------------------------------------------------------- /src/exclavekit/lock.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #define OS_UNFAIR_LOCK_OPTIONS_MASK \ 29 | (os_unfair_lock_options_t)(OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION) | \ 30 | (os_unfair_lock_options_t)(OS_UNFAIR_LOCK_ADAPTIVE_SPIN) 31 | 32 | _Static_assert(sizeof(xrt_sync_object_t) <= 33 | sizeof(struct os_unfair_lock_s), "os_unfair_lock and xrt_sync_object_t size mismatch"); 34 | 35 | _Static_assert(sizeof(xrt_sync_object_t) <= 36 | sizeof(struct os_unfair_recursive_lock_s), "os_unfair_recursive_lock and xrt_sync_object_t size mismatch"); 37 | 38 | #pragma mark - 39 | #pragma mark os_unfair_lock 40 | 41 | void os_unfair_lock_lock(os_unfair_lock_t lock) 42 | { 43 | xrt_sync_mutex_lock((xrt_sync_mutex_t *) &lock->_os_unfair_lock_opaque); 44 | } 45 | 46 | bool os_unfair_lock_trylock(os_unfair_lock_t lock) 47 | { 48 | return xrt_sync_mutex_try_lock((xrt_sync_mutex_t *) &lock->_os_unfair_lock_opaque); 49 | } 50 | 51 | void os_unfair_lock_unlock(os_unfair_lock_t lock) 52 | { 53 | xrt_sync_mutex_unlock((xrt_sync_mutex_t *) &lock->_os_unfair_lock_opaque); 54 | } 55 | 56 | void os_unfair_lock_assert_owner(const os_unfair_lock *lock) 57 | { 58 | xrt_sync_mutex_assert_owner((xrt_sync_mutex_t *) &lock->_os_unfair_lock_opaque); 59 | } 60 | 61 | void os_unfair_lock_assert_not_owner(const os_unfair_lock *lock) 62 | { 63 | xrt_sync_mutex_assert_not_owner((xrt_sync_mutex_t *) &lock->_os_unfair_lock_opaque); 64 | } 65 | 66 | void os_unfair_lock_lock_with_options(os_unfair_lock_t lock, os_unfair_lock_options_t options) 67 | { 68 | assert((options & OS_UNFAIR_LOCK_OPTIONS_MASK) || (options == OS_UNFAIR_LOCK_NONE)); 69 | os_unfair_lock_lock(lock); 70 | } 71 | 72 | #pragma mark - 73 | #pragma mark os_unfair_recursive_lock 74 | 75 | void os_unfair_recursive_lock_lock(os_unfair_recursive_lock_t lock) 76 | { 77 | os_unfair_lock_lock((os_unfair_lock *)(&lock->ourl_lock)); 78 | } 79 | 80 | void os_unfair_recursive_lock_lock_with_options(os_unfair_recursive_lock_t lock, 81 | os_unfair_lock_options_t options) 82 | { 83 | os_unfair_lock_lock((os_unfair_lock *)(&lock->ourl_lock)); 84 | } 85 | 86 | bool os_unfair_recursive_lock_trylock(os_unfair_recursive_lock_t lock) 87 | { 88 | return os_unfair_lock_trylock((os_unfair_lock *)(&lock->ourl_lock)); 89 | } 90 | 91 | void os_unfair_recursive_lock_unlock(os_unfair_recursive_lock_t lock) { 92 | os_unfair_lock_unlock((os_unfair_lock *)(&lock->ourl_lock)); 93 | } 94 | -------------------------------------------------------------------------------- /man/ffs.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 1990, 1991, 1993 2 | .\" The Regents of the University of California. All rights reserved. 3 | .\" 4 | .\" This code is derived from software contributed to Berkeley by 5 | .\" Chris Torek. 6 | .\" Redistribution and use in source and binary forms, with or without 7 | .\" modification, are permitted provided that the following conditions 8 | .\" are met: 9 | .\" 1. Redistributions of source code must retain the above copyright 10 | .\" notice, this list of conditions and the following disclaimer. 11 | .\" 2. Redistributions in binary form must reproduce the above copyright 12 | .\" notice, this list of conditions and the following disclaimer in the 13 | .\" documentation and/or other materials provided with the distribution. 14 | .\" 4. Neither the name of the University nor the names of its contributors 15 | .\" may be used to endorse or promote products derived from this software 16 | .\" without specific prior written permission. 17 | .\" 18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 | .\" SUCH DAMAGE. 29 | .\" 30 | .\" @(#)ffs.3 8.2 (Berkeley) 4/19/94 31 | .\" $FreeBSD: src/lib/libc/string/ffs.3,v 1.15 2012/09/30 03:25:04 eadler Exp $ 32 | .\" 33 | .Dd September 29, 2012 34 | .Dt FFS 3 35 | .Os 36 | .Sh NAME 37 | .Nm ffs , 38 | .Nm ffsl , 39 | .Nm ffsll , 40 | .Nm fls , 41 | .Nm flsl , 42 | .Nm flsll 43 | .Nd find first or last bit set in a bit string 44 | .Sh LIBRARY 45 | .Lb libc 46 | .Sh SYNOPSIS 47 | .In strings.h 48 | .Ft int 49 | .Fn ffs "int value" 50 | .Ft int 51 | .Fn ffsl "long value" 52 | .Ft int 53 | .Fn ffsll "long long value" 54 | .Ft int 55 | .Fn fls "int value" 56 | .Ft int 57 | .Fn flsl "long value" 58 | .Ft int 59 | .Fn flsll "long long value" 60 | .Sh DESCRIPTION 61 | The 62 | .Fn ffs , 63 | .Fn ffsl 64 | and 65 | .Fn ffsll 66 | functions find the first (least significant) bit set 67 | in 68 | .Fa value 69 | and return the index of that bit. 70 | .Pp 71 | The 72 | .Fn fls , 73 | .Fn flsl 74 | and 75 | .Fn flsll 76 | functions find the last (most significant) bit set in 77 | .Fa value 78 | and return the index of that bit. 79 | .Pp 80 | Bits are numbered starting at 1, the least significant bit. 81 | A return value of zero from any of these functions means that the 82 | argument was zero. 83 | .Sh SEE ALSO 84 | .Xr bitstring 3 85 | .Sh HISTORY 86 | The 87 | .Fn ffs 88 | function appeared in 89 | .Bx 4.3 . 90 | Its prototype existed previously in 91 | .In string.h 92 | before it was moved to 93 | .In strings.h 94 | for 95 | .St -p1003.1-2001 96 | compliance. 97 | .Pp 98 | The 99 | .Fn ffsl , 100 | .Fn fls 101 | and 102 | .Fn flsl 103 | functions appeared in 104 | .Fx 5.3 . 105 | The 106 | .Fn ffsll 107 | and 108 | .Fn flsll 109 | functions appeared in 110 | .Fx 7.1 . 111 | -------------------------------------------------------------------------------- /src/ucontext/x86_64/_ctx_start.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | /* 25 | * Copyright (c) 2001 Daniel Eischen 26 | * All rights reserved. 27 | * 28 | * Redistribution and use in source and binary forms, with or without 29 | * modification, are permitted provided that the following conditions 30 | * are met: 31 | * 1. Redistributions of source code must retain the above copyright 32 | * notice, this list of conditions and the following disclaimer. 33 | * 2. Neither the name of the author nor the names of its contributors 34 | * may be used to endorse or promote products derived from this software 35 | * without specific prior written permission. 36 | * 37 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 38 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 40 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 41 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 42 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 43 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 45 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 46 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 | * SUCH DAMAGE. 48 | */ 49 | 50 | #if defined(__x86_64__) 51 | 52 | #include 53 | 54 | /* 55 | * _ctx_start((void *func)(int arg1, ..., argn), 56 | * int arg1, ..., argn, ucontext_t *ucp) 57 | * 58 | * %rdi - func 59 | * %rsi - arg1 60 | * %rdx - arg2 61 | * %rcx - arg3 62 | * %r8 - arg4 63 | * %r9 - arg5 64 | * WRONG! 65 | * (8*(n-6))(%rsp) - argn 66 | * (8*(n + 1))(%rsp) - ucp, %rbp setup to point here (base of stack) 67 | */ 68 | TEXT 69 | .private_extern __ctx_start 70 | LABEL(__ctx_start) 71 | popq %rax /* accounted for in makecontext() */ 72 | /* makecontext will simulate 6 parameters at least */ 73 | /* Or it could just set these in the mcontext... */ 74 | popq %rdi 75 | popq %rsi 76 | popq %rdx 77 | popq %rcx 78 | popq %r8 79 | popq %r9 80 | 81 | callq *%rax /* call start function */ 82 | movq %r12, %rsp /* setup stack for completion routine; 83 | ucp is now at top of stack. r12 is calleee save */ 84 | movq (%rsp), %rdi 85 | CALL_EXTERN(__ctx_done) /* should never return */ 86 | int $5 /* trap */ 87 | 88 | #endif /* __x86_64__ */ 89 | -------------------------------------------------------------------------------- /include/setjmp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | #ifndef _BSD_SETJMP_H 24 | #define _BSD_SETJMP_H 25 | 26 | #include 27 | #include 28 | 29 | #if defined(__x86_64__) 30 | /* 31 | * _JBLEN is number of ints required to save the following: 32 | * rflags, rip, rbp, rsp, rbx, r12, r13, r14, r15... these are 8 bytes each 33 | * mxcsr, fp control word, sigmask... these are 4 bytes each 34 | * add 16 ints for future expansion needs... 35 | */ 36 | #define _JBLEN ((9 * 2) + 3 + 16) 37 | typedef int jmp_buf[_JBLEN]; 38 | typedef int sigjmp_buf[_JBLEN + 1]; 39 | 40 | #elif defined(__i386__) 41 | 42 | /* 43 | * _JBLEN is number of ints required to save the following: 44 | * eax, ebx, ecx, edx, edi, esi, ebp, esp, ss, eflags, eip, 45 | * cs, de, es, fs, gs == 16 ints 46 | * onstack, mask = 2 ints 47 | */ 48 | 49 | #define _JBLEN (18) 50 | typedef int jmp_buf[_JBLEN]; 51 | typedef int sigjmp_buf[_JBLEN + 1]; 52 | 53 | #elif defined(__arm__) && !defined(__ARM_ARCH_7K__) 54 | /* 55 | * _JBLEN is number of ints required to save the following: 56 | * r4-r8, r10, fp, sp, lr, sig == 10 register_t sized 57 | * s16-s31 == 16 register_t sized + 1 int for FSTMX 58 | * 1 extra int for future use 59 | */ 60 | #define _JBLEN (10 + 16 + 2) 61 | #define _JBLEN_MAX _JBLEN 62 | 63 | typedef int jmp_buf[_JBLEN]; 64 | typedef int sigjmp_buf[_JBLEN + 1]; 65 | 66 | #elif defined(__arm64__) || defined(__ARM_ARCH_7K__) 67 | /* 68 | * _JBLEN is the number of ints required to save the following: 69 | * r21-r29, sp, fp, lr == 12 registers, 8 bytes each. d8-d15 70 | * are another 8 registers, each 8 bytes long. (aapcs64 specifies 71 | * that only 64-bit versions of FP registers need to be saved). 72 | * Finally, two 8-byte fields for signal handling purposes. 73 | */ 74 | #define _JBLEN ((14 + 8 + 2) * 2) 75 | 76 | typedef int jmp_buf[_JBLEN]; 77 | typedef int sigjmp_buf[_JBLEN + 1]; 78 | 79 | #else 80 | # error Undefined platform for setjmp 81 | #endif 82 | 83 | __BEGIN_DECLS 84 | extern int setjmp(jmp_buf); 85 | extern void longjmp(jmp_buf, int) __dead2; 86 | 87 | #ifndef _ANSI_SOURCE 88 | int _setjmp(jmp_buf); 89 | void _longjmp(jmp_buf, int) __dead2; 90 | int sigsetjmp(sigjmp_buf, int); 91 | void siglongjmp(sigjmp_buf, int) __dead2; 92 | #endif /* _ANSI_SOURCE */ 93 | 94 | #if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)) 95 | void longjmperror(void); 96 | #endif /* neither ANSI nor POSIX */ 97 | __END_DECLS 98 | 99 | #endif /* _BSD_SETJMP_H */ 100 | -------------------------------------------------------------------------------- /src/setjmp/x86_64/_setjmp.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1999-2018 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | /* 24 | * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved 25 | * 26 | * HISTORY 27 | * 20-Apr-92 Bruce Martin (bmartin@next.com) 28 | * Created from M68K sources. 29 | */ 30 | 31 | /* 32 | * C library -- _setjmp, _longjmp 33 | * 34 | * _longjmp(a,v) 35 | * will generate a "return(v)" from 36 | * the last call to 37 | * _setjmp(a) 38 | * by restoring registers from the stack, 39 | * The previous signal state is NOT restored. 40 | * 41 | */ 42 | 43 | #include 44 | #include 45 | 46 | #define JB_RBX 0 47 | #define JB_RBP 8 48 | #define JB_RSP 16 49 | #define JB_R12 24 50 | #define JB_R13 32 51 | #define JB_R14 40 52 | #define JB_R15 48 53 | #define JB_RIP 56 54 | #define JB_RFLAGS 64 55 | #define JB_MXCSR 72 56 | #define JB_FPCONTROL 76 57 | #define JB_MASK 80 58 | 59 | LEAF(__setjmp, 0) 60 | // %rdi is a jmp_buf (struct sigcontext *) 61 | 62 | // now build sigcontext 63 | movq %rbx, JB_RBX(%rdi) 64 | movq %rbp, %rax 65 | _OS_PTR_MUNGE(%rax) 66 | movq %rax, JB_RBP(%rdi) 67 | movq %r12, JB_R12(%rdi) 68 | movq %r13, JB_R13(%rdi) 69 | movq %r14, JB_R14(%rdi) 70 | movq %r15, JB_R15(%rdi) 71 | 72 | // RIP is set to the frame return address value 73 | movq (%rsp), %rax 74 | _OS_PTR_MUNGE(%rax) 75 | movq %rax, JB_RIP(%rdi) 76 | // RSP is set to the frame return address plus 8 77 | leaq 8(%rsp), %rax 78 | _OS_PTR_MUNGE(%rax) 79 | movq %rax, JB_RSP(%rdi) 80 | 81 | // save fp control word 82 | fnstcw JB_FPCONTROL(%rdi) 83 | 84 | // save MXCSR 85 | stmxcsr JB_MXCSR(%rdi) 86 | 87 | // return 0 88 | xorl %eax, %eax 89 | ret 90 | UNWIND_EPILOGUE 91 | 92 | 93 | LEAF(__longjmp, 0) 94 | fninit // Clear all FP exceptions 95 | // %rdi is a jmp_buf (struct sigcontext *) 96 | // %esi is the return value 97 | testl %esi, %esi 98 | movl $1, %eax 99 | cmovnel %esi, %eax 100 | 101 | // general registers 102 | movq JB_RBX(%rdi), %rbx 103 | movq JB_RBP(%rdi), %rsi 104 | _OS_PTR_UNMUNGE(%rsi) 105 | movq %rsi, %rbp 106 | movq JB_RSP(%rdi), %rsi 107 | _OS_PTR_UNMUNGE(%rsi) 108 | movsbq (%rsi), %r12 // probe to detect absolutely corrupt stack pointers 109 | movq %rsi, %rsp 110 | movq JB_R12(%rdi), %r12 111 | movq JB_R13(%rdi), %r13 112 | movq JB_R14(%rdi), %r14 113 | movq JB_R15(%rdi), %r15 114 | movq JB_RIP(%rdi), %rsi 115 | _OS_PTR_UNMUNGE(%rsi) 116 | 117 | // restore FP control word 118 | fldcw JB_FPCONTROL(%rdi) 119 | 120 | // restore MXCSR 121 | ldmxcsr JB_MXCSR(%rdi) 122 | 123 | // Make sure DF is reset 124 | cld 125 | 126 | jmp *%rsi 127 | UNWIND_EPILOGUE 128 | -------------------------------------------------------------------------------- /src/setjmp/i386/_setjmp.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1999-2018 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | /* 24 | * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved 25 | * 26 | * HISTORY 27 | * 20-Apr-92 Bruce Martin (bmartin@next.com) 28 | * Created from M68K sources. 29 | */ 30 | 31 | /* 32 | * C library -- _setjmp, _longjmp 33 | * 34 | * _longjmp(a,v) 35 | * will generate a "return(v)" from 36 | * the last call to 37 | * _setjmp(a) 38 | * by restoring registers from the stack, 39 | * The previous signal state is NOT restored. 40 | * 41 | */ 42 | 43 | #include 44 | #include 45 | 46 | // The FP control word is actually two bytes, but there's no harm in 47 | // using four bytes for it and keeping the struct aligned. 48 | #define JB_FPCW 0 49 | #define JB_MASK 4 50 | #define JB_MXCSR 8 51 | #define JB_EBX 12 52 | #define JB_ONSTACK 16 53 | #define JB_EDX 20 54 | #define JB_EDI 24 55 | #define JB_ESI 28 56 | #define JB_EBP 32 57 | #define JB_ESP 36 58 | #define JB_SS 40 59 | #define JB_EFLAGS 44 60 | #define JB_EIP 48 61 | #define JB_CS 52 62 | #define JB_DS 56 63 | #define JB_ES 60 64 | #define JB_FS 64 65 | #define JB_GS 68 66 | 67 | LEAF(__setjmp, 0) 68 | movl 4(%esp), %ecx // jmp_buf (struct sigcontext *) 69 | 70 | // Build the jmp_buf 71 | fnstcw JB_FPCW(%ecx) // Save the FP control word 72 | stmxcsr JB_MXCSR(%ecx) // Save the MXCSR 73 | movl %ebx, JB_EBX(%ecx) 74 | movl %edi, JB_EDI(%ecx) 75 | movl %esi, JB_ESI(%ecx) 76 | movl %ebp, %eax 77 | _OS_PTR_MUNGE(%eax) 78 | movl %eax, JB_EBP(%ecx) 79 | 80 | // EIP is set to the frame return address value 81 | movl (%esp), %eax 82 | _OS_PTR_MUNGE(%eax) 83 | movl %eax, JB_EIP(%ecx) 84 | // ESP is set to the frame return address plus 4 85 | leal 4(%esp), %eax 86 | _OS_PTR_MUNGE(%eax) 87 | movl %eax, JB_ESP(%ecx) 88 | 89 | // return 0 90 | xorl %eax, %eax 91 | ret 92 | 93 | 94 | LEAF(__longjmp, 0) 95 | fninit // Clear all FP exceptions 96 | movl 4(%esp), %ecx // jmp_buf (struct sigcontext *) 97 | movl 8(%esp), %edx // return value 98 | xorl %eax, %eax 99 | incl %eax 100 | testl %edx, %edx 101 | cmovnel %edx, %eax 102 | 103 | // general registers 104 | movl JB_EBX(%ecx), %ebx 105 | movl JB_ESI(%ecx), %esi 106 | movl JB_EDI(%ecx), %edi 107 | movl JB_EBP(%ecx), %edx 108 | _OS_PTR_UNMUNGE(%edx) 109 | movl %edx, %ebp 110 | movl JB_ESP(%ecx), %edx 111 | _OS_PTR_UNMUNGE(%edx) 112 | movl %edx, %esp 113 | movl JB_EIP(%ecx), %edx 114 | _OS_PTR_UNMUNGE(%edx) 115 | 116 | fldcw JB_FPCW(%ecx) // Restore FP control word 117 | ldmxcsr JB_MXCSR(%ecx) // Restore the MXCSR 118 | 119 | cld // Make sure DF is reset 120 | jmp *%edx 121 | -------------------------------------------------------------------------------- /man/ucontext.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2002 Packet Design, LLC. 2 | .\" All rights reserved. 3 | .\" 4 | .\" Subject to the following obligations and disclaimer of warranty, 5 | .\" use and redistribution of this software, in source or object code 6 | .\" forms, with or without modifications are expressly permitted by 7 | .\" Packet Design; provided, however, that: 8 | .\" 9 | .\" (i) Any and all reproductions of the source or object code 10 | .\" must include the copyright notice above and the following 11 | .\" disclaimer of warranties; and 12 | .\" (ii) No rights are granted, in any manner or form, to use 13 | .\" Packet Design trademarks, including the mark "PACKET DESIGN" 14 | .\" on advertising, endorsements, or otherwise except as such 15 | .\" appears in the above copyright notice or in the software. 16 | .\" 17 | .\" THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND 18 | .\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO 19 | .\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING 20 | .\" THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED 21 | .\" WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, 22 | .\" OR NON-INFRINGEMENT. PACKET DESIGN DOES NOT WARRANT, GUARANTEE, 23 | .\" OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS 24 | .\" OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, 25 | .\" RELIABILITY OR OTHERWISE. IN NO EVENT SHALL PACKET DESIGN BE 26 | .\" LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE 27 | .\" OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT, 28 | .\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL 29 | .\" DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF 30 | .\" USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF 31 | .\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 33 | .\" THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF 34 | .\" THE POSSIBILITY OF SUCH DAMAGE. 35 | .\" 36 | .\" $FreeBSD: src/lib/libc/gen/ucontext.3,v 1.3 2004/07/03 22:30:08 ru Exp $ 37 | .\" 38 | .Dd September 10, 2002 39 | .Dt UCONTEXT 3 40 | .Os 41 | .Sh NAME 42 | .Nm ucontext 43 | .Nd user thread context 44 | .Sh LIBRARY 45 | .Lb libc 46 | .Sh SYNOPSIS 47 | .In ucontext.h 48 | .Sh DESCRIPTION 49 | The 50 | .Vt ucontext_t 51 | type is a structure type suitable for holding the context for a user 52 | thread of execution. 53 | A thread's context includes its stack, saved registers, and list of 54 | blocked signals. 55 | .Pp 56 | The 57 | .Vt ucontext_t 58 | structure contains at least these fields: 59 | .Pp 60 | .Bl -tag -width ".Va mcontext_t\ \ uc_mcontext" -offset 3n -compact 61 | .It Va "ucontext_t *uc_link" 62 | context to assume when this one returns 63 | .It Va "sigset_t uc_sigmask" 64 | signals being blocked 65 | .It Va "stack_t uc_stack" 66 | stack area 67 | .It Va "mcontext_t uc_mcontext" 68 | saved registers 69 | .El 70 | .Pp 71 | The 72 | .Va uc_link 73 | field points to the context to resume when this context's entry point 74 | function returns. 75 | If 76 | .Va uc_link 77 | is equal to 78 | .Dv NULL , 79 | then the process exits when this context returns. 80 | .Pp 81 | The 82 | .Va uc_mcontext 83 | field is machine-dependent and should be treated as opaque by 84 | portable applications. 85 | .Pp 86 | The following functions are defined to manipulate 87 | .Vt ucontext_t 88 | structures: 89 | .Pp 90 | .Bl -item -offset 3n -compact 91 | .It 92 | .Ft int 93 | .Fn getcontext "ucontext_t *" ; 94 | .It 95 | .Ft int 96 | .Fn setcontext "const ucontext_t *" ; 97 | .It 98 | .Ft void 99 | .Fn makecontext "ucontext_t *" "void \*[lp]*\*[rp]\*[lp]void\*[rp]" int ... ; 100 | .It 101 | .Ft int 102 | .Fn swapcontext "ucontext_t *" "const ucontext_t *" ; 103 | .El 104 | .Sh SEE ALSO 105 | .Xr sigaltstack 2 , 106 | .Xr getcontext 3 , 107 | .Xr makecontext 3 108 | -------------------------------------------------------------------------------- /man/makecontext.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2002 Packet Design, LLC. 2 | .\" All rights reserved. 3 | .\" 4 | .\" Subject to the following obligations and disclaimer of warranty, 5 | .\" use and redistribution of this software, in source or object code 6 | .\" forms, with or without modifications are expressly permitted by 7 | .\" Packet Design; provided, however, that: 8 | .\" 9 | .\" (i) Any and all reproductions of the source or object code 10 | .\" must include the copyright notice above and the following 11 | .\" disclaimer of warranties; and 12 | .\" (ii) No rights are granted, in any manner or form, to use 13 | .\" Packet Design trademarks, including the mark "PACKET DESIGN" 14 | .\" on advertising, endorsements, or otherwise except as such 15 | .\" appears in the above copyright notice or in the software. 16 | .\" 17 | .\" THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND 18 | .\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO 19 | .\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING 20 | .\" THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED 21 | .\" WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, 22 | .\" OR NON-INFRINGEMENT. PACKET DESIGN DOES NOT WARRANT, GUARANTEE, 23 | .\" OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS 24 | .\" OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, 25 | .\" RELIABILITY OR OTHERWISE. IN NO EVENT SHALL PACKET DESIGN BE 26 | .\" LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE 27 | .\" OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT, 28 | .\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL 29 | .\" DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF 30 | .\" USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF 31 | .\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 33 | .\" THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF 34 | .\" THE POSSIBILITY OF SUCH DAMAGE. 35 | .\" 36 | .\" $FreeBSD: src/lib/libc/gen/makecontext.3,v 1.4 2002/12/19 09:40:21 ru Exp $ 37 | .\" 38 | .Dd September 10, 2002 39 | .Dt MAKECONTEXT 3 40 | .Os 41 | .Sh NAME 42 | .Nm makecontext , swapcontext 43 | .Nd modify and exchange user thread contexts 44 | .Sh LIBRARY 45 | .Lb libc 46 | .Sh SYNOPSIS 47 | .In ucontext.h 48 | .Ft void 49 | .Fo makecontext 50 | .Fa "ucontext_t *ucp" 51 | .Fa "void \*[lp]*func\*[rp]\*[lp]\*[rp]" 52 | .Fa "int argc" ... 53 | .Fc 54 | .Ft int 55 | .Fn swapcontext "ucontext_t *oucp" "const ucontext_t *ucp" 56 | .Sh DESCRIPTION 57 | The 58 | .Fn makecontext 59 | function 60 | modifies the user thread context pointed to by 61 | .Fa ucp , 62 | which must have previously been initialized by a call to 63 | .Xr getcontext 3 64 | and had a stack allocated for it. 65 | The context is modified so that it will continue execution by invoking 66 | .Fn func 67 | with the arguments (of type 68 | .Ft int ) 69 | provided. 70 | The 71 | .Fa argc 72 | argument 73 | must be equal to the number of additional arguments provided to 74 | .Fn makecontext 75 | and also equal to the number of arguments to 76 | .Fn func , 77 | or else the behavior is undefined. 78 | .Pp 79 | The 80 | .Fa "ucp->uc_link" 81 | argument 82 | must be initialized before calling 83 | .Fn makecontext 84 | and determines the action to take when 85 | .Fn func 86 | returns: 87 | if equal to 88 | .Dv NULL , 89 | the process exits; 90 | otherwise, 91 | .Fn setcontext "ucp->uc_link" 92 | is implicitly invoked. 93 | .Pp 94 | The 95 | .Fn swapcontext 96 | function 97 | saves the current thread context in 98 | .Fa "*oucp" 99 | and makes 100 | .Fa "*ucp" 101 | the currently active context. 102 | .Sh RETURN VALUES 103 | If successful, 104 | .Fn swapcontext 105 | returns zero; 106 | otherwise \-1 is returned and the global variable 107 | .Va errno 108 | is set appropriately. 109 | .Sh ERRORS 110 | The 111 | .Fn swapcontext 112 | function 113 | will fail if: 114 | .Bl -tag -width Er 115 | .It Bq Er ENOMEM 116 | There is not enough stack space in 117 | .Fa ucp 118 | to complete the operation. 119 | .El 120 | .Sh SEE ALSO 121 | .Xr setcontext 3 , 122 | .Xr ucontext 3 123 | -------------------------------------------------------------------------------- /src/string/generic/strlen.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2009 Xin LI 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | __FBSDID("$FreeBSD: src/lib/libc/string/strlen.c,v 1.7 2009/01/26 07:31:28 delphij Exp $"); 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #if !_PLATFORM_OPTIMIZED_STRLEN 35 | 36 | /* 37 | * Portable strlen() for 32-bit and 64-bit systems. 38 | * 39 | * Rationale: it is generally much more efficient to do word length 40 | * operations and avoid branches on modern computer systems, as 41 | * compared to byte-length operations with a lot of branches. 42 | * 43 | * The expression: 44 | * 45 | * ((x - 0x01....01) & ~x & 0x80....80) 46 | * 47 | * would evaluate to a non-zero value iff any of the bytes in the 48 | * original word is zero. However, we can further reduce ~1/3 of 49 | * time if we consider that strlen() usually operate on 7-bit ASCII 50 | * by employing the following expression, which allows false positive 51 | * when high bit of 1 and use the tail case to catch these case: 52 | * 53 | * ((x - 0x01....01) & 0x80....80) 54 | * 55 | * This is more than 5.2 times as fast as the raw implementation on 56 | * Intel T7300 under long mode for strings longer than word length. 57 | */ 58 | 59 | /* Magic numbers for the algorithm */ 60 | #if LONG_BIT == 32 61 | static const unsigned long mask01 = 0x01010101; 62 | static const unsigned long mask80 = 0x80808080; 63 | #elif LONG_BIT == 64 64 | static const unsigned long mask01 = 0x0101010101010101; 65 | static const unsigned long mask80 = 0x8080808080808080; 66 | #else 67 | #error Unsupported word size 68 | #endif 69 | 70 | #define LONGPTR_MASK (sizeof(long) - 1) 71 | 72 | /* 73 | * Helper macro to return string length if we caught the zero 74 | * byte. 75 | */ 76 | #define testbyte(x) \ 77 | do { \ 78 | if (p[x] == '\0') \ 79 | return (p - str + x); \ 80 | } while (0) 81 | 82 | size_t 83 | _platform_strlen(const char *str) 84 | { 85 | const char *p; 86 | const unsigned long *lp; 87 | 88 | /* Skip the first few bytes until we have an aligned p */ 89 | for (p = str; (uintptr_t)p & LONGPTR_MASK; p++) 90 | if (*p == '\0') 91 | return (p - str); 92 | 93 | /* Scan the rest of the string using word sized operation */ 94 | for (lp = (const unsigned long *)p; ; lp++) 95 | if ((*lp - mask01) & mask80) { 96 | p = (const char *)(lp); 97 | testbyte(0); 98 | testbyte(1); 99 | testbyte(2); 100 | testbyte(3); 101 | #if (LONG_BIT >= 64) 102 | testbyte(4); 103 | testbyte(5); 104 | testbyte(6); 105 | testbyte(7); 106 | #endif 107 | } 108 | 109 | /* NOTREACHED */ 110 | return (0); 111 | } 112 | 113 | #if VARIANT_STATIC 114 | size_t 115 | strlen(const char *str) 116 | { 117 | return _platform_strlen(str); 118 | } 119 | #endif 120 | 121 | #endif 122 | 123 | -------------------------------------------------------------------------------- /man/getcontext.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2002 Packet Design, LLC. 2 | .\" All rights reserved. 3 | .\" 4 | .\" Subject to the following obligations and disclaimer of warranty, 5 | .\" use and redistribution of this software, in source or object code 6 | .\" forms, with or without modifications are expressly permitted by 7 | .\" Packet Design; provided, however, that: 8 | .\" 9 | .\" (i) Any and all reproductions of the source or object code 10 | .\" must include the copyright notice above and the following 11 | .\" disclaimer of warranties; and 12 | .\" (ii) No rights are granted, in any manner or form, to use 13 | .\" Packet Design trademarks, including the mark "PACKET DESIGN" 14 | .\" on advertising, endorsements, or otherwise except as such 15 | .\" appears in the above copyright notice or in the software. 16 | .\" 17 | .\" THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND 18 | .\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO 19 | .\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING 20 | .\" THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED 21 | .\" WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, 22 | .\" OR NON-INFRINGEMENT. PACKET DESIGN DOES NOT WARRANT, GUARANTEE, 23 | .\" OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS 24 | .\" OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, 25 | .\" RELIABILITY OR OTHERWISE. IN NO EVENT SHALL PACKET DESIGN BE 26 | .\" LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE 27 | .\" OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT, 28 | .\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL 29 | .\" DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF 30 | .\" USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF 31 | .\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 33 | .\" THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF 34 | .\" THE POSSIBILITY OF SUCH DAMAGE. 35 | .\" 36 | .\" $FreeBSD: src/lib/libc/gen/getcontext.3,v 1.3 2004/12/03 14:10:04 rse Exp $ 37 | .\" 38 | .Dd September 10, 2002 39 | .Dt GETCONTEXT 3 40 | .Os 41 | .Sh NAME 42 | .Nm getcontext , setcontext 43 | .Nd get and set user thread context 44 | .Sh LIBRARY 45 | .Lb libc 46 | .Sh SYNOPSIS 47 | .In ucontext.h 48 | .Ft int 49 | .Fn getcontext "ucontext_t *ucp" 50 | .Ft int 51 | .Fn setcontext "const ucontext_t *ucp" 52 | .Sh DESCRIPTION 53 | The 54 | .Fn getcontext 55 | function 56 | saves the current thread's execution context in the structure pointed to by 57 | .Fa ucp . 58 | This saved context may then later be restored by calling 59 | .Fn setcontext . 60 | .Pp 61 | The 62 | .Fn setcontext 63 | function 64 | makes a previously saved thread context the current thread context, i.e., 65 | the current context is lost and 66 | .Fn setcontext 67 | does not return. 68 | Instead, execution continues in the context specified by 69 | .Fa ucp , 70 | which must have been previously initialized by a call to 71 | .Fn getcontext , 72 | .Xr makecontext 3 , 73 | or by being passed as an argument to a signal handler (see 74 | .Xr sigaction 2 ) . 75 | .Pp 76 | If 77 | .Fa ucp 78 | was initialized by 79 | .Fn getcontext , 80 | then execution continues as if the original 81 | .Fn getcontext 82 | call had just returned (again). 83 | .Pp 84 | If 85 | .Fa ucp 86 | was initialized by 87 | .Xr makecontext 3 , 88 | execution continues with the invocation of the function specified to 89 | .Xr makecontext 3 . 90 | When that function returns, 91 | .Fa "ucp->uc_link" 92 | determines what happens next: 93 | if 94 | .Fa "ucp->uc_link" 95 | is 96 | .Dv NULL , 97 | the process exits; 98 | otherwise, 99 | .Fn setcontext "ucp->uc_link" 100 | is implicitly invoked. 101 | .Pp 102 | If 103 | .Fa ucp 104 | was initialized by the invocation of a signal handler, execution continues 105 | at the point the thread was interrupted by the signal. 106 | .Sh RETURN VALUES 107 | If successful, 108 | .Fn getcontext 109 | returns zero and 110 | .Fn setcontext 111 | does not return; otherwise \-1 is returned. 112 | .Sh ERRORS 113 | No errors are defined for 114 | .Fn getcontext 115 | or 116 | .Fn setcontext . 117 | .Sh SEE ALSO 118 | .Xr sigaction 2 , 119 | .Xr sigaltstack 2 , 120 | .Xr makecontext 3 , 121 | .Xr ucontext 3 122 | -------------------------------------------------------------------------------- /src/timingsafe/arm64/timingsafe.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | #include "timingsafe.h" 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #define REG_DIT "dit" 33 | #define REG_SB "sb" 34 | #define DIT_ON (1) 35 | #define DIT_OFF (0) 36 | #define DIT_REG_MASK (1U << 24) 37 | #define BARRIER_SY (0xf) 38 | #define COMMPAGE_FEATURES \ 39 | ((volatile uint64_t const *const)_COMM_PAGE_CPU_CAPABILITIES64) 40 | #define TEST_FEATURES(FEATURES) ((*(COMMPAGE_FEATURES)) & (FEATURES)) 41 | 42 | // Use commpage for features that aren't mandatory for the current 43 | // architecture. 44 | #if defined(__ARM_ARCH_8_5__) 45 | #define NEED_FEATURES (0) 46 | #define HAS_FEATURES (kHasFeatDIT | kHasFeatSB) 47 | #elif defined(__ARM_ARCH_8_4__) 48 | #define NEED_FEATURES (kHasFeatSB) 49 | #define HAS_FEATURES (kHasFeatDIT) 50 | #else 51 | #define HAS_FEATURES (0) 52 | #define NEED_FEATURES (kHasFeatSB | kHasFeatDIT) 53 | #endif 54 | 55 | #if NEED_FEATURES 56 | #define CPU_FEATURES ((HAS_FEATURES) | TEST_FEATURES(NEED_FEATURES)) 57 | #else 58 | #define CPU_FEATURES (HAS_FEATURES) 59 | #endif 60 | 61 | /** 62 | Definition of supported CPU features. 63 | 64 | timingsafe_token_t is an opaque wrapper around this type, so this type must remain compatible. 65 | */ 66 | OS_OPTIONS(timingsafe_features, uint64_t, 67 | /** 68 | No timingsafe features supported. 69 | */ 70 | TIMINGSAFE_FEATURE_NONE = 0, 71 | 72 | /** 73 | Data Independent Timing feature. 74 | More information on this feature is available at the following link: 75 | https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations 76 | */ 77 | TIMINGSAFE_FEATURE_DIT = 1, 78 | ); 79 | 80 | /** 81 | CPU capabilities from commpage. 82 | */ 83 | typedef uint64_t cpu_cap_t; 84 | 85 | __attribute__((target(REG_DIT))) static inline bool is_dit_enabled(void) { 86 | return 0 != (DIT_REG_MASK & __builtin_arm_rsr64(REG_DIT)); 87 | } 88 | 89 | __attribute__((target(REG_SB))) static inline void sb(void) { 90 | asm volatile(REG_SB::: "memory"); 91 | } 92 | 93 | static inline void speculation_barrier(cpu_cap_t feat) { 94 | if (feat & kHasFeatSB) { 95 | sb(); 96 | } else { 97 | __builtin_arm_dsb(BARRIER_SY); 98 | __builtin_arm_isb(BARRIER_SY); 99 | } 100 | } 101 | 102 | __attribute__((target(REG_DIT))) timingsafe_token_t 103 | timingsafe_enable_if_supported(void) { 104 | timingsafe_features_t token = TIMINGSAFE_FEATURE_NONE; 105 | cpu_cap_t const feat = CPU_FEATURES; 106 | if (feat & kHasFeatDIT) { 107 | if (is_dit_enabled()) { 108 | token |= TIMINGSAFE_FEATURE_DIT; 109 | } 110 | __builtin_arm_wsr64(REG_DIT, DIT_ON); 111 | } 112 | speculation_barrier(feat); 113 | return (timingsafe_token_t)token; 114 | } 115 | 116 | __attribute__((target(REG_DIT))) void 117 | timingsafe_restore_if_supported(timingsafe_token_t token) { 118 | timingsafe_features_t const enum_token = (timingsafe_features_t)token; 119 | cpu_cap_t const feat = CPU_FEATURES; 120 | if ((feat & kHasFeatDIT) && !(enum_token & TIMINGSAFE_FEATURE_DIT)) { 121 | // Disable DIT if it was previously disabled 122 | __builtin_arm_wsr64(REG_DIT, DIT_OFF); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/setjmp/i386/setjmp.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | /* 24 | * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved 25 | */ 26 | /* 27 | * NeXT 386 setjmp/longjmp 28 | * 29 | * Written by Bruce Martin, NeXT Inc. 4/9/92 30 | */ 31 | 32 | /* 33 | * C library -- setjmp, longjmp 34 | * 35 | * longjmp(a,v) 36 | * will generate a "return(v)" from 37 | * the last call to 38 | * setjmp(a) 39 | * by restoring registers from the stack, 40 | * The previous value of the signal mask is 41 | * restored. 42 | * 43 | */ 44 | 45 | #include 46 | 47 | // The FP control word is actually two bytes, but there's no harm in 48 | // using four bytes for it and keeping the struct aligned. 49 | #define JB_FPCW 0 50 | #define JB_MASK 4 51 | #define JB_MXCSR 8 52 | #define JB_EBX 12 53 | #define JB_ONSTACK 16 54 | #define JB_EDX 20 55 | #define JB_EDI 24 56 | #define JB_ESI 28 57 | #define JB_EBP 32 58 | #define JB_ESP 36 59 | #define JB_SS 40 60 | #define JB_EFLAGS 44 61 | #define JB_EIP 48 62 | #define JB_CS 52 63 | #define JB_DS 56 64 | #define JB_ES 60 65 | #define JB_FS 64 66 | #define JB_GS 68 67 | #define JB_SAVEMASK 72 // sigsetjmp/siglongjmp only 68 | 69 | LEAF(_sigsetjmp, 0) 70 | movl 4(%esp), %eax // sigjmp_buf * jmpbuf; 71 | movl 8(%esp), %ecx // int savemask; 72 | movl %ecx, JB_SAVEMASK(%eax) // jmpbuf[_JBLEN] = savemask; 73 | cmpl $0, %ecx // if savemask != 0 74 | jne _setjmp // setjmp(jmpbuf); 75 | jmp L_do__setjmp // else _setjmp(jmpbuf); 76 | 77 | LEAF(_setjmp, 0) 78 | subl $16, %esp // make space for return from sigprocmask 79 | // + 12 to align stack 80 | pushl %esp // oset 81 | pushl $0 // set = NULL 82 | pushl $1 // how = SIG_BLOCK 83 | CALL_EXTERN(_sigprocmask) 84 | movl 12(%esp),%eax // save the mask 85 | addl $28, %esp // restore original esp 86 | movl 4(%esp), %ecx // jmp_buf (struct sigcontext *) 87 | movl %eax, JB_MASK(%ecx) 88 | 89 | subl $20, %esp // temporary struct sigaltstack + 8 to 90 | // align stack 91 | pushl %esp // oss 92 | pushl $0 // ss == NULL 93 | CALL_EXTERN(___sigaltstack) // get alternate signal stack info 94 | movl 16(%esp), %eax // oss->ss_flags 95 | addl $28, %esp // Restore %esp 96 | movl %eax, JB_ONSTACK(%ecx) 97 | 98 | L_do__setjmp: 99 | BRANCH_EXTERN(__setjmp) 100 | 101 | LEAF(_siglongjmp, 0) 102 | movl 4(%esp), %eax // sigjmp_buf * jmpbuf; 103 | cmpl $0, JB_SAVEMASK(%eax) // if jmpbuf[_JBLEN] != 0 104 | jne _longjmp // longjmp(jmpbuf, var); 105 | jmp L_do__longjmp // else _longjmp(jmpbuf, var); 106 | 107 | LEAF(_longjmp, 0) 108 | movl 4(%esp), %ecx // address of jmp_buf (saved context) 109 | movl JB_MASK(%ecx),%eax // get the mask 110 | subl $12, %esp // Make sure the stack is 16-byte 111 | // aligned when we call sigprocmask 112 | pushl %eax // store the mask 113 | movl %esp, %edx // save the address where we stored the mask 114 | pushl $0 // oset = NULL 115 | pushl %edx // set 116 | pushl $3 // how = SIG_SETMASK 117 | CALL_EXTERN_AGAIN(_sigprocmask) 118 | addl $28, %esp // restore original esp 119 | 120 | movl 4(%esp), %ecx // address of jmp_buf 121 | movl JB_ONSTACK(%ecx), %eax // ss_flags 122 | subl $8, %esp 123 | pushl %eax 124 | CALL_EXTERN(__sigunaltstack) 125 | addl $12, %esp 126 | 127 | L_do__longjmp: 128 | BRANCH_EXTERN(__longjmp) // else 129 | END(_longjmp) 130 | -------------------------------------------------------------------------------- /src/ucontext/generic/swapcontext.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007, 2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | /* 25 | * Copyright (c) 2001 Daniel M. Eischen 26 | * All rights reserved. 27 | * 28 | * Redistribution and use in source and binary forms, with or without 29 | * modification, are permitted provided that the following conditions 30 | * are met: 31 | * 1. Redistributions of source code must retain the above copyright 32 | * notice, this list of conditions and the following disclaimer. 33 | * 2. Neither the name of the author nor the names of its contributors 34 | * may be used to endorse or promote products derived from this software 35 | * without specific prior written permission. 36 | * 37 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 38 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 40 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 41 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 42 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 43 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 45 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 46 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 | * SUCH DAMAGE. 48 | */ 49 | 50 | #define _XOPEN_SOURCE 600L 51 | #include 52 | #include 53 | #include 54 | 55 | /* This is a macro to capture all the code added in here that is purely to make 56 | * conformance tests pass and seems to have no functional reason nor is it 57 | * required by the standard */ 58 | #define CONFORMANCE_SPECIFIC_HACK 1 59 | 60 | #if ((TARGET_OS_OSX || TARGET_OS_DRIVERKIT) && (defined(__LP64__) || defined(__i386__))) 61 | 62 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" 63 | 64 | #include 65 | #include 66 | #include 67 | 68 | #define uc_flags uc_onstack 69 | #define UCF_SWAPPED 0x80000000 70 | 71 | int 72 | swapcontext(ucontext_t *oucp, const ucontext_t *ucp) 73 | { 74 | int ret; 75 | if ((oucp == NULL) || (ucp == NULL)) { 76 | errno = EINVAL; 77 | return -1; 78 | } 79 | 80 | oucp->uc_flags &= ~UCF_SWAPPED; 81 | 82 | #if CONFORMANCE_SPECIFIC_HACK 83 | // getcontext overwrites uc_link so we save it and restore it 84 | ucontext_t *next_context = oucp->uc_link; 85 | ret = getcontext(oucp); 86 | oucp->uc_link = next_context; 87 | #endif 88 | 89 | if ((ret == 0) && !(oucp->uc_flags & UCF_SWAPPED)) { 90 | oucp->uc_flags |= UCF_SWAPPED; 91 | /* In the future, when someone calls setcontext(oucp), that will return 92 | * us to the getcontext call above with ret = 0. However, because we 93 | * just flipped the UCF_SWAPPED bit, we will not call setcontext again 94 | * and will return. */ 95 | ret = setcontext(ucp); 96 | } 97 | 98 | asm(""); // Prevent tailcall 99 | return (ret); 100 | } 101 | 102 | #else /* TARGET_OS_OSX || TARGET_OS_DRIVERKIT */ 103 | 104 | int 105 | swapcontext(ucontext_t *oucp, const ucontext_t *ucp) 106 | { 107 | errno = ENOTSUP; 108 | return -1; 109 | } 110 | 111 | #endif /* TARGET_OS_OSX || TARGET_OS_DRIVERKIT */ 112 | -------------------------------------------------------------------------------- /src/cachecontrol/arm64/cache.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011-2017 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * This file contains Original Code and/or Modifications of Original Code 7 | * as defined in and that are subject to the Apple Public Source License 8 | * Version 2.0 (the 'License'). You may not use this file except in 9 | * compliance with the License. Please obtain a copy of the License at 10 | * http://www.opensource.apple.com/apsl/ and read it before using this 11 | * file. 12 | * 13 | * The Original Code and all software distributed under the License are 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 | * Please see the License for the specific language governing rights and 19 | * limitations under the License. 20 | * 21 | * @APPLE_LICENSE_HEADER_END@ 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #define MMU_I_CLINE 6 // cache line size as 1< 0 ? 36 | 37 | and x9, x0, #~((1<> MMU_I_CLINE) + 1; 112 | asm volatile("dsb ish" ::: "memory"); 113 | while (count--) { 114 | asm("ic ivau, %[addr]" :: [addr] "r" (addr) : "memory"); 115 | addr += (1 << MMU_I_CLINE); 116 | } 117 | if (hasICDSB) { 118 | asm volatile("dsb ish" ::: "memory"); 119 | } else { 120 | asm volatile("dsb ish" ::: "memory"); 121 | asm volatile("isb" ::: "memory"); 122 | } 123 | } 124 | 125 | cbz x1, 0x44 126 | mov x8, #0xfffff0000 127 | movk x8, #0xc020 128 | ldr w8, [x8] 129 | and x9, x0, #0xffffffffffffffc0 130 | and x10, x0, #0x3f 131 | add x10, x1, x10 132 | sub x10, x10, #0x1 133 | mov x11, #-0x1 134 | eor x10, x11, x10, lsr #6 135 | ic ivau, x9 136 | add x9, x9, #0x40 137 | adds x10, x10, #0x1 138 | b.ne 0x28 139 | dsb ish 140 | tbnz w8, #0x2, 0x44 141 | isb 142 | ret 143 | 144 | #endif 145 | 146 | --------------------------------------------------------------------------------