Compare commits

...

7 commits

Author SHA1 Message Date
Imbus
cb0ad6a2a5 Add linux-inspired rtc_class_ops struct 2025-10-01 03:40:11 +02:00
Imbus
5c243e3f81 Makefile: separate machine specific options, add link to gcc docs 2025-10-01 03:39:50 +02:00
Imbus
c36a22c5c8 Clang-format, align function pointers 2025-10-01 03:39:15 +02:00
Imbus
70f78dfa26 Better testing for random 2025-10-01 03:39:02 +02:00
Imbus
e8b2c1ae56 Rename rtc functions 2025-10-01 03:38:46 +02:00
Imbus
76037c1c18 badrand: a looks_random functions for a simple sanity check 2025-10-01 03:38:00 +02:00
Imbus
2a63fd9d1c Formatting 2025-10-01 03:37:44 +02:00
8 changed files with 62 additions and 15 deletions

View file

@ -8,11 +8,11 @@ AllowShortFunctionsOnASingleLine: false
AlwaysBreakTemplateDeclarations: true
BreakConstructorInitializers: BeforeComma
AlignConsecutiveDeclarations:
Enabled: true
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
AlignFunctionPointers: false
AlignFunctionPointers: true
PadOperators: false
AlignConsecutiveMacros: true
AllowShortCaseLabelsOnASingleLine: true

View file

@ -26,11 +26,13 @@ ASFLAGS = -march=rv64gc -mabi=lp64
LDFLAGS = -Tkern/kernel.ld
LDFLAGS += -m elf64lriscv
CFLAGS = -Wall -Werror -O
CFLAGS += -Wno-unused-result
CFLAGS += -Wno-unused-variable
# See: https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/RISC-V-Options.html#index-march-14
CFLAGS += -mcmodel=medany
CFLAGS += -march=rv64gc -mabi=lp64
CFLAGS += -Wall -Werror -O
CFLAGS += -Wno-unused-result
CFLAGS += -Wno-unused-variable
CFLAGS += -ffreestanding
CFLAGS += -fno-common
CFLAGS += -nostdlib

View file

@ -1,4 +1,5 @@
#include "badrand.h"
#include "stdio.h"
#include <stdint.h>
#include <string.h>
@ -49,4 +50,30 @@ void sbadrand(uint64_t s) {
}
}
/* Simple but dumb sanity check for randomness */
int looks_random(char *buf, size_t len) {
int counts[256] = {0};
int run = 1, max_run = 1;
for (size_t i = 0; i < len; i++) {
counts[(unsigned int)buf[i]]++;
if (i > 0 && buf[i] == buf[i - 1]) {
if (++run > max_run)
max_run = run;
} else {
run = 1;
}
}
for (int i = 0; i < 256; i++) {
if (counts[i] > (int)(len * 1 / 10))
return 0;
}
if (max_run > 16)
return 0;
return 1;
}
#undef PRAND_BUILD_SEED

View file

@ -45,4 +45,6 @@ uint64_t badrand_range(uint64_t min, uint64_t max);
*/
void badrand_buf(char *buf, size_t len);
int looks_random(char *buf, size_t len);
#endif // BADRAND_H

View file

@ -1,5 +1,5 @@
#include <stddef.h>
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
void hexdump(const void *data, size_t size) {

View file

@ -21,12 +21,17 @@ static inline void mmio_write32(uintptr_t addr, uint32_t value) {
*(volatile uint32_t *)addr = value;
}
uint64_t rtc_read_time(void) {
uint64_t rtc_time_read(void) {
uint32_t low = mmio_read32(VIRT_RTC_BASE + RTC_TIME_LOW);
uint32_t high = mmio_read32(VIRT_RTC_BASE + RTC_TIME_HIGH);
return ((uint64_t)high << 32) | low;
}
void rtc_time_set(uint64_t ns) {
mmio_write32(VIRT_RTC_BASE + RTC_TIME_LOW, ns);
mmio_write32(VIRT_RTC_BASE + RTC_TIME_HIGH, ns);
}
void rtc_alarm_set(uint64_t ns) {
mmio_write32(VIRT_RTC_BASE + RTC_ALARM_HIGH, ns >> 32);
mmio_write32(VIRT_RTC_BASE + RTC_ALARM_LOW, ns & 0xffffffff);

View file

@ -3,7 +3,16 @@
#include <stdint.h>
uint64_t rtc_read_time(void);
struct rtc_class_ops {
uint64_t (*read_time)(void);
void (*set_time)(uint64_t);
uint64_t (*read_alarm)(uint64_t);
void (*set_alarm)(uint64_t);
void (*alarm_irq_enable)(void);
};
uint64_t rtc_time_read(void);
void rtc_time_set(uint64_t ns);
void rtc_alarm_set(uint64_t ns);
uint64_t rtc_alarm_read(void);

View file

@ -1,11 +1,11 @@
#include <endian.h>
#include <badrand.h>
#include <hexdump.h>
#include <assert.h>
#include <badrand.h>
#include <banner.h>
#include <buddy.h>
#include <config.h>
#include <endian.h>
#include <freelist.h>
#include <hexdump.h>
#include <memory.h>
#include <panic.h>
#include <proc.h>
@ -49,7 +49,7 @@ void start() {
memory_sweep(heap_start, heap_end);
buddy_init(heap_start, heap_end);
spinlock_init(&sl);
sbadrand(rtc_read_time() ^ swap64(rtc_read_time()));
sbadrand(rtc_time_read() ^ swap64(rtc_time_read()));
for (int i = 0; i < banner_len; i++) uart_putc(banner[i]);
__sync_synchronize();
hold = 0;
@ -83,8 +83,8 @@ void start() {
buddy_free(mem);
}
{
uint64_t time = rtc_read_time();
time = rtc_read_time();
uint64_t time = rtc_time_read();
time = rtc_time_read();
rtc_alarm_set(time + 3000000000);
uint64_t alrm = rtc_alarm_read();
assert(alrm > time);
@ -100,8 +100,10 @@ void start() {
}
{
char buffer[128];
memset(buffer, 0, 128);
assert(!looks_random(buffer, 128));
badrand_buf(buffer, 128);
hexdump(buffer, 128);
assert(looks_random(buffer, 128));
}
kprintf("To exit qemu, press CTRL+a followed by x\n");