From 02b36283a5279f97aeed401907caf778ff04b9cb Mon Sep 17 00:00:00 2001 From: Karina Date: Sun, 1 Feb 2026 00:26:22 +0400 Subject: [PATCH] ref(libkern): changed to match libterm --- kernel/inc/lib/String.h | 13 +-- kernel/src/FS/CPIO.c | 14 +-- kernel/src/FS/VFS.c | 4 +- kernel/src/OS/Exec/HOTLoader.c | 4 +- kernel/src/OS/Exec/OSLoader.c | 4 +- kernel/src/OS/OSScheduler.c | 4 +- kernel/src/OS/Services/OSServiceMemory.c | 2 +- kernel/src/VM/Heap.c | 2 +- kernel/src/VM/PMM.c | 2 +- kernel/src/VM/VMM.c | 12 +-- kernel/src/lib/String.c | 104 ++++++++++++----------- kernel/src/shell/ksh.c | 2 +- userspace/libterm/src/String.c | 2 +- 13 files changed, 88 insertions(+), 81 deletions(-) diff --git a/kernel/inc/lib/String.h b/kernel/inc/lib/String.h index a294eb9..79b5628 100644 --- a/kernel/inc/lib/String.h +++ b/kernel/inc/lib/String.h @@ -4,9 +4,10 @@ #pragma once #include -Int32 strcmp(const char* s1, const char* s2); -Int32 strncmp(const char* s1, const char* s2, UInt64 n); -char* strcpy(char* dest, const char* src); -char* strncpy(char* dest, const char* src, UInt64 n); -void *memset(void *ptr, int value, Size num); -void* memcpy(void* dest, const void* src, UInt64 n); \ No newline at end of file +void* MemorySet(void* destination, UInt8 value, UInt64 count); +void* MemoryCopy(void* destination, const void* source, UInt64 count); +Int32 StringCompare(const char* firstString, const char* secondString); +Int32 StringCompareWithLimit(const char* firstString, const char* secondString, UInt64 limit); +char* StringCopy(char* destination, const char* source); +char* StringCopyWithLimit(char* destination, const char* source, UInt64 limit); +UInt64 StringGetLength(const char* string); \ No newline at end of file diff --git a/kernel/src/FS/CPIO.c b/kernel/src/FS/CPIO.c index f28e0f9..4d38350 100644 --- a/kernel/src/FS/CPIO.c +++ b/kernel/src/FS/CPIO.c @@ -51,7 +51,7 @@ UInt64 FSCPIORead(FSVNode* node, UInt64 offset, UInt64 size, UInt8* buffer) { if (offset > node->dataLength) return 0; if ((offset + size) > node->dataLength) size = node->dataLength - offset; - memcpy(buffer, (char*)node->implementationData + offset, size); + MemoryCopy(buffer, (char*)node->implementationData + offset, size); return size; } @@ -62,8 +62,8 @@ FSVNode* FSCPIOMount(void* baseAddress, UInt64 totalSize) { FSVNode* rootNode = (FSVNode*)VMHeapAllocate(sizeof(FSVNode)); if (!rootNode) OSPanic("CPIO: Failed to allocate memory for root node"); - memset(rootNode, 0, sizeof(FSVNode)); - strcpy(rootNode->name, "/"); + MemorySet(rootNode, 0, sizeof(FSVNode)); + StringCopy(rootNode->name, "/"); rootNode->flags = kFSVNodeFlagDirectory; rootNode->operations = &gFSCPIOOperations; @@ -72,7 +72,7 @@ FSVNode* FSCPIOMount(void* baseAddress, UInt64 totalSize) { while (currentPointer < endPointer) { FSCPIOHeader* header = (FSCPIOHeader*)currentPointer; - if (strncmp(header->magic, "070701", 6) != 0) { + if (StringCompareWithLimit(header->magic, "070701", 6) != 0) { OSPanic("CPIO: Invalid magic detected in initramfs"); } @@ -80,7 +80,7 @@ FSVNode* FSCPIOMount(void* baseAddress, UInt64 totalSize) { UInt64 fileSize = sFSCPIOHexadecimalToUInt64(header->fileLength, 8); char* fileName = (char*)(currentPointer + sizeof(FSCPIOHeader)); - if (strcmp(fileName, "TRAILER!!!") == 0) break; + if (StringCompare(fileName, "TRAILER!!!") == 0) break; UInt64 headerAndNameLength = sizeof(FSCPIOHeader) + nameSize; UInt64 offsetToData = FSCPIO_ALIGN4(headerAndNameLength); @@ -89,8 +89,8 @@ FSVNode* FSCPIOMount(void* baseAddress, UInt64 totalSize) { FSVNode* newNode = (FSVNode*)VMHeapAllocate(sizeof(FSVNode)); if (!newNode) OSPanic("CPIO: Failed to allocate memory for new node"); - memset(newNode, 0, sizeof(FSVNode)); - strncpy(newNode->name, fileName, sizeof(newNode->name) - 1); + MemorySet(newNode, 0, sizeof(FSVNode)); + StringCopyWithLimit(newNode->name, fileName, sizeof(newNode->name) - 1); newNode->dataLength = fileSize; newNode->inodeIdentifier = sFSCPIOHexadecimalToUInt64(header->inode, 8); diff --git a/kernel/src/FS/VFS.c b/kernel/src/FS/VFS.c index 336bc74..e74dde2 100644 --- a/kernel/src/FS/VFS.c +++ b/kernel/src/FS/VFS.c @@ -23,7 +23,7 @@ FSVNode* FSVirtualFileSystemOpenPath(const char* path) { return nullptr; } - if (strcmp(path, "/") == 0) { + if (StringCompare(path, "/") == 0) { return gFSVirtualFileSystemRoot; } @@ -34,7 +34,7 @@ FSVNode* FSVirtualFileSystemOpenPath(const char* path) { FSVNode* nodeIterator = gFSVirtualFileSystemRoot->childNode; while (nodeIterator != nullptr) { - if (strcmp(nodeIterator->name, searchName) == 0) { + if (StringCompare(nodeIterator->name, searchName) == 0) { if (nodeIterator->operations && nodeIterator->operations->open) { nodeIterator->operations->open(nodeIterator); } diff --git a/kernel/src/OS/Exec/HOTLoader.c b/kernel/src/OS/Exec/HOTLoader.c index a9cbe60..5d8906f 100644 --- a/kernel/src/OS/Exec/HOTLoader.c +++ b/kernel/src/OS/Exec/HOTLoader.c @@ -29,7 +29,7 @@ UInt64 HOTLoad(OSProcess* process, UInt8* data) { void* physicalPage = VMPhysicalMemoryAllocatePage(); VMVirtualMemoryMapPage((UInt64*)process->physicalPML4, (UInt64)physicalPage, address, PTE_USER | PTE_RW | PTE_PRESENT); void* kernelVirtAddress = (void*)((UInt64)physicalPage + HHDM_OFFSET); - memset(kernelVirtAddress, 0, kVMPageSize); + MemorySet(kernelVirtAddress, 0, kVMPageSize); UInt64 pageOverleapStart = (address > segment->vaddr) ? address : segment->vaddr; UInt64 pageOverleapEnd = (address + kVMPageSize < segment->vaddr + segment->filesz) ? (address + kVMPageSize) @@ -39,7 +39,7 @@ UInt64 HOTLoad(OSProcess* process, UInt8* data) { UInt64 sourceOffset = segment->offset + (pageOverleapStart - segment->vaddr); UInt64 destinationOffset = pageOverleapStart - address; - memcpy((UInt8*)kernelVirtAddress + destinationOffset, data + sourceOffset, copySize); + MemoryCopy((UInt8*)kernelVirtAddress + destinationOffset, data + sourceOffset, copySize); } } } diff --git a/kernel/src/OS/Exec/OSLoader.c b/kernel/src/OS/Exec/OSLoader.c index 4d69bfa..3d1d49e 100644 --- a/kernel/src/OS/Exec/OSLoader.c +++ b/kernel/src/OS/Exec/OSLoader.c @@ -36,13 +36,13 @@ Int32 OSLoaderProcessSpawn(const char* executablePath, const char* processName) return -2; } - memset(newProcess, 0, sizeof(OSProcess)); + MemorySet(newProcess, 0, sizeof(OSProcess)); newProcess->processId = gOSSchedulerNextProcessID++; newProcess->state = kOSProcessStateRunning; newProcess->physicalPML4 = VMVirtualMemoryCreateAddressSpace(); newProcess->heapStartPointer = kOSHeapStart; newProcess->heapCurrentPointer = kOSHeapStart; - strncpy(newProcess->name, processName, 31); + StringCopyWithLimit(newProcess->name, processName, 31); UInt8* imageBuffer = (UInt8*)VMHeapAllocate(executableFile->dataLength); if (!imageBuffer) { diff --git a/kernel/src/OS/OSScheduler.c b/kernel/src/OS/OSScheduler.c index 8a49b38..baa2411 100644 --- a/kernel/src/OS/OSScheduler.c +++ b/kernel/src/OS/OSScheduler.c @@ -75,12 +75,12 @@ void OSSchedulerInitialize() { sOSSchedulerKernelProcess.processId = 0; sOSSchedulerKernelProcess.state = kOSProcessStateRunning; sOSSchedulerKernelProcess.physicalPML4 = gVMKernelPML4Physical; - strcpy(sOSSchedulerKernelProcess.name, "kernel"); + StringCopy(sOSSchedulerKernelProcess.name, "kernel"); OSTask* kernelTask = (OSTask*)VMHeapAllocate(sizeof(OSTask)); if (!kernelTask) OSPanic("Failed to initialize scheduler: OOm"); - memset(kernelTask, 0, sizeof(OSTask)); + MemorySet(kernelTask, 0, sizeof(OSTask)); kernelTask->id = 0; kernelTask->process = &sOSSchedulerKernelProcess; diff --git a/kernel/src/OS/Services/OSServiceMemory.c b/kernel/src/OS/Services/OSServiceMemory.c index 1faec03..54da0c3 100644 --- a/kernel/src/OS/Services/OSServiceMemory.c +++ b/kernel/src/OS/Services/OSServiceMemory.c @@ -21,7 +21,7 @@ UInt64 OSServiceMemoryAllocate(UInt64 size) { if (!phycialAddress) return 0; VMVirtualMemoryMapPage((UInt64*)currentProcess->physicalPML4, (UInt64)phycialAddress, currentProcess->heapCurrentPointer, PTE_PRESENT | PTE_RW | PTE_USER); - memset((void*)PHYS_TO_HHDM((UInt64)phycialAddress), 0, kVMPageSize); + MemorySet((void*)PHYS_TO_HHDM((UInt64)phycialAddress), 0, kVMPageSize); currentProcess->heapCurrentPointer += kVMPageSize; } diff --git a/kernel/src/VM/Heap.c b/kernel/src/VM/Heap.c index 2d05a35..3cf5d9f 100644 --- a/kernel/src/VM/Heap.c +++ b/kernel/src/VM/Heap.c @@ -122,7 +122,7 @@ void* VMHeapResize(void* pointer, UInt64 newSize) { void* newPointer = sVMHeapAllocateInternal(newSize); if (newPointer) { - memcpy(newPointer, pointer, current->size); + MemoryCopy(newPointer, pointer, current->size); sVMHeapFreeInternal(pointer); } diff --git a/kernel/src/VM/PMM.c b/kernel/src/VM/PMM.c index f82daad..995f69c 100644 --- a/kernel/src/VM/PMM.c +++ b/kernel/src/VM/PMM.c @@ -71,7 +71,7 @@ void VMPhysicalMemoryInitialize(BIMemoryMap *memoryMap) { gVMPhycalMemoryBitmap = (UInt8*)bitmapHostDescriptor->physicalStart; gVMPhycalMemoryBitmapSize = bitmapSize; - memset(gVMPhycalMemoryBitmap, 0xFF, bitmapSize); + MemorySet(gVMPhycalMemoryBitmap, 0xFF, bitmapSize); for (UInt64 i = 0; i < descriptorCount; i++) { OSMemoryDescriptor *descriptor = (OSMemoryDescriptor*)((UInt8*)memoryMap->map + (i * memoryMap->descriptorSize)); diff --git a/kernel/src/VM/VMM.c b/kernel/src/VM/VMM.c index 210dc52..386fe5d 100644 --- a/kernel/src/VM/VMM.c +++ b/kernel/src/VM/VMM.c @@ -53,7 +53,7 @@ static UInt64* sVMVirtualMemoryMapPageInternal(UInt64* pml4, UInt64 phys, UInt64 UInt64* addr = VMPhysicalMemoryAllocatePage(); if (!addr) return nullptr; UInt64* addr_virt = sVMGetVirtualTable((UInt64)addr); - memset(addr_virt, 0, kVMPageSize); + MemorySet(addr_virt, 0, kVMPageSize); pml4_virt[pml4_idx] = (UInt64)addr | table_flags; } else { pml4_virt[pml4_idx] |= (flags & PTE_USER); @@ -66,7 +66,7 @@ static UInt64* sVMVirtualMemoryMapPageInternal(UInt64* pml4, UInt64 phys, UInt64 UInt64* addr = VMPhysicalMemoryAllocatePage(); if (!addr) return nullptr; UInt64* addr_virt = sVMGetVirtualTable((UInt64)addr); - memset(addr_virt, 0, kVMPageSize); + MemorySet(addr_virt, 0, kVMPageSize); pdpt_virt[pdpt_idx] = (UInt64)addr | table_flags; } else { pdpt_virt[pdpt_idx] |= (flags & PTE_USER); @@ -79,7 +79,7 @@ static UInt64* sVMVirtualMemoryMapPageInternal(UInt64* pml4, UInt64 phys, UInt64 UInt64* addr = VMPhysicalMemoryAllocatePage(); if (!addr) return nullptr; UInt64* addr_virt = sVMGetVirtualTable((UInt64)addr); - memset(addr_virt, 0, kVMPageSize); + MemorySet(addr_virt, 0, kVMPageSize); pd_virt[pd_idx] = (UInt64)addr | table_flags; } else { pd_virt[pd_idx] |= (flags & PTE_USER); @@ -145,7 +145,7 @@ void VMLoadCR3(UInt64 pml4_addr) { void VMVirtualMemoryInitialize(Bootinfo* info) { gVMKernelPML4Physical = (UInt64)VMPhysicalMemoryAllocatePage(); gVMKernelPML4 = (UInt64*)gVMKernelPML4Physical; - memset(gVMKernelPML4, 0, kVMPageSize); + MemorySet(gVMKernelPML4, 0, kVMPageSize); UInt64 k_virt_start = (UInt64)&_kernel_start; UInt64 k_virt_end = (UInt64)&_kernel_end; @@ -177,7 +177,7 @@ UInt64 VMVirtualMemoryCreateAddressSpace() { }; UInt64* virt = (UInt64*)PHYS_TO_HHDM(phys); - memset(virt, 0, kVMPageSize); + MemorySet(virt, 0, kVMPageSize); UInt64* kernel_pml4_virt = sVMGetVirtualTable((UInt64)gVMKernelPML4); @@ -201,7 +201,7 @@ void VMVirtualMemorySetupUserStack(UInt64* pml4_phys) { for (UInt64 addr = stack_bottom; addr < kVMUserStackTop; addr += 4096) { void* phys = VMPhysicalMemoryAllocatePage(); if (!phys) OSPanic("OOM in user stack setup"); - memset((void*)PHYS_TO_HHDM((UInt64)phys), 0, 4096); + MemorySet((void*)PHYS_TO_HHDM((UInt64)phys), 0, 4096); VMVirtualMemoryMapPage((UInt64*)pml4_phys, (UInt64)phys, addr, PTE_PRESENT | PTE_RW | PTE_USER); } } diff --git a/kernel/src/lib/String.c b/kernel/src/lib/String.c index 1f32227..8590bf6 100644 --- a/kernel/src/lib/String.c +++ b/kernel/src/lib/String.c @@ -3,69 +3,75 @@ #include -Int32 strcmp(const char *s1, const char *s2) { - while (*s1 && (*s1 == *s2)) { - s1++; - s2++; +void* MemorySet(void* destination, UInt8 value, UInt64 count) { + UInt8* bytePointer = (UInt8*) destination; + while (count--) { + *bytePointer++ = (UInt8)value; } - return *(const unsigned char*)s1 - *(const unsigned char*)s2; + return destination; } -Int32 strncmp(const char* s1, const char* s2, UInt64 n) { - while (n > 0) { - if (*s1 != *s2) return *(unsigned char*)s1 - *(unsigned char*)s2; - if (*s1 == '\0') return 0; - s1++; - s2++; - n--; +void* MemoryCopy(void* destination, const void* source, UInt64 count) { + UInt8* destinationBuffer = (UInt8*)destination; + const UInt8* sourceBuffer = (const UInt8*)source; + + while (count >= 8) { + *(UInt64*)destinationBuffer = *(const UInt64*)sourceBuffer; + destinationBuffer += 8; + sourceBuffer += 8; + count -= 8; + } + + while (count > 0) { + *destinationBuffer++ = *sourceBuffer++; + count--; + } + + return destination; +} + +Int32 StringCompare(const char* firstString, const char* secondString) { + while (*firstString && (*firstString == *secondString)) { + firstString++; + secondString++; + } + return *(const unsigned char*)firstString - *(const unsigned char*)secondString; +} + +Int32 StringCompareWithLimit(const char* firstString, const char* secondString, UInt64 limit) { + while (limit > 0) { + if (*firstString != *secondString) return *(unsigned char*)firstString - *(unsigned char*)secondString; + if (*firstString == '\0') return 0; + firstString++; + secondString++; + limit--; } return 0; } -char* strcpy(char* dest, const char* src) { - char* saved = dest; - while (*src) *dest++ = *src++; - *dest = 0; +char* StringCopy(char* destination, const char* source) { + char* saved = destination; + while (*source) *destination++ = *source++; + *destination = 0; return saved; } -char* strncpy(char* dest, const char* src, UInt64 n) { - char* saved = dest; - while (*src && n > 0) { - *dest++ = *src++; - n--; +char* StringCopyWithLimit(char* destination, const char* source, UInt64 limit) { + char* saved = destination; + while (*source && limit > 0) { + *destination++ = *source++; + limit--; } - while (n > 0) { - *dest++ = 0; - n--; + while (limit > 0) { + *destination++ = 0; + limit--; } return saved; } -void *memset(void *ptr, int value, Size num) { - UInt8 *p = (UInt8 *)ptr; - while (num--) { - *p++ = (UInt8)value; - } - return ptr; -} - -void* memcpy(void* dest, const void* src, UInt64 n) { - UInt8* d = (UInt8*)dest; - const UInt8* s = (const UInt8*)src; - - while (n >= 8) { - *(UInt64*)d = *(const UInt64*)s; - d += 8; - s += 8; - n -= 8; - } - - while (n > 0) { - *d++ = *s++; - n--; - } - - return dest; +UInt64 StringGetLength(const char* string) { + UInt64 result = 0; + for (result = 0; string[result]; result++); + return result; } \ No newline at end of file diff --git a/kernel/src/shell/ksh.c b/kernel/src/shell/ksh.c index 8bade3e..d8792fb 100644 --- a/kernel/src/shell/ksh.c +++ b/kernel/src/shell/ksh.c @@ -74,7 +74,7 @@ static const KSHCommandMap CommandMap[] = { KSHToken char2token(char* token) { for (Int32 i = 0; CommandMap[i].str != nullptr; i++) { - if (strcmp(token, CommandMap[i].str) == 0) return CommandMap[i].token; + if (StringCompare(token, CommandMap[i].str) == 0) return CommandMap[i].token; } return TOKEN_ILLEGAL; } diff --git a/userspace/libterm/src/String.c b/userspace/libterm/src/String.c index d2cedfa..b3cf90d 100644 --- a/userspace/libterm/src/String.c +++ b/userspace/libterm/src/String.c @@ -5,7 +5,7 @@ static char* olds; -void* MemorySet(void* destination, UInt8 value, usize count) { +void* MemorySet(void* destination, UInt8 value, UInt64 count) { UInt8* bytePointer = (UInt8*) destination; while (count--) { *bytePointer++ = (UInt8)value;