Changeset 7bf29e5 in mainline for uspace/lib/c
- Timestamp:
- 2025-01-09T11:29:38Z (6 months ago)
- Children:
- a5c2960e
- Parents:
- bc3d695 (diff), 4e1221c (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- uspace/lib/c
- Files:
-
- 29 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/arch/abs32le/include/libarch/thread.h
rbc3d695 r7bf29e5 36 36 #define _LIBC_abs32le_THREAD_H_ 37 37 38 #include <align.h> 39 40 static inline uintptr_t arch_thread_prepare(void *stack, size_t stack_size, 41 void (*main)(void *), void *arg) 42 { 43 uintptr_t *sp = (uintptr_t *) ALIGN_DOWN((uintptr_t) stack + stack_size, 16); 44 45 sp[-1] = (uintptr_t) arg; 46 sp[-2] = (uintptr_t) main; 47 48 return (uintptr_t) sp; 49 } 50 38 51 #endif 39 52 -
uspace/lib/c/arch/abs32le/src/thread_entry.c
rbc3d695 r7bf29e5 30 30 */ 31 31 32 #include <stdbool.h> 32 33 #include <stddef.h> 33 34 #include "../../../generic/private/thread.h" … … 35 36 void __thread_entry(void) 36 37 { 37 __thread_main(NULL); 38 while (true) 39 ; 38 40 } 39 41 -
uspace/lib/c/arch/amd64/include/libarch/thread.h
rbc3d695 r7bf29e5 36 36 #define _LIBC_amd64_THREAD_H_ 37 37 38 #include <align.h> 39 #include <stddef.h> 40 #include <stdint.h> 41 42 static inline uintptr_t arch_thread_prepare(void *stack, size_t stack_size, 43 void (*main)(void *), void *arg) 44 { 45 uintptr_t *sp = (uintptr_t *) ALIGN_DOWN((uintptr_t) stack + stack_size, 16); 46 47 *--sp = (uintptr_t) arg; 48 *--sp = (uintptr_t) main; 49 50 return (uintptr_t) sp; 51 } 52 38 53 #endif 39 54 -
uspace/lib/c/arch/amd64/src/thread_entry.S
rbc3d695 r7bf29e5 35 35 # 36 36 SYMBOL_BEGIN(__thread_entry) 37 # Pop libc thread entry function and argument 38 popq %rax 39 popq %rdi 40 37 41 # 38 42 # Create the first stack frame. … … 42 46 movq %rsp, %rbp 43 47 44 # 45 # RAX contains address of uarg 46 # 47 movq %rax, %rdi 48 call FUNCTION_REF(__thread_main) 48 call *%rax 49 49 SYMBOL_END(__thread_entry) -
uspace/lib/c/arch/arm32/include/libarch/thread.h
rbc3d695 r7bf29e5 37 37 #define _LIBC_arm32_THREAD_H_ 38 38 39 #include <align.h> 40 41 static inline uintptr_t arch_thread_prepare(void *stack, size_t stack_size, 42 void (*main)(void *), void *arg) 43 { 44 uintptr_t *sp = (uintptr_t *) ALIGN_DOWN((uintptr_t) stack + stack_size, 16); 45 46 *--sp = (uintptr_t) main; 47 *--sp = (uintptr_t) arg; 48 49 return (uintptr_t) sp; 50 } 51 39 52 #endif 40 53 -
uspace/lib/c/arch/arm32/src/thread_entry.S
rbc3d695 r7bf29e5 35 35 # 36 36 SYMBOL(__thread_entry) 37 # Pop libc entry function and its argument. 38 pop { r0, r4 } 39 37 40 # 38 41 # Create the first stack frame. … … 43 46 sub fp, ip, #4 44 47 45 b __thread_main48 bx r4 -
uspace/lib/c/arch/arm64/include/libarch/thread.h
rbc3d695 r7bf29e5 36 36 #define _LIBC_arm64_THREAD_H_ 37 37 38 #include <align.h> 39 40 static inline uintptr_t arch_thread_prepare(void *stack, size_t stack_size, 41 void (*main)(void *), void *arg) 42 { 43 uintptr_t *sp = (uintptr_t *) ALIGN_DOWN((uintptr_t) stack + stack_size, 16); 44 45 *--sp = (uintptr_t) arg; 46 *--sp = (uintptr_t) main; 47 48 return (uintptr_t) sp; 49 } 50 38 51 #endif 39 52 -
uspace/lib/c/arch/arm64/src/thread_entry.S
rbc3d695 r7bf29e5 35 35 # 36 36 SYMBOL(__thread_entry) 37 # 37 # Load entry function and argument from stack. 38 ldp x1, x0, [sp], #16 39 38 40 # Create the first stack frame. 39 #40 41 mov x29, #0 41 42 stp x29, x30, [sp, #-16]! 42 43 mov x29, sp 43 44 44 b __thread_main45 br x1 -
uspace/lib/c/arch/ia32/include/libarch/thread.h
rbc3d695 r7bf29e5 36 36 #define _LIBC_ia32_THREAD_H_ 37 37 38 #include <align.h> 39 #include <stddef.h> 40 #include <stdint.h> 41 42 static inline uintptr_t arch_thread_prepare(void *stack, size_t stack_size, 43 void (*main)(void *), void *arg) 44 { 45 uintptr_t *sp = (uintptr_t *) ALIGN_DOWN((uintptr_t) stack + stack_size, 16); 46 47 *--sp = (uintptr_t) arg; 48 *--sp = (uintptr_t) main; 49 50 return (uintptr_t) sp; 51 } 52 38 53 #endif 39 54 -
uspace/lib/c/arch/ia32/src/syscall.S
rbc3d695 r7bf29e5 77 77 pushl %ebp 78 78 mov %esp, %ebp 79 lea ra, %edi80 79 movl 20(%esp), %edx # First argument. 81 80 movl 24(%esp), %ecx # Second argument. … … 83 82 movl 32(%esp), %esi # Fourth argument. 84 83 movl 44(%esp), %eax # Syscall number. 85 sysenter 86 ra: 84 call 1f 87 85 movw %cs, %cx 88 86 addw $8, %cx … … 95 93 popl %ebx 96 94 ret 95 96 /* Trampoline for entering kernel */ 97 1: 98 pop %edi 99 sysenter 97 100 FUNCTION_END(__syscall_fast) -
uspace/lib/c/arch/ia32/src/thread_entry.S
rbc3d695 r7bf29e5 41 41 # Do not set %gs, it contains descriptor that can see TLS 42 42 43 # Pop libc thread main function. 44 popl %eax 45 # Pop argument. 46 popl %ebx 47 43 48 # 44 49 # Create the first stack frame. … … 48 53 mov %esp, %ebp 49 54 50 # 51 # EAX contains address of uarg. 52 # 53 pushl %eax 54 call __thread_main 55 pushl %ebx 56 call *%eax 55 57 56 58 # -
uspace/lib/c/arch/ia64/include/libarch/thread.h
rbc3d695 r7bf29e5 36 36 #define _LIBC_ia64_THREAD_H_ 37 37 38 #include <align.h> 39 40 static inline uintptr_t arch_thread_prepare(void *stack, size_t stack_size, 41 void (*main)(void *), void *arg) 42 { 43 uintptr_t *sp = (uintptr_t *) (ALIGN_DOWN((uintptr_t) stack + stack_size / 2, 16)); 44 45 /* Store data under stack pointer */ 46 sp[-1] = (uintptr_t) arg; 47 sp[-2] = (uintptr_t) main; 48 49 return (uintptr_t) sp; 50 } 51 38 52 #endif 39 53 -
uspace/lib/c/arch/ia64/src/thread_entry.S
rbc3d695 r7bf29e5 37 37 alloc loc0 = ar.pfs, 0, 1, 1, 0 38 38 39 #ifndef CONFIG_RTLD 40 # XXX This does not work in a shared library 41 movl gp = __gp 42 #endif 39 add r8 = -8, sp ;; 40 # Entry function argument 41 ld8 out0 = [r8], -8 ;; 43 42 44 # 45 # r8 contains address of uarg structure. 46 # 43 # Entry function descriptor 44 ld8 r8 = [r8] ;; 45 # Entry function address 46 ld8 r9 = [r8], 8 ;; 47 # Entry function global pointer 48 ld8 gp = [r8] ;; 47 49 48 mov out0 = r8;;49 # XXX br.call.sptk.many b0 = FUNCTION_REF(__thread_main) 50 br.call.sptk.many b0 = __thread_main50 mov b1 = r9 ;; 51 52 br.call.sptk.many b0 = b1 ;; 51 53 52 54 # -
uspace/lib/c/arch/mips32/include/libarch/thread.h
rbc3d695 r7bf29e5 37 37 #define _LIBC_mips32_THREAD_H_ 38 38 39 #include <align.h> 40 41 static inline uintptr_t arch_thread_prepare(void *stack, size_t stack_size, 42 void (*main)(void *), void *arg) 43 { 44 uintptr_t *sp = (uintptr_t *) ALIGN_DOWN((uintptr_t) stack + stack_size, 16); 45 46 sp[-1] = (uintptr_t) arg; 47 sp[-2] = (uintptr_t) main; 48 49 return (uintptr_t) sp; 50 } 51 39 52 #endif 40 53 -
uspace/lib/c/arch/mips32/src/thread_entry.S
rbc3d695 r7bf29e5 40 40 # 41 41 SYMBOL(__thread_entry) 42 .ent __thread_entry 43 .frame $sp, ABI_STACK_FRAME, $ra 44 .cpload $t9 42 # All registers should be zero, including $fp and $ra. 43 # Instead of setting up a stack frame here, we leave it for __thread_main. 45 44 46 # 47 # v0 contains address of uarg.48 # 49 add $a0, $v0, 045 # Function argument. 46 lw $a0, -4($sp) 47 # Function pointer. 48 lw $t0, -8($sp) 50 49 51 # Allocate the stack frame. 52 addiu $sp, -ABI_STACK_FRAME 53 54 # Allow PIC code 55 .cprestore 16 56 57 jal __thread_main 50 j $t0 58 51 nop 59 60 #61 # Not reached.62 #63 addiu $sp, ABI_STACK_FRAME64 .end __thread_entry -
uspace/lib/c/arch/ppc32/include/libarch/thread.h
rbc3d695 r7bf29e5 36 36 #define _LIBC_ppc32_THREAD_H_ 37 37 38 #include <align.h> 39 40 static inline uintptr_t arch_thread_prepare(void *stack, size_t stack_size, 41 void (*main)(void *), void *arg) 42 { 43 uintptr_t *sp = (uintptr_t *) ALIGN_DOWN((uintptr_t) stack + stack_size - sizeof(void *), 16); 44 45 sp[0] = 0; 46 sp[-1] = (uintptr_t) arg; 47 sp[-2] = (uintptr_t) main; 48 49 return (uintptr_t) sp; 50 } 51 38 52 #endif 39 53 -
uspace/lib/c/arch/ppc32/src/thread_entry.S
rbc3d695 r7bf29e5 35 35 # 36 36 SYMBOL(__thread_entry) 37 # 38 # Create the first stack frame. 39 # 40 li %r4, 0 41 stw %r4, 0(%r1) 42 stwu %r1, -16(%r1) 37 # Load function and argument. 38 lwz %r3, -4(%r1) 39 lwz %r4, -8(%r1) 43 40 44 b __thread_main 41 # Clear LR 42 li %r0, 0 43 mtlr %r0 45 44 45 mtctr %r4 46 bctr -
uspace/lib/c/arch/riscv64/include/libarch/thread.h
rbc3d695 r7bf29e5 36 36 #define _LIBC_riscv64_THREAD_H_ 37 37 38 #include <align.h> 39 40 static inline uintptr_t arch_thread_prepare(void *stack, size_t stack_size, 41 void (*main)(void *), void *arg) 42 { 43 uintptr_t *sp = (uintptr_t *) ALIGN_DOWN((uintptr_t) stack + stack_size, 16); 44 45 sp[-1] = (uintptr_t) arg; 46 sp[-2] = (uintptr_t) main; 47 48 return (uintptr_t) sp; 49 } 50 38 51 #endif 39 52 -
uspace/lib/c/arch/riscv64/src/thread_entry.c
rbc3d695 r7bf29e5 35 35 void __thread_entry(void) 36 36 { 37 __thread_main((void *) 0); 37 // TODO 38 while (true) 39 ; 38 40 } 39 41 -
uspace/lib/c/arch/sparc64/include/libarch/thread.h
rbc3d695 r7bf29e5 35 35 #define _LIBC_sparc64_THREAD_H_ 36 36 37 #include <assert.h> 38 #include <align.h> 39 #include <libarch/stack.h> 40 41 static inline uintptr_t arch_thread_prepare(void *stack, size_t stack_size, 42 void (*main)(void *), void *arg) 43 { 44 /* We must leave space above the stack pointer for initial register spill area. */ 45 uintptr_t *sp = (uintptr_t *) ALIGN_DOWN((uintptr_t) stack + stack_size - STACK_WINDOW_SAVE_AREA_SIZE - STACK_ARG_SAVE_AREA_SIZE, 16); 46 47 sp[-1] = (uintptr_t) arg; 48 sp[-2] = (uintptr_t) main; 49 50 return ((uintptr_t) sp) - STACK_BIAS; 51 } 52 37 53 #endif 38 54 -
uspace/lib/c/arch/sparc64/src/thread_entry.S
rbc3d695 r7bf29e5 35 35 # 36 36 SYMBOL(__thread_entry) 37 add %sp, 0x7ff, %g1 38 37 39 # 38 40 # Create the first stack frame. … … 43 45 44 46 # 45 # Propagate the input arguments to the new window.47 # Load libc entry point address and argument from stack 46 48 # 49 ldn [%g1 - 8], %o0 50 ldn [%g1 - 16], %g1 47 51 48 call __thread_main ! %o0 contains address of uarg 49 mov %i0, %o0 52 jmpl %g1, %r0 53 # Wipe link register 54 xor %o7, %o7, %o7 50 55 51 56 ! not reached -
uspace/lib/c/generic/malloc.c
rbc3d695 r7bf29e5 38 38 #include <stdbool.h> 39 39 #include <stddef.h> 40 #include <stdio.h> 40 41 #include <as.h> 41 42 #include <align.h> … … 725 726 * 726 727 */ 727 static void *malloc_internal( const size_t size, constsize_t align)728 static void *malloc_internal(size_t size, size_t align) 728 729 { 729 730 malloc_assert(first_heap_area != NULL); 730 731 732 if (size == 0) 733 size = 1; 734 731 735 if (align == 0) 732 return NULL;736 align = BASE_ALIGN; 733 737 734 738 size_t falign = lcm(align, BASE_ALIGN); … … 821 825 * 822 826 */ 823 void *realloc(void *const addr, constsize_t size)827 void *realloc(void *const addr, size_t size) 824 828 { 825 829 if (size == 0) { 826 f ree(addr);827 return NULL;830 fprintf(stderr, "realloc() called with size 0\n"); 831 size = 1; 828 832 } 829 833 … … 930 934 } 931 935 936 /** Reallocate memory for an array 937 * 938 * Same as realloc(ptr, nelem * elsize), except the multiplication is checked 939 * for numerical overflow. Borrowed from POSIX 2024. 940 * 941 * @param ptr 942 * @param nelem 943 * @param elsize 944 * 945 * @return Reallocated memory or NULL. 946 */ 947 void *reallocarray(void *ptr, size_t nelem, size_t elsize) 948 { 949 if (nelem > SIZE_MAX / elsize) 950 return NULL; 951 952 return realloc(ptr, nelem * elsize); 953 } 954 932 955 /** Free a memory block 933 956 * -
uspace/lib/c/generic/private/fibril.h
rbc3d695 r7bf29e5 33 33 #include <context.h> 34 34 #include <tls.h> 35 #include <abi/proc/uarg.h>36 35 #include <fibril.h> 37 36 #include <ipc/common.h> … … 50 49 context_t ctx; 51 50 52 uspace_arg_t uarg;53 51 link_t link; 54 52 void *stack; -
uspace/lib/c/generic/private/thread.h
rbc3d695 r7bf29e5 37 37 38 38 #include <time.h> 39 #include <abi/proc/uarg.h>40 39 #include <libarch/thread.h> 41 40 #include <abi/proc/thread.h> 42 41 43 42 extern void __thread_entry(void); 44 extern void __thread_main(uspace_arg_t *);45 43 46 extern errno_t thread_create(void (*)(void *), void *, const char *, 47 thread_id_t *); 44 extern errno_t thread_create(errno_t (*)(void *), void *, const char *); 48 45 extern void thread_exit(int) __attribute__((noreturn)); 49 extern void thread_detach(thread_id_t);50 46 extern thread_id_t thread_get_id(void); 51 47 extern void thread_usleep(usec_t); -
uspace/lib/c/generic/thread/fibril.c
rbc3d695 r7bf29e5 781 781 } 782 782 783 static void_runner_fn(void *arg)783 static errno_t _runner_fn(void *arg) 784 784 { 785 785 _helper_fibril_fn(arg); 786 return EOK; 786 787 } 787 788 … … 808 809 809 810 for (int i = 0; i < n; i++) { 810 thread_id_t tid; 811 rc = thread_create(_runner_fn, NULL, "fibril runner", &tid); 811 rc = thread_create(_runner_fn, NULL, "fibril runner"); 812 812 if (rc != EOK) 813 813 return i; 814 thread_detach(tid);815 814 } 816 815 -
uspace/lib/c/generic/thread/thread.c
rbc3d695 r7bf29e5 37 37 #include <stdlib.h> 38 38 #include <libarch/faddr.h> 39 #include <abi/proc/uarg.h>40 39 #include <fibril.h> 41 40 #include <stack.h> … … 54 53 * and exit when thread returns back. 55 54 * 56 * @param uarg Pointer to userspace argument structure.55 * @param arg Fibril pointer. 57 56 * 58 57 */ 59 void __thread_main(uspace_arg_t *uarg)58 static void __thread_main(void *arg) 60 59 { 60 fibril_t *fibril = arg; 61 61 62 assert(!__tcb_is_set()); 62 63 fibril_t *fibril = uarg->uspace_thread_arg;64 63 assert(fibril); 65 64 66 65 __tcb_set(fibril->tcb); 67 66 68 uarg->uspace_thread_function(fibril->arg);67 fibril->func(fibril->arg); 69 68 /* 70 69 * XXX: we cannot free the userspace stack while running on it … … 90 89 * @return Zero on success or a code from @ref errno.h on failure. 91 90 */ 92 errno_t thread_create(void (*function)(void *), void *arg, const char *name, 93 thread_id_t *tid) 91 errno_t thread_create(errno_t (*func)(void *), void *arg, const char *name) 94 92 { 95 uspace_arg_t *uarg = calloc(1, sizeof(uspace_arg_t));96 if (! uarg)93 fibril_t *fibril = fibril_alloc(); 94 if (!fibril) 97 95 return ENOMEM; 98 96 99 fibril_t *fibril = fibril_alloc(); 100 if (!fibril) { 101 free(uarg); 102 return ENOMEM; 103 } 97 fibril->func = func; 98 fibril->arg = arg; 104 99 105 100 size_t stack_size = stack_size_get(); … … 109 104 if (stack == AS_MAP_FAILED) { 110 105 fibril_teardown(fibril); 111 free(uarg);112 106 return ENOMEM; 113 107 } 114 108 115 fibril->arg = arg; 116 uarg->uspace_entry = (void *) FADDR(__thread_entry); 117 uarg->uspace_stack = stack; 118 uarg->uspace_stack_size = stack_size; 119 uarg->uspace_thread_function = function; 120 uarg->uspace_thread_arg = fibril; 121 uarg->uspace_uarg = uarg; 109 uintptr_t sp = arch_thread_prepare(stack, stack_size, __thread_main, 110 fibril); 122 111 123 errno_t rc = (errno_t) __SYSCALL4(SYS_THREAD_CREATE, (sysarg_t) uarg, 124 (sysarg_t) name, (sysarg_t) str_size(name), (sysarg_t) tid); 112 errno_t rc = (errno_t) __SYSCALL4(SYS_THREAD_CREATE, 113 (sysarg_t) FADDR(__thread_entry), sp, 114 (sysarg_t) name, (sysarg_t) str_size(name)); 125 115 126 116 if (rc != EOK) { … … 130 120 */ 131 121 as_area_destroy(stack); 132 free(uarg);133 122 } 134 123 … … 148 137 while (true) 149 138 ; 150 }151 152 /** Detach thread.153 *154 * Currently not implemented.155 *156 * @param thread TID.157 */158 void thread_detach(thread_id_t thread)159 {160 139 } 161 140 -
uspace/lib/c/include/align.h
rbc3d695 r7bf29e5 41 41 * @param a Size of alignment, must be power of 2. 42 42 */ 43 #define ALIGN_DOWN(s, a) ((s) & ~(( a) - 1))43 #define ALIGN_DOWN(s, a) ((s) & ~((typeof(s))(a) - 1)) 44 44 45 45 /** Align to the nearest higher address which is a power of two. … … 48 48 * @param a Size of alignment, must be power of 2. 49 49 */ 50 #define ALIGN_UP(s, a) (( long)((s) + ((a) - 1)) & ~((long) (a) - 1))50 #define ALIGN_UP(s, a) ((((s) + ((a) - 1)) & ~((typeof(s))(a) - 1))) 51 51 52 52 /** Round up to the nearest higher boundary. -
uspace/lib/c/meson.build
rbc3d695 r7bf29e5 59 59 60 60 src += files( 61 'common/adt/bitmap.c', 61 62 'common/adt/checksum.c', 62 63 'common/adt/circ_buf.c', -
uspace/lib/c/test/double_to_str.c
rbc3d695 r7bf29e5 176 176 PCUT_ASSERT_INT_EQUALS(-3, dec); 177 177 PCUT_ASSERT_STR_EQUALS("1100", buf); 178 179 d = extract_ieee_double(768.0); 180 ret = double_to_fixed_str(d, -1, 3, buf, size, &dec); 181 PCUT_ASSERT_INT_EQUALS(4, ret); 182 PCUT_ASSERT_INT_EQUALS(-3, dec); 183 PCUT_ASSERT_STR_EQUALS("768", buf); 178 184 } 179 185
Note:
See TracChangeset
for help on using the changeset viewer.