modified some syscall code in rlibc, fs stuff
This commit is contained in:
@@ -14,18 +14,29 @@ static inline int32_t inline_syscall(uint32_t num, uint32_t arg1, uint32_t arg2,
|
||||
return ret;
|
||||
}
|
||||
|
||||
void _PDCLIB_exit(int status) {
|
||||
void _exit(int status) {
|
||||
inline_syscall(1, (uint32_t)status, 0, 0);
|
||||
while(1); // Halt if exit fails to yield
|
||||
while(1) { asm volatile("hlt"); }
|
||||
}
|
||||
|
||||
void _PDCLIB_exit(int status) {
|
||||
_exit(status);
|
||||
}
|
||||
|
||||
int _PDCLIB_read(_PDCLIB_fd_t fd, void *buf, size_t count) {
|
||||
return inline_syscall(3, (uint32_t)fd, (uint32_t)buf, (uint32_t)count);
|
||||
int32_t ret = inline_syscall(3, (uint32_t)fd, (uint32_t)buf, (uint32_t)count);
|
||||
if (ret < 0) {
|
||||
return 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 3. sys_write (eax = 4)
|
||||
int _PDCLIB_write(_PDCLIB_fd_t fd, const void *buf, size_t count) {
|
||||
return inline_syscall(4, (uint32_t)fd, (uint32_t)buf, (uint32_t)count);
|
||||
int32_t ret = inline_syscall(4, (uint32_t)fd, (uint32_t)buf, (uint32_t)count);
|
||||
if (ret < 0) {
|
||||
return 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
_PDCLIB_fd_t _PDCLIB_open(const char *filename, unsigned int mode) {
|
||||
@@ -38,21 +49,24 @@ int _PDCLIB_close(_PDCLIB_fd_t fd) {
|
||||
return inline_syscall(6, (uint32_t)fd, 0, 0);
|
||||
}
|
||||
|
||||
void * _PDCLIB_allocpages(size_t n) {
|
||||
// n is the number of PAGES PDCLib wants (usually 4096 bytes per page)
|
||||
size_t bytes_requested = n * 4096;
|
||||
|
||||
// Call your sys_brk with 0 to find the current break address
|
||||
void * _PDCLIB_allocpages( size_t n ) {
|
||||
// 1. Get the current page break
|
||||
uint32_t current_break = inline_syscall(12, 0, 0, 0);
|
||||
|
||||
// Request the new break point
|
||||
uint32_t new_break = current_break + bytes_requested;
|
||||
uint32_t result = inline_syscall(12, new_break, 0, 0);
|
||||
|
||||
if (result == 0) {
|
||||
return (void *)current_break; // Return the start of the newly allocated region
|
||||
if (current_break == (uint32_t)-1) {
|
||||
return NULL;
|
||||
}
|
||||
return NULL; // Allocation failed
|
||||
|
||||
// 2. PDCLib requests memory in multiples of PAGES (typically 4096 bytes)
|
||||
uint32_t new_break = current_break + (n * 4096);
|
||||
|
||||
// 3. Request kernel to move the break
|
||||
uint32_t result = inline_syscall(12, new_break, 0, 0);
|
||||
if (result != new_break) {
|
||||
return NULL; // Allocation failed
|
||||
}
|
||||
|
||||
// Return the start of the newly allocated region
|
||||
return (void *)current_break;
|
||||
}
|
||||
|
||||
int _PDCLIB_remove(const char *filename) { return -1; }
|
||||
@@ -60,7 +74,6 @@ int _PDCLIB_rename(const char *old, const char *new) { return -1; }
|
||||
long _PDCLIB_lseek(_PDCLIB_fd_t fd, long offset, int whence) { return -1; }
|
||||
|
||||
int open(const char *pathname, int flags, ...) {
|
||||
// Wrap to your internal _PDCLIB_open or inline_syscall directly
|
||||
_PDCLIB_fd_t fd = _PDCLIB_open(pathname, (unsigned int)flags);
|
||||
return fd;
|
||||
}
|
||||
@@ -79,19 +92,23 @@ int close(int fd) {
|
||||
return _PDCLIB_close(file_desc);
|
||||
}
|
||||
|
||||
void _exit(int status) {
|
||||
_PDCLIB_exit(status);
|
||||
}
|
||||
|
||||
void * sbrk(intptr_t increment) {
|
||||
// If increment is 0, dlmalloc is querying the current boundary.
|
||||
// For a simple implementation, let's look up memory by pages.
|
||||
if (increment == 0) {
|
||||
return _PDCLIB_allocpages(0);
|
||||
uint32_t current_break = inline_syscall(12, 0, 0, 0);
|
||||
if (current_break == (uint32_t)-1) {
|
||||
return (void *)-1;
|
||||
}
|
||||
|
||||
if (increment == 0) {
|
||||
return (void *)current_break;
|
||||
}
|
||||
|
||||
uint32_t new_break = (uint32_t)((intptr_t)current_break + increment);
|
||||
|
||||
// Convert bytes requested by sbrk into page increments (4KB blocks)
|
||||
// You can also change your syscall wrapper to use regular byte increments
|
||||
size_t pages_needed = ((size_t)increment + 4095) / 4096;
|
||||
return _PDCLIB_allocpages(pages_needed);
|
||||
uint32_t result = inline_syscall(12, new_break, 0, 0);
|
||||
|
||||
if (result == new_break) {
|
||||
return (void *)current_break;
|
||||
}
|
||||
|
||||
return (void *)-1;
|
||||
}
|
||||
Reference in New Issue
Block a user