Skip to content

Commit 1fc9138

Browse files
committed
cleanup
1 parent e1841b0 commit 1fc9138

15 files changed

Lines changed: 201 additions & 244 deletions

File tree

Makefile

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,30 @@
11
CC := clang-18
2-
32
BIN := kexp.bin
4-
53
SRC_DIR := src
64
BUILD_DIR := build
75

8-
CFLAGS := -O3 -fPIE -ffreestanding -nostdlib -nostartfiles \
9-
-fno-omit-frame-pointer -Iinclude -Wall -Wextra -Werror \
10-
-Wno-unused-variable -Wno-unused-function -Wno-unused-but-set-variable -Wno-uninitialized -Wno-int-conversion
11-
12-
OBJS := \
13-
$(BUILD_DIR)/api.o \
14-
$(BUILD_DIR)/main.o \
15-
$(BUILD_DIR)/iommu.o \
16-
$(BUILD_DIR)/logger.o \
17-
$(BUILD_DIR)/loader.o \
18-
$(BUILD_DIR)/kernel.o \
19-
$(BUILD_DIR)/syscalls.o \
6+
CFLAGS := -O3 -Iinclude \
7+
-fPIE -fcommon -fno-omit-frame-pointer -fno-zero-initialized-in-bss \
8+
-ffreestanding -nostdlib -nostartfiles \
9+
-Wall -Wextra -Werror -Wno-int-conversion
2010

21-
all: $(BUILD_DIR)/$(BIN)
22-
23-
$(BUILD_DIR):
24-
mkdir -p $(BUILD_DIR)
11+
SRCS := $(wildcard $(SRC_DIR)/*.c)
12+
OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(SRCS)) $(BUILD_DIR)/syscalls.o
2513

26-
$(BUILD_DIR)/syscalls.S: $(SRC_DIR)/syscalls.S $(SRC_DIR)/syscalls.defs
27-
cp $< $@
14+
.PHONY: all clean
2815

29-
awk -F, '\
30-
BEGIN { \
31-
print "\n"; \
32-
} \
33-
{ \
34-
print ".global " $$1 "_stub"; \
35-
print $$1 "_stub:"; \
36-
print " mov rax, " $$2; \
37-
print " jmp syscall"; \
38-
print ""; \
39-
}' $(word 2,$^) >> $@
16+
all: $(BUILD_DIR)/$(BIN)
4017

41-
$(BUILD_DIR)/syscalls.ld: $(SRC_DIR)/syscalls.defs
42-
awk -F, '{print "PROVIDE(" $$1 " = " $$1 "_stub);"}' $< > $@
18+
_ := $(shell mkdir -p $(BUILD_DIR))
4319

44-
$(BUILD_DIR)/syscalls.o: $(BUILD_DIR)/syscalls.S
20+
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
4521
$(CC) $(CFLAGS) -c $< -o $@
4622

47-
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR)
23+
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.S
4824
$(CC) $(CFLAGS) -c $< -o $@
4925

50-
$(BUILD_DIR)/$(BIN): $(OBJS) script.ld $(BUILD_DIR)/syscalls.ld
51-
$(CC) $(CFLAGS) $(OBJS) -o $@ -Tscript.ld -T$(BUILD_DIR)/syscalls.ld
26+
$(BUILD_DIR)/$(BIN): $(OBJS) script.ld
27+
$(CC) $(CFLAGS) -Tscript.ld $(OBJS) -o $@
5228

5329
clean:
5430
rm -rf $(BUILD_DIR)

include/api.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#ifndef API_H
22
#define API_H
33

4-
#include "syscalls.h"
54
#include "types.h"
65

7-
#define API(ret, name, args) COMMON ret(*name) args;
6+
#define API(id, ret, name, args) ret name args;
7+
#include "syscalls-imports.h"
8+
#undef API
9+
10+
#define API(ret, name, args) ret(*name) args;
811
#include "libc-imports.h"
912
#include "libkernel-imports.h"
1013
#undef API

include/kernel.h

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,14 @@
99

1010
#define CPU_PDE_PRESENT_SHIFT 0
1111
#define CPU_PDE_PRESENT_MASK 1
12-
#define CPU_PDE_RW_SHIFT 1
13-
#define CPU_PDE_RW_MASK 1
14-
#define CPU_PDE_USER_SHIFT 2
15-
#define CPU_PDE_USER_MASK 1
16-
#define CPU_PDE_WRITE_THROUGH_SHIFT 3
17-
#define CPU_PDE_WRITE_THROUGH_MASK 1
18-
#define CPU_PDE_CACHE_DISABLE_SHIFT 4
19-
#define CPU_PDE_CACHE_DISABLE_MASK 1
20-
#define CPU_PDE_ACCESSED_SHIFT 5
21-
#define CPU_PDE_ACCESSED_MASK 1
22-
#define CPU_PDE_DIRTY_SHIFT 6
23-
#define CPU_PDE_DIRTY_MASK 1
2412
#define CPU_PDE_PS_SHIFT 7
2513
#define CPU_PDE_PS_MASK 1
26-
#define CPU_PDE_GLOBAL_SHIFT 8
27-
#define CPU_PDE_GLOBAL_MASK 1
28-
#define CPU_PDE_XOTEXT_SHIFT 58
29-
#define CPU_PDE_XOTEXT_MASK 1
30-
#define CPU_PDE_PROTECTION_KEY_SHIFT 59
31-
#define CPU_PDE_PROTECTION_KEY_MASK 0x7FFF
32-
#define CPU_PDE_EXECUTE_DISABLE_SHIFT 63
33-
#define CPU_PDE_EXECUTE_DISABLE_MASK 1
3414
#define CPU_PG_PHYS_FRAME 0x000ffffffffff000
3515
#define CPU_PG_PS_FRAME 0x000fffffffe00000
36-
#define CPU_PDE_FIELD(pde, field) ((pde >> field##_SHIFT) & field##_MASK)
16+
#define CPU_PDE_FIELD(pde, field) ((pde >> CPU_PDE_##field##_SHIFT) & CPU_PDE_##field##_MASK)
3717

38-
COMMON kaddrs_t kaddrs;
39-
COMMON karw_ctx_t karw_ctx;
18+
kaddrs_t kaddrs;
19+
karw_ctx_t karw_ctx;
4020

4121
void init_karw(payload_args_t *args);
4222
void init_kaddrs(uintptr_t allproc);

include/loader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ typedef struct {
3333
} loader_args_t;
3434

3535
typedef struct {
36-
uintptr_t base;
3736
size_t size;
37+
uintptr_t base;
3838
uintptr_t entry;
39-
uintptr_t args_map;
39+
uintptr_t args_base;
4040
loader_args_t args;
4141
} loader_ctx_t;
4242

43-
COMMON loader_ctx_t loader_ctx;
43+
loader_ctx_t loader_ctx;
4444

4545
int init_loader(char *elfldr_ptr, size_t size);
4646
int init_loader_args();

include/syscalls-imports.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
API(0x3, ssize_t, read, (int fd, void *buf, size_t nbytes))
2+
API(0x4, ssize_t, write, (int fd, const void *buf, size_t nbytes))
3+
API(0x5, int, open, (const char *path, int flags, ...))
4+
API(0x6, int, close, (int fd))
5+
API(0x14, pid_t, getpid, (void))
6+
API(0x49, int, munmap, (void *addr, size_t len))
7+
API(0x4a, int, mprotect, (void *addr, size_t len, int prot))
8+
API(0x5a, int, dup2, (int oldd, int newd))
9+
API(0x61, int, socket, (int domain, int type, int protocol))
10+
API(0x69, int, setsockopt,
11+
(int s, int level, int optname, const void *optval, socklen_t optlen))
12+
API(0x1dd, void *, mmap,
13+
(void *addr, size_t len, int prot, int flags, int fd, off_t offset))
14+
API(0x24f, int, dlsym,
15+
(SceKernelModule handle, const char *symbol, void **addrp))
16+
API(0x2af, int, pipe2, (int fildes[2], int flags))

include/syscalls.h

Lines changed: 0 additions & 22 deletions
This file was deleted.

include/types.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
#include <stdint.h>
66
#include <sys/types.h>
77

8-
#define PACKED __attribute__((packed))
9-
#define COMMON __attribute__((common))
10-
#define DATA __attribute__((section(".data")))
11-
#define ENTRY __attribute__((section(".entry")))
128
#define ALIGN_UP(addr, align) (addr + (align - 1)) & ~(align - 1)
139
#define ALIGN_DOWN(addr, align) addr & ~(align - 1)
1410

@@ -86,6 +82,7 @@ typedef struct {
8682
uintptr_t softc;
8783
uintptr_t krodata;
8884
uintptr_t allproc;
85+
uintptr_t fdt_ofiles;
8986
} kaddrs_t;
9087

9188
#endif

src/api.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
#include "api.h"
22

3-
#define API(ret, name, args) DATA ret(*name) args;
3+
#define API(ret, name, args) ret(*name) args = 0;
44
#include "libc-imports.h"
55
#include "libkernel-imports.h"
66
#undef API
77

8-
void init_libkernel_api() {
8+
static inline void *resolve_symbol(SceKernelModule handle, const char *name) {
99
void *addrp;
10+
if (dlsym(handle, name, &addrp))
11+
__builtin_trap();
12+
return addrp;
13+
}
1014

15+
void init_libkernel_api() {
1116
#define API(ret, name, args) \
12-
do { \
13-
if (dlsym(LIBKERNEL_HANDLE, #name, &addrp) == -1) \
14-
__builtin_trap(); \
15-
name = (ret(*) args)(addrp); \
16-
} while (0);
17+
name = (ret(*) args)resolve_symbol(LIBKERNEL_HANDLE, #name);
1718

1819
#include "libkernel-imports.h"
1920

2021
#undef API
2122
}
2223

2324
void init_libc_api() {
24-
void *addrp;
25-
2625
#define API(ret, name, args) \
27-
do { \
28-
if (dlsym(LIBC_HANDLE, #name, &addrp) == -1) \
29-
__builtin_trap(); \
30-
name = (ret(*) args)(addrp); \
31-
} while (0);
26+
name = (ret(*) args)resolve_symbol(LIBC_HANDLE, #name);
3227

3328
#include "libc-imports.h"
3429

0 commit comments

Comments
 (0)