Skip to content

Commit

Permalink
fix buffer overflow in log
Browse files Browse the repository at this point in the history
  • Loading branch information
tcfw committed Oct 11, 2023
1 parent 4d6e35d commit b92a078
Show file tree
Hide file tree
Showing 33 changed files with 464 additions and 234 deletions.
4 changes: 3 additions & 1 deletion arch/aarch64/arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ enableFP()
// Power down
void arch_poweroff()
{
__asm__ volatile("ldr w0, =0x84000008");
__asm__ volatile("hvc 0");
}

// Get the current PE ID
Expand Down Expand Up @@ -77,7 +79,7 @@ static uint64_t psci_cpu_on(uint64_t affinity, uint64_t entrypoint)

__asm__ volatile("mov x1, %0" ::"r"(affinity));
__asm__ volatile("mov x2, %0" ::"r"(entrypoint));
__asm__ volatile("ldr w0, =0xc4000003");
__asm__ volatile("ldr x0, =0xc4000003");
__asm__ volatile("mov x3, 0");
__asm__ volatile("hvc 0");
__asm__ volatile("mov %0, x0"
Expand Down
24 changes: 17 additions & 7 deletions arch/aarch64/entry.S
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#include "multiboot.S"

_start:
.globl _start
MRS x0, MPIDR_EL1
AND x0, x0, #0xFFFF // Mask off to leave Aff0 and Aff1
CBZ x0, boot // If not *.*.0.0, then wait
MRS x10, MPIDR_EL1
AND x10, x10, #0xFFFF // Mask off to leave Aff0 and Aff1
CBZ x10, boot // If not *.*.0.0, then wait
B secondary_boot

boot:
// Set Stackpointer
LDR x0, =stack
MOV sp, x0
LDR x10, =stack
MOV sp, x10

ISB

Expand All @@ -22,6 +20,18 @@ halt_loop:
WFE
B halt_loop

address_xlate_read:
.globl address_xlate_read
AT S1E1R, X0
MRS X0, PAR_EL1
RET

address_xlate_write:
.globl address_xlate_write
AT S1E1W, X0
MRS X0, PAR_EL1
RET

.global secondary_boot
secondary_boot:
WFE
Expand Down
9 changes: 5 additions & 4 deletions arch/aarch64/include/kernel/paging.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

#define ARCH_PAGE_SIZE 4096
#define RAM_MAX (0x40000000 + 0x20000000)
#define L3_BLOCK_SIZE ARCH_PAGE_SIZE
#define L2_BLOCK_SIZE ((1ULL << 21) - 1)
#define L1_BLOCK_SIZE ((1ULL << 30) - 1)
#define L0_BLOCK_SIZE ((1ULL << 39) - 1)

#define L3_BLOCK_SIZE (ARCH_PAGE_SIZE)
#define L2_BLOCK_SIZE (2 * 1024 * 1024)
#define L1_BLOCK_SIZE (1024 * 1024 * 1024)
#define L0_BLOCK_SIZE (512 * 1024 * 1024 * 1024)

#define TCR_T0SZ_SHIFT 0ULL
#define TCR_TSZ_BITS 6ULL
Expand Down
12 changes: 11 additions & 1 deletion arch/aarch64/include/kernel/regions.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#ifndef _ARCH_REGIONS_H
#define _ARCH_REGIONS_H

#define DEVICE_REGION (0xF000000000ULL)
#ifndef KERNEL_REGION
#define KERNEL_REGION (0x40000000ULL)
#endif

#ifndef DEVICE_DESCRIPTOR_REGION
#define DEVICE_DESCRIPTOR_REGION (0x7E000000000ULL)
#endif

#ifndef DEVICE_REGION
#define DEVICE_REGION (0x7D000000000ULL)
#endif

#endif
23 changes: 16 additions & 7 deletions arch/aarch64/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@ ENTRY (_start)
SECTIONS
{
. = 0x40000000;
/* . = 0xffff000000000000; High mem kernel, otherwise would be limited to 1GB user space, but needs a bootloader to set up virtual memory first */
/* . = 0x7F000000000; */
/* High mem kernel, otherwise would be limited to
1GB user space, but needs a bootloader to set up
virtual memory first */

kernelstart = .;

.boot : { KEEP(*(.multiboot*)) KEEP(*(.boot*)) }
.text : { *(.text*) *(.rodata*) }
.data : { *(.data*) }
.text : {
KEEP(*(.boot*))

*(.text*)
*(.rodata*)

__start_init = .;
.init : { *(.init*) }
__stop_init = .;
__start_init = .;
KEEP(*(.init*))
__stop_init = .;
}

.data : { *(.data*) }

.bss : { *(.bss*) *(COMMON*) }

. = ALIGN(8);
. += 0x20000; /* 128kB of stack memory */
stack = .;
Expand Down
2 changes: 1 addition & 1 deletion arch/aarch64/make.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ KERNEL_ARCH_CPPFLAGS=
KERNEL_ARCH_LDFLAGS=
KERNEL_ARCH_LIBS=

ARCH_BUILD_INFO=Arch: ARMv8.4
ARCH_BUILD_INFO=Arch: ARMv8.4-a

C_SRC_FILES:=$(wildcard $(ARCHDIR)/*.c)
S_SRC_FILES:=$(wildcard $(ARCHDIR)/*.S)
Expand Down
45 changes: 45 additions & 0 deletions arch/aarch64/spinlock.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Acquire lock using Compare and Swap instruction.
*
* Compare for 0 with acquire semantics, and swap 1. If failed to acquire, use
* load exclusive semantics to monitor the address and enter WFE.
*
* void spin_lock(spinlock_t *lock);
*/
spinlock_acquire:
.globl spinlock_acquire
/*
mov w2, #1
1: mov w1, wzr
2: casa w1, w2, [x0]
cbz w1, 3f
ldxr w1, [x0]
cbz w1, 2b
wfe
b 1b
3:
ret
*/

mov w2, #1
sevl
l1: wfe
l2: ldaxr w1, [x0]
cbnz w1, l1
stxr w1, w2, [x0]
cbnz w1, l2
ret

/*
* Release lock previously acquired by spin_lock.
*
* Use store-release to unconditionally clear the spinlock variable.
* Store operation generates an event to all cores waiting in WFE
* when address is monitored by the global monitor.
*
* void spin_unlock(spinlock_t *lock);
*/
spinlock_release:
.globl spinlock_release
stlr wzr, [x0]
ret
22 changes: 0 additions & 22 deletions arch/aarch64/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,6 @@ void spinlock_init(spinlock_t *lock)
*lock = 0;
}

// Acquire the lock
void spinlock_acquire(spinlock_t *lock)
{
__asm__ volatile(
"mov w2, #1 \n\t"
"sevl \n\t"
"l1: wfe \n\t"
"l2: ldaxr w1, [%0] \n\t"
"cbnz w1, l1 \n\t"
"stxr w1, w2, [%0] \n\t"
"cbnz w1, l2 \n\t"
: "=r"(lock));
}

// Release the lock
void spinlock_release(spinlock_t *lock)
{
__asm__ volatile(
"stlr wzr, [%0]"
: "=r"(lock));
}

// Check if the lock is currently held
bool spinlock_is_locked(spinlock_t *lock)
{
Expand Down
10 changes: 5 additions & 5 deletions arch/aarch64/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct pl011
uint32_t stop_bits;
};

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)
{
Expand Down Expand Up @@ -190,13 +192,11 @@ void terminal_writestring(char *str)
terminal_putchar(*str++);
}

spinlock_t log_lock;

void terminal_log(char *str)
{
spinlock_acquire(&log_lock);

static char buf[8];
static char buf[24];

struct clocksource_t *cs = clock_first(CS_GLOBAL);

Expand All @@ -220,14 +220,14 @@ void terminal_write(const char *data, size_t size)

void terminal_logf(char *fmt, ...)
{
static char buf[2048];
static spinlock_t buflock = 0;
static char buf[2048];
spinlock_acquire(&buflock);

__builtin_va_list argp;
__builtin_va_start(argp, fmt);

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

__builtin_va_end(argp);

Expand Down
Loading

0 comments on commit b92a078

Please sign in to comment.