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
#include <types.h>
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);
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);
+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 + 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);
+2 -2
View File
@@ -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);
}
+2 -2
View File
@@ -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);
}
}
}
+2 -2
View File
@@ -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) {
+2 -2
View File
@@ -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;
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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);
}
+1 -1
View File
@@ -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));
+6 -6
View File
@@ -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);
}
}
+55 -49
View File
@@ -3,69 +3,75 @@
#include <lib/String.h>
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;
}
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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;