97 lines
2.9 KiB
C
97 lines
2.9 KiB
C
|
|
#include <stdint.h>
|
||
|
|
#include <stddef.h>
|
||
|
|
|
||
|
|
typedef int _PDCLIB_fd_t;
|
||
|
|
|
||
|
|
static inline int32_t inline_syscall(uint32_t num, uint32_t arg1, uint32_t arg2, uint32_t arg3) {
|
||
|
|
int32_t ret;
|
||
|
|
asm volatile (
|
||
|
|
"int $0x80"
|
||
|
|
: "=a" (ret)
|
||
|
|
: "a" (num), "b" (arg1), "c" (arg2), "d" (arg3)
|
||
|
|
: "memory"
|
||
|
|
);
|
||
|
|
return ret;
|
||
|
|
}
|
||
|
|
|
||
|
|
void _PDCLIB_exit(int status) {
|
||
|
|
inline_syscall(1, (uint32_t)status, 0, 0);
|
||
|
|
while(1); // Halt if exit fails to yield
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 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);
|
||
|
|
}
|
||
|
|
|
||
|
|
_PDCLIB_fd_t _PDCLIB_open(const char *filename, unsigned int mode) {
|
||
|
|
_PDCLIB_fd_t fd;
|
||
|
|
fd = inline_syscall(5, (uint32_t)filename, (uint32_t)mode, 0);
|
||
|
|
return fd;
|
||
|
|
}
|
||
|
|
|
||
|
|
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
|
||
|
|
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
|
||
|
|
}
|
||
|
|
return NULL; // Allocation failed
|
||
|
|
}
|
||
|
|
|
||
|
|
int _PDCLIB_remove(const char *filename) { return -1; }
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
|
||
|
|
int read(int fd, void *buf, size_t count) {
|
||
|
|
_PDCLIB_fd_t file_desc = fd;
|
||
|
|
return _PDCLIB_read(file_desc, buf, count);
|
||
|
|
}
|
||
|
|
|
||
|
|
int write(int fd, const void *buf, size_t count) {
|
||
|
|
return _PDCLIB_write(fd, buf, count);
|
||
|
|
}
|
||
|
|
|
||
|
|
int close(int fd) {
|
||
|
|
_PDCLIB_fd_t file_desc = 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);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 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);
|
||
|
|
}
|