Skip to content

Commit

Permalink
xxhashing and use proc structs
Browse files Browse the repository at this point in the history
  • Loading branch information
tcfw committed May 8, 2024
1 parent 39199d9 commit 9381214
Show file tree
Hide file tree
Showing 36 changed files with 1,110 additions and 126 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
*.kernel
*.kernel.bin
*.o
.build
.build
.env.mk
.env.*.mk
11 changes: 6 additions & 5 deletions arch/aarch64/arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern void _start();
extern void halt_loop();

extern uintptr_t stack;
extern uintptr_t kernelstart;

#define CORE_BOOT_SP_SIZE (128 * 1024)

Expand Down Expand Up @@ -78,14 +79,14 @@ void wfi()
__asm__ volatile("wfi");
}

static uint64_t psci_cpu_on(uint64_t affinity, uint64_t entrypoint)
static uint64_t psci_cpu_on(uint64_t affinity, uintptr_t entrypoint)
{
uint64_t ret = 0;

__asm__ volatile("mov x1, %0" ::"r"(affinity));
__asm__ volatile("mov x2, %0" ::"r"(entrypoint));
__asm__ volatile("mov x3, xzr");
__asm__ volatile("mov x2, %0" :: "r"(entrypoint));
__asm__ volatile("mov x1, %0" :: "r"(affinity));
__asm__ volatile("ldr x0, =0xc4000003");
__asm__ volatile("mov x3, 0");
__asm__ volatile("hvc 0");
__asm__ volatile("mov %0, x0"
: "=r"(ret));
Expand All @@ -103,7 +104,7 @@ void wake_cores(void)
for (int i = 1; i < cpuN; i++)
{
cpu_spin_table[i] = (uintptr_t)page_alloc_s(CORE_BOOT_SP_SIZE) + CORE_BOOT_SP_SIZE;
int ret = psci_cpu_on(i, (uint64_t)_start);
int ret = psci_cpu_on(i, (uintptr_t)0x40400000);
if (ret < 0)
terminal_logf("failed to boot CPU: %x", ret);
}
Expand Down
15 changes: 10 additions & 5 deletions arch/aarch64/entry.S
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ _start:

//TODO(tcfw) check if in EL1

ADRP x1, init_tcr
MOV x0, #0x3C0
MSR DAIF, X0

ISB

ADRP x1, #init_tcr
ADD x1, x1, #:lo12:init_tcr
LDR x1, [x1]
MSR TCR_EL1, x1

ADRP x1, init_mair
ADRP x1, #init_mair
ADD x1, x1, #:lo12:init_mair
LDR x1, [x1]
MSR MAIR_EL1, x1

ADRP x2, init_vm_map
ADRP x2, #init_vm_map
ADD x2, x2, #:lo12:init_vm_map

ADRP x3, l0_vm_map_attrs
ADRP x3, #l0_vm_map_attrs
ADD x3, x3, #:lo12:l0_vm_map_attrs
LDR x3, [x3]

Expand Down Expand Up @@ -87,7 +92,7 @@ l1_vm_map:
boot:
.globl boot
// Set Stackpointer
ADRP x10, stack
ADRP x10, #stack
ADD x10, x10, #:lo12:stack
MOV sp, x10

Expand Down
1 change: 1 addition & 0 deletions arch/aarch64/include/kernel/stdint.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
typedef __uint128_t uint128_t;

typedef unsigned long long uintptr_t;

Expand Down
12 changes: 6 additions & 6 deletions arch/aarch64/irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extern unsigned long stack;
if (next != thread && didsave) \
{ \
thread = next; \
vm_set_table(thread->vm_table, thread->pid); \
vm_set_table(thread->process->vm.vm_table, thread->process->pid); \
set_to_context(&thread->ctx, trapFrame); \
}

Expand Down Expand Up @@ -237,9 +237,9 @@ void k_fiq_exphandler(unsigned int xrq)
switch (ESR_EXCEPTION_CLASS(xrq))
{
case ESR_EXCEPTION_INSTRUCTION_ABORT_LOWER_EL:
terminal_logf("instruction abort from EL0 addr 0x%x", far);
terminal_logf("instruction abort from EL0 addr 0x%X", far);
if (cls->rq.current_thread != 0)
terminal_logf("on PID 0x%x", cls->rq.current_thread->pid);
terminal_logf("on PID 0x%x", cls->rq.current_thread->process->pid);
// send SIGILL
break;
case ESR_EXCEPTION_INSTRUCTION_ABORT_SAME_EL:
Expand All @@ -254,7 +254,7 @@ void k_fiq_exphandler(unsigned int xrq)
case ESR_EXCEPTION_DATA_ABORT_LOWER_EL:
terminal_logf("data abort from EL0 addr 0x%x", far);
if (cls->rq.current_thread != 0)
terminal_logf("on PID 0x%x", cls->rq.current_thread->pid);
terminal_logf("on PID 0x%x", cls->rq.current_thread->process->pid);
// send SIGSEGV

break;
Expand All @@ -269,11 +269,11 @@ void k_fiq_exphandler(unsigned int xrq)
else
{
pa = vm_va_to_pa(vm_get_current_table(), far);
panicf("Unhandlable Data Abort: \n\tELR: 0x%x \n\tESR: 0x%x \n\tVirtual Address: 0x%x\n\tPhysical Address: 0x%x\n\tPAR: 0x%x", elr, xrq, far, pa, par);
panicf("Unhandlable Data Abort: \n\tELR: 0x%X \n\tESR: 0x%x \n\tVirtual Address: 0x%X\n\tPhysical Address: 0x%X\n\tPAR: 0x%X", elr, xrq, far, pa, par);
}
break;
default:
panicf("unhandled FIQ(0x%x) FAR: 0x%x", xrq, far);
panicf("unhandled FIQ(0x%x) FAR: 0x%X", xrq, far);
}
}

Expand Down
2 changes: 1 addition & 1 deletion arch/aarch64/make.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
KERNEL_ARCH_CFLAGS=-nostdlib -nostartfiles -std=gnu11 -march=armv8.4-a+fp
KERNEL_ARCH_CPPFLAGS=
KERNEL_ARCH_LDFLAGS=
KERNEL_ARCH_LDFLAGS=-m aarch64elf
KERNEL_ARCH_LIBS=

ARCH_ARCH=ARMv8.4-a
Expand Down
2 changes: 1 addition & 1 deletion arch/aarch64/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

void arch_thread_prep_switch(thread_t *thread)
{
uint64_t pid = (uint64_t)thread->pid;
uint64_t pid = (uint64_t)thread->process->pid;
__asm__ volatile("MSR TPIDRRO_EL0, %0" ::"r"(pid));
return;
}
Expand Down
14 changes: 7 additions & 7 deletions arch/aarch64/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <kernel/stdint.h>
#include <kernel/unistd.h>
#include <kernel/stdint.h>
#include <asm/unaligned.h>

#define PL011_DR_OFFSET 0x000
#define PL011_FR_OFFSET 0x018
Expand Down Expand Up @@ -42,16 +43,13 @@ static spinlock_t log_lock = 0;
// Write to a specific register given the offset
static void pl011_regwrite(const struct pl011 *dev, uint32_t offset, uint32_t data)
{
volatile uint32_t *addr = (void *)dev->base_address + offset;
*addr = data;
put_unaligned_le32(data, (void*)dev->base_address+offset);
}

// Read from a specific register given the offset
static uint32_t pl011_regread(const struct pl011 *dev, uint32_t offset)
{
volatile uint64_t *addr = (void *)dev->base_address + offset;

return *addr;
return get_unaligned_le32((void*)dev->base_address+offset);
}

// Wait for the busy state to clear
Expand Down Expand Up @@ -194,6 +192,8 @@ void terminal_writestring(char *str)

void terminal_log(char *str)
{
const char *eol="\r\n";

spinlock_acquire(&log_lock);

static char buf[24];
Expand All @@ -207,7 +207,7 @@ void terminal_log(char *str)
ksprintf(&buf[0], "[%.4f] ", val);
terminal_writestring(buf);
terminal_writestring(str);
terminal_putchar('\n');
terminal_writestring(eol);

spinlock_release(&log_lock);
}
Expand All @@ -227,7 +227,7 @@ void terminal_logf(char *fmt, ...)
__builtin_va_list argp;
__builtin_va_start(argp, fmt);

ksprintfz((char *)&buf[0], fmt, argp);
ksprintfz(&buf[0], fmt, argp);

__builtin_va_end(argp);

Expand Down
4 changes: 2 additions & 2 deletions arch/aarch64/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ vm_table *vm_get_current_table()
thread_t *thread = get_cls()->rq.current_thread;
if (thread != 0)
{
vm_table *uvm = thread->vm_table;
vm_table *uvm = thread->process->vm.vm_table;
if (uvm != 0)
return uvm;
}
Expand Down Expand Up @@ -721,7 +721,7 @@ void vm_init()
if (vm_map_region(kernel_vm_map, PHY_DEVICE_DESCRIPTOR_REGION, DEVICE_DESCRIPTOR_REGION, 0x100000 - 1, MEMORY_TYPE_KERNEL) < 0)
terminal_log("failed to map dbt region");

terminal_logf("Loaded kernel is mapping device region to 0x%x", vm_va_to_pa(kernel_vm_map, 7765300871168));
terminal_logf("Loaded kernel is mapping device region to 0x%X", vm_va_to_pa(kernel_vm_map, 7765300871168));

terminal_log("Loaded kernel VM map");
}
Expand Down
85 changes: 85 additions & 0 deletions include/asm/unaligned.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#ifndef _GENERIC_UNALIGNED_H
#define _GENERIC_UNALIGNED_H

#include <kernel/stdint.h>
#include <kernel/endian.h>

#define __packed __attribute__((packed))

#define __get_unaligned_t(type, ptr) ({ \
const struct { type x; } __packed * __pptr = (typeof(__pptr))(ptr); \
__pptr->x; \
})

#define __put_unaligned_t(type, val, ptr) do { \
struct { type x; } __packed * __pptr = (typeof(__pptr))(ptr); \
__pptr->x = (val); \
} while (0)

#define get_unaligned(ptr) __get_unaligned_t(typeof(*(ptr)), (ptr))
#define put_unaligned(val, ptr) __put_unaligned_t(typeof(*(ptr)), (val), (ptr))

static inline uint16_t get_unaligned_le16(const void *p)
{
return LITTLE_ENDIAN_UINT16(__get_unaligned_t(int16_t, p));
}

static inline uint32_t get_unaligned_le32(const void *p)
{
return LITTLE_ENDIAN_UINT32(__get_unaligned_t(int32_t, p));
}

static inline uint64_t get_unaligned_le64(const void *p)
{
return LITTLE_ENDIAN_UINT64(__get_unaligned_t(int64_t, p));
}

static inline void put_unaligned_le16(uint16_t val, void *p)
{
__put_unaligned_t(int16_t, LITTLE_ENDIAN_UINT16(val), p);
}

static inline void put_unaligned_le32(uint32_t val, void *p)
{
__put_unaligned_t(int32_t, LITTLE_ENDIAN_UINT32(val), p);
}

static inline void put_unaligned_le64(uint64_t val, void *p)
{
__put_unaligned_t(int64_t, LITTLE_ENDIAN_UINT64(val), p);
}

static inline uint16_t get_unaligned_be16(const void *p)
{
return BIG_ENDIAN_UINT16(__get_unaligned_t(int16_t, p));
}

static inline uint32_t get_unaligned_be32(const void *p)
{
return BIG_ENDIAN_UINT32(__get_unaligned_t(int32_t, p));
}

static inline uint64_t get_unaligned_be64(const void *p)
{
return BIG_ENDIAN_UINT64(__get_unaligned_t(int64_t, p));
}

static inline void put_unaligned_be16(uint16_t val, void *p)
{
__put_unaligned_t(int16_t, BIG_ENDIAN_UINT16(val), p);
}

static inline void put_unaligned_be32(uint32_t val, void *p)
{
__put_unaligned_t(int32_t, BIG_ENDIAN_UINT32(val), p);
}

static inline void put_unaligned_be64(uint64_t val, void *p)
{
__put_unaligned_t(int64_t, BIG_ENDIAN_UINT64(val), p);
}

/* Allow unaligned memory access */
void allow_unaligned(void);

#endif
2 changes: 2 additions & 0 deletions include/errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
#define ERREXISTS (7)
#define ERREXHAUSTED (8)
#define ERRINUSE (9)
#define ERRAGAIN (10)
#define ERRINVALID (11)

#endif
11 changes: 10 additions & 1 deletion include/kernel/devices.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ typedef struct device_node_property_t
void *data;
} device_node_property_t;

int discover_devices(uintptr_t ddr);
struct dev_info {
uint32_t id;
char name[64];
char type[64];
uint64_t phy_bar;
uint64_t phy_bar_size;
uint64_t interrupts[5];
};

void discover_devices();

struct list_head *get_devices_head();

Expand Down
4 changes: 4 additions & 0 deletions include/kernel/devicetree.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ void *devicetree_first_with_device_type(char *type);

void *devicetree_find_node(char *path);

void *devicetree_get_next_node(void *current);

void *devicetree_get_root_node();

char *devicetree_get_property(void *node, char *propkey);

char *devicetree_get_root_property(char *propkey);
Expand Down
2 changes: 2 additions & 0 deletions include/kernel/endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define LITTLE_ENDIAN_UINT16(n) (n)
#define LITTLE_ENDIAN_UINT32(n) (n)
#define LITTLE_ENDIAN_UINT64(n) (n)
#define BIG_ENDIAN_UINT16(n) REVERSE_UINT16(n)
#define BIG_ENDIAN_UINT32(n) REVERSE_UINT32(n)
#define BIG_ENDIAN_UINT64(n) REVERSE_UINT64(n)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define LITTLE_ENDIAN_UINT16(n) REVERSE_UINT16(n)
#define LITTLE_ENDIAN_UINT32(n) REVERSE_UINT32(n)
#define LITTLE_ENDIAN_UINT64(n) REVERSE_UINT64(n)
#define BIG_ENDIAN_UINT16(n) (n)
#define BIG_ENDIAN_UINT32(n) (n)
#define BIG_ENDIAN_UINT64(n) (n)
Expand Down
32 changes: 32 additions & 0 deletions include/kernel/futex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef _KERNEL_FUTEX_H
#define _KERNEL_FUTEX_H

#include <kernel/sync.h>
#include <kernel/list.h>
#include <kernel/stdint.h>

union futex_key {
struct {
uintptr_t addr;
} private;
struct {
uintptr_t addr;
} shared;
struct {
uintptr_t ptr;
} both;
};

typedef struct futex_queue_t {
struct list_head list;

union futex_key key;
} futex_queue_t;

typedef struct futex_hb_t {
spinlock_t lock;
struct list_head chain;
} futex_hb_t;


#endif
Loading

0 comments on commit 9381214

Please sign in to comment.