From a6b5e947c18387d81c671f309e879c10f32dc412 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 15 Apr 2026 06:50:32 +0900 Subject: [PATCH] Signed integer types (`intx_t`, `size_t`, `ssize_t`), `memcpy`, `memmove`, `memcmp` and `memset` --- userland/init_service/init.bin | Bin 5256 -> 5188 bytes userland/init_service/server.c | 10 +---- userland/libOs/shared.h | 9 +++-- userland/libOs/string.h | 71 +++++++++++++++++++++++++++++++++ userland/libOs/syscalls.h | 2 + userland/libOs/types.h | 11 +++++ 6 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 userland/libOs/string.h diff --git a/userland/init_service/init.bin b/userland/init_service/init.bin index b7557efcff59801c959d88bd05ad0c81d9e8d294..1d54a3b60a1a71ab7559749fedc2cb2d8a2d76be 100755 GIT binary patch delta 766 zcmeCsJfacd7;bpLG0ZX4F(lNZ^RGv*Z5}@ZL$F8ln~WEM>AiqPz{gPOQoVf`E-c9GEkmhp5gO^*MES5dnZPya=zkYVDRac{lz!2Esn8r z;?opH`N>|4NsN0YuVu7j44nLi(SlK6vMQ52qr_xarc9=Pe3LgYnal5HW?*QjV07hg z-3Vqe`S7^rW@BE)xNq`W=5Ud%++e3A0wpIvEzX@R!6MH%XR-y04P))( zQWh=7rpXIfBp4S=-pQgVk;2Hpu)u)gGc8*epZjx1N0~FqUV?WT2dIp9Uy_4^=de%P%3Ic=B2B-+?%$E?CYav89xWH;85F)oY z!6Ix>k!}+ep!pGCjUL@ODjF}|fShogMFr%cm#qK(|M%!T22^qrqVF6~7Gj&8Z7M`) z4^Zg!IfyC^h?`czRoR|{xM}+2t8A*1f3v+{T)FuP`x8dqT2^3uSVfew`}C?NY`)5Q zl$mk$=GnZRi~>eL%`f-<0R`vFGeBx{3V#e^z1YwH|Mz_-4hR4G|G(e|rdn)bFf)%M znXma9qPH7p4pg4`zu3>sPJ;887y~9h6c%P&Gx@8qH?INEsVzYK;`RUkcPA%`r~?41 CFJajL delta 835 zcma)(Ur19?9LLY^?rw9sb+Q>OLuR}F@8m%_MI+YNirjZ0$jDj8=_)x4^H#UYc zIgvtTJ%~^+um)0wcB8BpsSt{ys2+4|Od$wrnsxi#duMy`p#z6=&gb|2{XNe;(b~>H zhgS|93$zE?+F9&3>+=m$LRwj5;cVupnGggc2)uG;pZbc2s+sho$HL<69wyC(Hllq{ zx7VPZI?4A$ZUy1uK&RVMiI$eWsyaGw^(5RKm_GwY_#xt%T>Z6GDNRAzU)PLB<~2#v-ri5hnSME*k@{Fl1!rn<9cO`eeOi+t!3H(A5%AMG4OYzdvt7?iZfF1i diff --git a/userland/init_service/server.c b/userland/init_service/server.c index 914ee90..0a13072 100644 --- a/userland/init_service/server.c +++ b/userland/init_service/server.c @@ -33,14 +33,6 @@ static int64_t allocate_user_stack(int slave_vspace, uint64_t stack_top, uint64_ return stack_top; } -void *memcpy(void *dst, const void *src, uint64_t n) { - uint8_t *d = (uint8_t *)dst; - const uint8_t *s = (const uint8_t *)src; - while (n--) - *d++ = *s++; - return dst; -} - __attribute__((noreturn, section(".text._start"))) void _start(BootInfo_t* boot_info) { int64_t ret; @@ -181,4 +173,4 @@ void _start(BootInfo_t* boot_info) { printf("Slave process started, init going idle\n"); for (;;) { spin_pause(); } -} \ No newline at end of file +} diff --git a/userland/libOs/shared.h b/userland/libOs/shared.h index 05be5c0..354879c 100644 --- a/userland/libOs/shared.h +++ b/userland/libOs/shared.h @@ -1,12 +1,13 @@ #pragma once -#include "types.h" -#include "syscalls.h" -#include "stdlib.h" -#include "stdio.h" #include "ipc.h" #include "memory.h" +#include "stdio.h" +#include "stdlib.h" +#include "string.h" +#include "syscalls.h" #include "tcb.h" +#include "types.h" #define SYS_CAP_COPY 0x80 diff --git a/userland/libOs/string.h b/userland/libOs/string.h new file mode 100644 index 0000000..6a0bacb --- /dev/null +++ b/userland/libOs/string.h @@ -0,0 +1,71 @@ +#pragma once + +#include "types.h" + +static inline int memcmp(const char *s1, const char *s2, size_t n) { + unsigned char u1, u2; + + for (; n--; s1++, s2++){ + u1 = *(unsigned char *)s1; + u2 = *(unsigned char *)s2; + + if (u1 != u2){ + return (u1 - u2); + } + } + + return 0; +} + +static inline void* memcpy(void *restrict destination, const void *restrict source, size_t n) { + size_t *tmp_dest = (size_t *)destination; + size_t *tmp_src = (size_t *)source; + size_t len = n / sizeof(size_t); + size_t i = 0; + size_t tail = n & (sizeof(size_t) - 1); + + for (; i < len; i++) { + *tmp_dest++ = *tmp_src++; + } + + if(tail) { + char *dest = (char *)destination; + const char *src = (const char *)source; + + for(i = n - tail; i < n; i++) { + dest[i] = src[i]; + } + } + + return destination; +} + +static inline void* memset(void* ptr, int value, size_t num) { + uint8_t* bytes = ptr; + + while(num--) { + *bytes++ = (uint8_t)value; + } +} + +static inline void* memmove(void *dest, void *src, size_t count) { + void* ret = dest; + + if (dest <= src || (char*)dest >= ((char*)src + count)) { + while (count--) { + *(char*)dest = *(char*)src; + dest = (char*)dest + 1; + src = (char*)src + 1; + } + } else { + dest = (char*)dest + count - 1; + src = (char*)src + count - 1; + while (count--) { + *(char*)dest = *(char*)src; + dest = (char*)dest - 1; + src = (char*)src - 1; + } + } + + return ret; +} \ No newline at end of file diff --git a/userland/libOs/syscalls.h b/userland/libOs/syscalls.h index 8402b81..046874e 100644 --- a/userland/libOs/syscalls.h +++ b/userland/libOs/syscalls.h @@ -1,5 +1,7 @@ #pragma once +#include "types.h" + static inline int64_t syscall0(uint64_t number) { int64_t ret; __asm__ volatile ( diff --git a/userland/libOs/types.h b/userland/libOs/types.h index 5fc1e01..5da9f2c 100644 --- a/userland/libOs/types.h +++ b/userland/libOs/types.h @@ -4,10 +4,21 @@ typedef unsigned char __uint8_t; typedef unsigned short int __uint16_t; typedef unsigned int __uint32_t; typedef unsigned long int __uint64_t; + +typedef signed char __int8_t; +typedef signed short int __int16_t; +typedef signed int __int32_t; typedef signed long int __int64_t; typedef __uint8_t uint8_t; typedef __uint16_t uint16_t; typedef __uint32_t uint32_t; typedef __uint64_t uint64_t; + +typedef __int8_t int8_t; +typedef __int16_t int16_t; +typedef __int32_t int32_t; typedef __int64_t int64_t; + +typedef __SIZE_TYPE__ size_t; +typedef int64_t ssize_t; \ No newline at end of file