ref(libkern): changed to match libterm

This commit is contained in:
Karina
2026-02-01 00:26:22 +04:00
parent e21f5ef52f
commit 02b36283a5
13 changed files with 88 additions and 81 deletions
+7 -6
View File
@@ -4,9 +4,10 @@
#pragma once #pragma once
#include <types.h> #include <types.h>
Int32 strcmp(const char* s1, const char* s2); void* MemorySet(void* destination, UInt8 value, UInt64 count);
Int32 strncmp(const char* s1, const char* s2, UInt64 n); void* MemoryCopy(void* destination, const void* source, UInt64 count);
char* strcpy(char* dest, const char* src); Int32 StringCompare(const char* firstString, const char* secondString);
char* strncpy(char* dest, const char* src, UInt64 n); Int32 StringCompareWithLimit(const char* firstString, const char* secondString, UInt64 limit);
void *memset(void *ptr, int value, Size num); char* StringCopy(char* destination, const char* source);
void* memcpy(void* dest, const void* src, UInt64 n); char* StringCopyWithLimit(char* destination, const char* source, UInt64 limit);
UInt64 StringGetLength(const char* string);
+7 -7
View File
@@ -51,7 +51,7 @@ UInt64 FSCPIORead(FSVNode* node, UInt64 offset, UInt64 size, UInt8* buffer) {
if (offset > node->dataLength) return 0; if (offset > node->dataLength) return 0;
if ((offset + size) > node->dataLength) size = node->dataLength - offset; 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; return size;
} }
@@ -62,8 +62,8 @@ FSVNode* FSCPIOMount(void* baseAddress, UInt64 totalSize) {
FSVNode* rootNode = (FSVNode*)VMHeapAllocate(sizeof(FSVNode)); FSVNode* rootNode = (FSVNode*)VMHeapAllocate(sizeof(FSVNode));
if (!rootNode) OSPanic("CPIO: Failed to allocate memory for root node"); if (!rootNode) OSPanic("CPIO: Failed to allocate memory for root node");
memset(rootNode, 0, sizeof(FSVNode)); MemorySet(rootNode, 0, sizeof(FSVNode));
strcpy(rootNode->name, "/"); StringCopy(rootNode->name, "/");
rootNode->flags = kFSVNodeFlagDirectory; rootNode->flags = kFSVNodeFlagDirectory;
rootNode->operations = &gFSCPIOOperations; rootNode->operations = &gFSCPIOOperations;
@@ -72,7 +72,7 @@ FSVNode* FSCPIOMount(void* baseAddress, UInt64 totalSize) {
while (currentPointer < endPointer) { while (currentPointer < endPointer) {
FSCPIOHeader* header = (FSCPIOHeader*)currentPointer; 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"); OSPanic("CPIO: Invalid magic detected in initramfs");
} }
@@ -80,7 +80,7 @@ FSVNode* FSCPIOMount(void* baseAddress, UInt64 totalSize) {
UInt64 fileSize = sFSCPIOHexadecimalToUInt64(header->fileLength, 8); UInt64 fileSize = sFSCPIOHexadecimalToUInt64(header->fileLength, 8);
char* fileName = (char*)(currentPointer + sizeof(FSCPIOHeader)); char* fileName = (char*)(currentPointer + sizeof(FSCPIOHeader));
if (strcmp(fileName, "TRAILER!!!") == 0) break; if (StringCompare(fileName, "TRAILER!!!") == 0) break;
UInt64 headerAndNameLength = sizeof(FSCPIOHeader) + nameSize; UInt64 headerAndNameLength = sizeof(FSCPIOHeader) + nameSize;
UInt64 offsetToData = FSCPIO_ALIGN4(headerAndNameLength); UInt64 offsetToData = FSCPIO_ALIGN4(headerAndNameLength);
@@ -89,8 +89,8 @@ FSVNode* FSCPIOMount(void* baseAddress, UInt64 totalSize) {
FSVNode* newNode = (FSVNode*)VMHeapAllocate(sizeof(FSVNode)); FSVNode* newNode = (FSVNode*)VMHeapAllocate(sizeof(FSVNode));
if (!newNode) OSPanic("CPIO: Failed to allocate memory for new node"); if (!newNode) OSPanic("CPIO: Failed to allocate memory for new node");
memset(newNode, 0, sizeof(FSVNode)); MemorySet(newNode, 0, sizeof(FSVNode));
strncpy(newNode->name, fileName, sizeof(newNode->name) - 1); StringCopyWithLimit(newNode->name, fileName, sizeof(newNode->name) - 1);
newNode->dataLength = fileSize; newNode->dataLength = fileSize;
newNode->inodeIdentifier = sFSCPIOHexadecimalToUInt64(header->inode, 8); newNode->inodeIdentifier = sFSCPIOHexadecimalToUInt64(header->inode, 8);
+2 -2
View File
@@ -23,7 +23,7 @@ FSVNode* FSVirtualFileSystemOpenPath(const char* path) {
return nullptr; return nullptr;
} }
if (strcmp(path, "/") == 0) { if (StringCompare(path, "/") == 0) {
return gFSVirtualFileSystemRoot; return gFSVirtualFileSystemRoot;
} }
@@ -34,7 +34,7 @@ FSVNode* FSVirtualFileSystemOpenPath(const char* path) {
FSVNode* nodeIterator = gFSVirtualFileSystemRoot->childNode; FSVNode* nodeIterator = gFSVirtualFileSystemRoot->childNode;
while (nodeIterator != nullptr) { while (nodeIterator != nullptr) {
if (strcmp(nodeIterator->name, searchName) == 0) { if (StringCompare(nodeIterator->name, searchName) == 0) {
if (nodeIterator->operations && nodeIterator->operations->open) { if (nodeIterator->operations && nodeIterator->operations->open) {
nodeIterator->operations->open(nodeIterator); nodeIterator->operations->open(nodeIterator);
} }
+2 -2
View File
@@ -29,7 +29,7 @@ UInt64 HOTLoad(OSProcess* process, UInt8* data) {
void* physicalPage = VMPhysicalMemoryAllocatePage(); void* physicalPage = VMPhysicalMemoryAllocatePage();
VMVirtualMemoryMapPage((UInt64*)process->physicalPML4, (UInt64)physicalPage, address, PTE_USER | PTE_RW | PTE_PRESENT); VMVirtualMemoryMapPage((UInt64*)process->physicalPML4, (UInt64)physicalPage, address, PTE_USER | PTE_RW | PTE_PRESENT);
void* kernelVirtAddress = (void*)((UInt64)physicalPage + HHDM_OFFSET); void* kernelVirtAddress = (void*)((UInt64)physicalPage + HHDM_OFFSET);
memset(kernelVirtAddress, 0, kVMPageSize); MemorySet(kernelVirtAddress, 0, kVMPageSize);
UInt64 pageOverleapStart = (address > segment->vaddr) ? address : segment->vaddr; UInt64 pageOverleapStart = (address > segment->vaddr) ? address : segment->vaddr;
UInt64 pageOverleapEnd = (address + kVMPageSize < segment->vaddr + segment->filesz) UInt64 pageOverleapEnd = (address + kVMPageSize < segment->vaddr + segment->filesz)
? (address + kVMPageSize) ? (address + kVMPageSize)
@@ -39,7 +39,7 @@ UInt64 HOTLoad(OSProcess* process, UInt8* data) {
UInt64 sourceOffset = segment->offset + (pageOverleapStart - segment->vaddr); UInt64 sourceOffset = segment->offset + (pageOverleapStart - segment->vaddr);
UInt64 destinationOffset = pageOverleapStart - address; UInt64 destinationOffset = pageOverleapStart - address;
memcpy((UInt8*)kernelVirtAddress + destinationOffset, data + sourceOffset, copySize); MemoryCopy((UInt8*)kernelVirtAddress + destinationOffset, data + sourceOffset, copySize);
} }
} }
} }
+2 -2
View File
@@ -36,13 +36,13 @@ Int32 OSLoaderProcessSpawn(const char* executablePath, const char* processName)
return -2; return -2;
} }
memset(newProcess, 0, sizeof(OSProcess)); MemorySet(newProcess, 0, sizeof(OSProcess));
newProcess->processId = gOSSchedulerNextProcessID++; newProcess->processId = gOSSchedulerNextProcessID++;
newProcess->state = kOSProcessStateRunning; newProcess->state = kOSProcessStateRunning;
newProcess->physicalPML4 = VMVirtualMemoryCreateAddressSpace(); newProcess->physicalPML4 = VMVirtualMemoryCreateAddressSpace();
newProcess->heapStartPointer = kOSHeapStart; newProcess->heapStartPointer = kOSHeapStart;
newProcess->heapCurrentPointer = kOSHeapStart; newProcess->heapCurrentPointer = kOSHeapStart;
strncpy(newProcess->name, processName, 31); StringCopyWithLimit(newProcess->name, processName, 31);
UInt8* imageBuffer = (UInt8*)VMHeapAllocate(executableFile->dataLength); UInt8* imageBuffer = (UInt8*)VMHeapAllocate(executableFile->dataLength);
if (!imageBuffer) { if (!imageBuffer) {
+2 -2
View File
@@ -75,12 +75,12 @@ void OSSchedulerInitialize() {
sOSSchedulerKernelProcess.processId = 0; sOSSchedulerKernelProcess.processId = 0;
sOSSchedulerKernelProcess.state = kOSProcessStateRunning; sOSSchedulerKernelProcess.state = kOSProcessStateRunning;
sOSSchedulerKernelProcess.physicalPML4 = gVMKernelPML4Physical; sOSSchedulerKernelProcess.physicalPML4 = gVMKernelPML4Physical;
strcpy(sOSSchedulerKernelProcess.name, "kernel"); StringCopy(sOSSchedulerKernelProcess.name, "kernel");
OSTask* kernelTask = (OSTask*)VMHeapAllocate(sizeof(OSTask)); OSTask* kernelTask = (OSTask*)VMHeapAllocate(sizeof(OSTask));
if (!kernelTask) OSPanic("Failed to initialize scheduler: OOm"); if (!kernelTask) OSPanic("Failed to initialize scheduler: OOm");
memset(kernelTask, 0, sizeof(OSTask)); MemorySet(kernelTask, 0, sizeof(OSTask));
kernelTask->id = 0; kernelTask->id = 0;
kernelTask->process = &sOSSchedulerKernelProcess; kernelTask->process = &sOSSchedulerKernelProcess;
+1 -1
View File
@@ -21,7 +21,7 @@ UInt64 OSServiceMemoryAllocate(UInt64 size) {
if (!phycialAddress) return 0; if (!phycialAddress) return 0;
VMVirtualMemoryMapPage((UInt64*)currentProcess->physicalPML4, (UInt64)phycialAddress, currentProcess->heapCurrentPointer, PTE_PRESENT | PTE_RW | PTE_USER); 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; currentProcess->heapCurrentPointer += kVMPageSize;
} }
+1 -1
View File
@@ -122,7 +122,7 @@ void* VMHeapResize(void* pointer, UInt64 newSize) {
void* newPointer = sVMHeapAllocateInternal(newSize); void* newPointer = sVMHeapAllocateInternal(newSize);
if (newPointer) { if (newPointer) {
memcpy(newPointer, pointer, current->size); MemoryCopy(newPointer, pointer, current->size);
sVMHeapFreeInternal(pointer); sVMHeapFreeInternal(pointer);
} }
+1 -1
View File
@@ -71,7 +71,7 @@ void VMPhysicalMemoryInitialize(BIMemoryMap *memoryMap) {
gVMPhycalMemoryBitmap = (UInt8*)bitmapHostDescriptor->physicalStart; gVMPhycalMemoryBitmap = (UInt8*)bitmapHostDescriptor->physicalStart;
gVMPhycalMemoryBitmapSize = bitmapSize; gVMPhycalMemoryBitmapSize = bitmapSize;
memset(gVMPhycalMemoryBitmap, 0xFF, bitmapSize); MemorySet(gVMPhycalMemoryBitmap, 0xFF, bitmapSize);
for (UInt64 i = 0; i < descriptorCount; i++) { for (UInt64 i = 0; i < descriptorCount; i++) {
OSMemoryDescriptor *descriptor = (OSMemoryDescriptor*)((UInt8*)memoryMap->map + (i * memoryMap->descriptorSize)); OSMemoryDescriptor *descriptor = (OSMemoryDescriptor*)((UInt8*)memoryMap->map + (i * memoryMap->descriptorSize));
+6 -6
View File
@@ -53,7 +53,7 @@ static UInt64* sVMVirtualMemoryMapPageInternal(UInt64* pml4, UInt64 phys, UInt64
UInt64* addr = VMPhysicalMemoryAllocatePage(); UInt64* addr = VMPhysicalMemoryAllocatePage();
if (!addr) return nullptr; if (!addr) return nullptr;
UInt64* addr_virt = sVMGetVirtualTable((UInt64)addr); UInt64* addr_virt = sVMGetVirtualTable((UInt64)addr);
memset(addr_virt, 0, kVMPageSize); MemorySet(addr_virt, 0, kVMPageSize);
pml4_virt[pml4_idx] = (UInt64)addr | table_flags; pml4_virt[pml4_idx] = (UInt64)addr | table_flags;
} else { } else {
pml4_virt[pml4_idx] |= (flags & PTE_USER); pml4_virt[pml4_idx] |= (flags & PTE_USER);
@@ -66,7 +66,7 @@ static UInt64* sVMVirtualMemoryMapPageInternal(UInt64* pml4, UInt64 phys, UInt64
UInt64* addr = VMPhysicalMemoryAllocatePage(); UInt64* addr = VMPhysicalMemoryAllocatePage();
if (!addr) return nullptr; if (!addr) return nullptr;
UInt64* addr_virt = sVMGetVirtualTable((UInt64)addr); UInt64* addr_virt = sVMGetVirtualTable((UInt64)addr);
memset(addr_virt, 0, kVMPageSize); MemorySet(addr_virt, 0, kVMPageSize);
pdpt_virt[pdpt_idx] = (UInt64)addr | table_flags; pdpt_virt[pdpt_idx] = (UInt64)addr | table_flags;
} else { } else {
pdpt_virt[pdpt_idx] |= (flags & PTE_USER); pdpt_virt[pdpt_idx] |= (flags & PTE_USER);
@@ -79,7 +79,7 @@ static UInt64* sVMVirtualMemoryMapPageInternal(UInt64* pml4, UInt64 phys, UInt64
UInt64* addr = VMPhysicalMemoryAllocatePage(); UInt64* addr = VMPhysicalMemoryAllocatePage();
if (!addr) return nullptr; if (!addr) return nullptr;
UInt64* addr_virt = sVMGetVirtualTable((UInt64)addr); UInt64* addr_virt = sVMGetVirtualTable((UInt64)addr);
memset(addr_virt, 0, kVMPageSize); MemorySet(addr_virt, 0, kVMPageSize);
pd_virt[pd_idx] = (UInt64)addr | table_flags; pd_virt[pd_idx] = (UInt64)addr | table_flags;
} else { } else {
pd_virt[pd_idx] |= (flags & PTE_USER); pd_virt[pd_idx] |= (flags & PTE_USER);
@@ -145,7 +145,7 @@ void VMLoadCR3(UInt64 pml4_addr) {
void VMVirtualMemoryInitialize(Bootinfo* info) { void VMVirtualMemoryInitialize(Bootinfo* info) {
gVMKernelPML4Physical = (UInt64)VMPhysicalMemoryAllocatePage(); gVMKernelPML4Physical = (UInt64)VMPhysicalMemoryAllocatePage();
gVMKernelPML4 = (UInt64*)gVMKernelPML4Physical; gVMKernelPML4 = (UInt64*)gVMKernelPML4Physical;
memset(gVMKernelPML4, 0, kVMPageSize); MemorySet(gVMKernelPML4, 0, kVMPageSize);
UInt64 k_virt_start = (UInt64)&_kernel_start; UInt64 k_virt_start = (UInt64)&_kernel_start;
UInt64 k_virt_end = (UInt64)&_kernel_end; UInt64 k_virt_end = (UInt64)&_kernel_end;
@@ -177,7 +177,7 @@ UInt64 VMVirtualMemoryCreateAddressSpace() {
}; };
UInt64* virt = (UInt64*)PHYS_TO_HHDM(phys); UInt64* virt = (UInt64*)PHYS_TO_HHDM(phys);
memset(virt, 0, kVMPageSize); MemorySet(virt, 0, kVMPageSize);
UInt64* kernel_pml4_virt = sVMGetVirtualTable((UInt64)gVMKernelPML4); 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) { for (UInt64 addr = stack_bottom; addr < kVMUserStackTop; addr += 4096) {
void* phys = VMPhysicalMemoryAllocatePage(); void* phys = VMPhysicalMemoryAllocatePage();
if (!phys) OSPanic("OOM in user stack setup"); 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); VMVirtualMemoryMapPage((UInt64*)pml4_phys, (UInt64)phys, addr, PTE_PRESENT | PTE_RW | PTE_USER);
} }
} }
+55 -49
View File
@@ -3,69 +3,75 @@
#include <lib/String.h> #include <lib/String.h>
Int32 strcmp(const char *s1, const char *s2) { void* MemorySet(void* destination, UInt8 value, UInt64 count) {
while (*s1 && (*s1 == *s2)) { UInt8* bytePointer = (UInt8*) destination;
s1++; while (count--) {
s2++; *bytePointer++ = (UInt8)value;
} }
return *(const unsigned char*)s1 - *(const unsigned char*)s2; return destination;
} }
Int32 strncmp(const char* s1, const char* s2, UInt64 n) { void* MemoryCopy(void* destination, const void* source, UInt64 count) {
while (n > 0) { UInt8* destinationBuffer = (UInt8*)destination;
if (*s1 != *s2) return *(unsigned char*)s1 - *(unsigned char*)s2; const UInt8* sourceBuffer = (const UInt8*)source;
if (*s1 == '\0') return 0;
s1++; while (count >= 8) {
s2++; *(UInt64*)destinationBuffer = *(const UInt64*)sourceBuffer;
n--; 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; return 0;
} }
char* strcpy(char* dest, const char* src) { char* StringCopy(char* destination, const char* source) {
char* saved = dest; char* saved = destination;
while (*src) *dest++ = *src++; while (*source) *destination++ = *source++;
*dest = 0; *destination = 0;
return saved; return saved;
} }
char* strncpy(char* dest, const char* src, UInt64 n) { char* StringCopyWithLimit(char* destination, const char* source, UInt64 limit) {
char* saved = dest; char* saved = destination;
while (*src && n > 0) { while (*source && limit > 0) {
*dest++ = *src++; *destination++ = *source++;
n--; limit--;
} }
while (n > 0) { while (limit > 0) {
*dest++ = 0; *destination++ = 0;
n--; limit--;
} }
return saved; return saved;
} }
void *memset(void *ptr, int value, Size num) { UInt64 StringGetLength(const char* string) {
UInt8 *p = (UInt8 *)ptr; UInt64 result = 0;
while (num--) { for (result = 0; string[result]; result++);
*p++ = (UInt8)value; return result;
}
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;
} }
+1 -1
View File
@@ -74,7 +74,7 @@ static const KSHCommandMap CommandMap[] = {
KSHToken char2token(char* token) { KSHToken char2token(char* token) {
for (Int32 i = 0; CommandMap[i].str != nullptr; i++) { 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; return TOKEN_ILLEGAL;
} }
+1 -1
View File
@@ -5,7 +5,7 @@
static char* olds; static char* olds;
void* MemorySet(void* destination, UInt8 value, usize count) { void* MemorySet(void* destination, UInt8 value, UInt64 count) {
UInt8* bytePointer = (UInt8*) destination; UInt8* bytePointer = (UInt8*) destination;
while (count--) { while (count--) {
*bytePointer++ = (UInt8)value; *bytePointer++ = (UInt8)value;