-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kernel: add linux compatibility layer
- Loading branch information
1 parent
a8452c0
commit c520edb
Showing
22 changed files
with
742 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
140.82.121.4 github |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
root:x:0:0::/:none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// https://chromium.googlesource.com/chromiumos/docs/+/HEAD/constants/syscalls.md | ||
#ifndef SYS_LINUX_COMPAT_H | ||
#define SYS_LINUX_COMPAT_H | ||
|
||
#ifdef __x86_64__ | ||
|
||
#define SYSCALL_READ 0 | ||
#define SYSCALL_WRITE 1 | ||
#define SYSCALL_OPEN 2 | ||
#define SYSCALL_CLOSE 3 | ||
#define SYSCALL_FSTAT 5 | ||
#define SYSCALL_LSEEK 8 | ||
#define SYSCALL_BRK 12 | ||
#define SYSCALL_IOCTL 16 | ||
#define SYSCALL_WRITEV 20 | ||
#define SYSCALL_DUP2 33 | ||
#define SYSCALL_GETPID 39 | ||
#define SYSCALL_SOCKET 41 | ||
#define SYSCALL_SENDTO 44 | ||
#define SYSCALL_RECVFROM 45 | ||
#define SYSCALL_EXECV 49 | ||
#define SYSCALL_EXIT 60 | ||
#define SYSCALL_GETTIMEOFDAY 96 | ||
#define SYSCALL_GETEUID 107 | ||
#define SYSCALL_ARCH_PRCTL 158 | ||
#define SYSCALL_REBOOT 169 | ||
#define SYSCALL_SET_TID_ADDR 218 | ||
#define SYSCALL_EXIT_GROUP 231 | ||
#define SYSCALL_OPENAT 257 | ||
|
||
#elif __arm__ | ||
|
||
#define SYSCALL_EXIT 1 | ||
#define SYSCALL_READ 3 | ||
#define SYSCALL_WRITE 4 | ||
#define SYSCALL_OPEN 5 | ||
#define SYSCALL_CLOSE 6 | ||
#define SYSCALL_EXECV 11 | ||
#define SYSCALL_LSEEK 19 | ||
#define SYSCALL_GETPID 20 | ||
#define SYSCALL_BRK 45 | ||
#define SYSCALL_GETEUID 49 | ||
#define SYSCALL_IOCTL 54 | ||
#define SYSCALL_DUP2 63 | ||
#define SYSCALL_GETTIMEOFDAY 78 | ||
#define SYSCALL_REBOOT 88 | ||
#define SYSCALL_FSTAT 108 | ||
#define SYSCALL_WRITEV 146 | ||
#define SYSCALL_EXIT_GROUP 248 | ||
#define SYSCALL_SET_TID_ADDR 256 | ||
#define SYSCALL_SOCKET 281 | ||
#define SYSCALL_SENDTO 290 | ||
#define SYSCALL_RECVFROM 292 | ||
#define SYSCALL_OPENAT 322 | ||
|
||
// Not available on AArch32: | ||
// | ||
// - SYSCALL_ARCH_PRCTL | ||
|
||
#elif __aarch64__ | ||
|
||
#define SYSCALL_IOCTL 29 | ||
#define SYSCALL_OPENAT 56 | ||
#define SYSCALL_CLOSE 57 | ||
#define SYSCALL_LSEEK 62 | ||
#define SYSCALL_READ 63 | ||
#define SYSCALL_WRITE 64 | ||
#define SYSCALL_WRITEV 66 | ||
#define SYSCALL_FSTAT 80 | ||
#define SYSCALL_EXIT 93 | ||
#define SYSCALL_EXIT_GROUP 94 | ||
#define SYSCALL_SET_TID_ADDR 96 | ||
#define SYSCALL_REBOOT 142 | ||
#define SYSCALL_GETTIMEOFDAY 169 | ||
#define SYSCALL_GETPID 172 | ||
#define SYSCALL_GETEUID 175 | ||
#define SYSCALL_SOCKET 198 | ||
#define SYSCALL_SENDTO 206 | ||
#define SYSCALL_RECVFROM 207 | ||
#define SYSCALL_BRK 214 | ||
#define SYSCALL_EXECV 221 | ||
|
||
// Not available on AArch64: | ||
// | ||
// - SYSCALL_OPEN | ||
// - SYSCALL_DUP2 | ||
// - SYSCALL_ARCH_PRCTL | ||
|
||
#endif | ||
|
||
// Not available outside ArvernOS: | ||
#define SYSCALL_TEST 348 | ||
#define SYSCALL_GETHOSTBYNAME2 349 | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#include "process.h" | ||
|
||
#include <proc/logging.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#define PUSH_TO_STACK(stack, type, value) \ | ||
stack = (char*)stack - sizeof(type); \ | ||
*((type*)stack) = value | ||
|
||
static process_t* current_process = NULL; | ||
|
||
process_t* process_create_root() | ||
{ | ||
current_process = (process_t*)malloc(sizeof(process_t)); | ||
current_process->pid = 0; | ||
|
||
return current_process; | ||
} | ||
|
||
process_t* process_get_current() | ||
{ | ||
return current_process; | ||
} | ||
|
||
process_t* process_exec(uint8_t* image, const char* name, char* const argv[]) | ||
{ | ||
PROC_DEBUG("image=%p name=%s", image, name); | ||
|
||
if (current_process == NULL) { | ||
current_process = process_create_root(); | ||
} else { | ||
elf_unload(current_process->elf); | ||
free(current_process->elf); | ||
// Free the current name as we'll update it right after. | ||
free(current_process->name); | ||
// Give a new PID to the current process. | ||
current_process->pid++; | ||
} | ||
|
||
// Load ELF in current process. | ||
// TODO: Handle the case where the image isn't a valid/supported ELF. | ||
elf_header_t* elf = elf_load(image); | ||
current_process->elf = elf; | ||
|
||
// Set current process name. | ||
current_process->name = strdup(name); | ||
|
||
memset(current_process->user_heap, 0, USER_HEAP_SIZE); | ||
current_process->user_brk = (uintptr_t)current_process->user_heap; | ||
|
||
// Set up user stack. | ||
memset(current_process->user_stack, 0, USER_STACK_SIZE); | ||
|
||
// User stack: | ||
// | ||
// +--------+ user_stack[0] | ||
// | | | ||
// | | | ||
// ^ | | | ||
// | | | | ||
// sp --->| | user_stack[USER_STACK_TOP] | ||
// | | | ||
// | | | ||
// | | | ||
// | | | ||
// buf --->| | user_stack[USER_STACK_BUF] | ||
// | | | | ||
// v | | | ||
// | | | ||
// | | | ||
// +--------+ user_stack[USER_STACK_SIZE] | ||
// | ||
void* stack = (void*)¤t_process->user_stack[USER_STACK_TOP]; | ||
char* buf = (char*)¤t_process->user_stack[USER_STACK_BUF]; | ||
|
||
size_t off = 0; | ||
|
||
// auxv | ||
uint8_t rand_bytes[16] = { | ||
0xaa, 0xc0, 0xff, 0xee, 0xc0, 0xff, 0xee, 0xc0, | ||
0xff, 0xee, 0xc0, 0xff, 0xee, 0xc0, 0xff, 0xee, | ||
}; | ||
memcpy(&buf[off], rand_bytes, sizeof(rand_bytes)); | ||
char* rand_ptr = &buf[off]; | ||
off += sizeof(rand_bytes); | ||
|
||
// aux: AT_NULL | ||
PUSH_TO_STACK(stack, uintptr_t, (uintptr_t)NULL); | ||
PUSH_TO_STACK(stack, uintptr_t, (uintptr_t)NULL); | ||
// aux: AT_RANDOM | ||
PUSH_TO_STACK(stack, uintptr_t, (uintptr_t)rand_ptr); | ||
PUSH_TO_STACK(stack, uint64_t, 25); | ||
// aux: AT_PAGESZ | ||
PUSH_TO_STACK(stack, uint64_t, 4096); | ||
PUSH_TO_STACK(stack, uint64_t, 6); | ||
// aux: AT_PHNUM | ||
PUSH_TO_STACK(stack, uint64_t, elf->ph_num); | ||
PUSH_TO_STACK(stack, uint64_t, 5); | ||
// aux: AT_PHENT | ||
PUSH_TO_STACK(stack, uint64_t, elf->ph_size); | ||
PUSH_TO_STACK(stack, uint64_t, 4); | ||
// aux: AT_PHDR | ||
PUSH_TO_STACK(stack, uintptr_t, (uintptr_t)image + elf->ph_offset); | ||
PUSH_TO_STACK(stack, uint64_t, 3); | ||
|
||
// envp | ||
PUSH_TO_STACK(stack, uintptr_t, (uintptr_t)NULL); | ||
|
||
// We need both `argc` and `argv` so we start by retrieving `argc`. | ||
uint64_t argc = 0; | ||
while (argv[argc]) { | ||
argc++; | ||
} | ||
|
||
// argv | ||
PUSH_TO_STACK(stack, uintptr_t, (uintptr_t)NULL); | ||
|
||
for (int i = argc - 1; i >= 0; i--) { | ||
size_t len = strlen(argv[i]); | ||
// Copy argv[i] to high address in initial process stack. | ||
strncpy(&buf[off], argv[i], len + 1); | ||
|
||
// TODO: Check that we have enough space to write argv. | ||
|
||
// Add address of argv[i] to the stack. | ||
PUSH_TO_STACK(stack, uintptr_t, (uintptr_t)&buf[off]); | ||
|
||
off += len + 1; | ||
} | ||
|
||
// argc | ||
PUSH_TO_STACK(stack, uint64_t, argc); | ||
|
||
current_process->user_rsp = (uintptr_t)stack; | ||
|
||
return current_process; | ||
} |
Oops, something went wrong.