Compare commits
7 commits
cea6d7505f
...
cb0ad6a2a5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb0ad6a2a5 | ||
|
|
5c243e3f81 | ||
|
|
c36a22c5c8 | ||
|
|
70f78dfa26 | ||
|
|
e8b2c1ae56 | ||
|
|
76037c1c18 | ||
|
|
2a63fd9d1c |
8 changed files with 62 additions and 15 deletions
|
|
@ -8,11 +8,11 @@ AllowShortFunctionsOnASingleLine: false
|
||||||
AlwaysBreakTemplateDeclarations: true
|
AlwaysBreakTemplateDeclarations: true
|
||||||
BreakConstructorInitializers: BeforeComma
|
BreakConstructorInitializers: BeforeComma
|
||||||
AlignConsecutiveDeclarations:
|
AlignConsecutiveDeclarations:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
AcrossEmptyLines: false
|
AcrossEmptyLines: false
|
||||||
AcrossComments: false
|
AcrossComments: false
|
||||||
AlignCompound: false
|
AlignCompound: false
|
||||||
AlignFunctionPointers: false
|
AlignFunctionPointers: true
|
||||||
PadOperators: false
|
PadOperators: false
|
||||||
AlignConsecutiveMacros: true
|
AlignConsecutiveMacros: true
|
||||||
AllowShortCaseLabelsOnASingleLine: true
|
AllowShortCaseLabelsOnASingleLine: true
|
||||||
|
|
|
||||||
8
Makefile
8
Makefile
|
|
@ -26,11 +26,13 @@ ASFLAGS = -march=rv64gc -mabi=lp64
|
||||||
LDFLAGS = -Tkern/kernel.ld
|
LDFLAGS = -Tkern/kernel.ld
|
||||||
LDFLAGS += -m elf64lriscv
|
LDFLAGS += -m elf64lriscv
|
||||||
|
|
||||||
CFLAGS = -Wall -Werror -O
|
# See: https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/RISC-V-Options.html#index-march-14
|
||||||
CFLAGS += -Wno-unused-result
|
|
||||||
CFLAGS += -Wno-unused-variable
|
|
||||||
CFLAGS += -mcmodel=medany
|
CFLAGS += -mcmodel=medany
|
||||||
CFLAGS += -march=rv64gc -mabi=lp64
|
CFLAGS += -march=rv64gc -mabi=lp64
|
||||||
|
|
||||||
|
CFLAGS += -Wall -Werror -O
|
||||||
|
CFLAGS += -Wno-unused-result
|
||||||
|
CFLAGS += -Wno-unused-variable
|
||||||
CFLAGS += -ffreestanding
|
CFLAGS += -ffreestanding
|
||||||
CFLAGS += -fno-common
|
CFLAGS += -fno-common
|
||||||
CFLAGS += -nostdlib
|
CFLAGS += -nostdlib
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include "badrand.h"
|
#include "badrand.h"
|
||||||
|
#include "stdio.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.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
|
#undef PRAND_BUILD_SEED
|
||||||
|
|
|
||||||
|
|
@ -45,4 +45,6 @@ uint64_t badrand_range(uint64_t min, uint64_t max);
|
||||||
*/
|
*/
|
||||||
void badrand_buf(char *buf, size_t len);
|
void badrand_buf(char *buf, size_t len);
|
||||||
|
|
||||||
|
int looks_random(char *buf, size_t len);
|
||||||
|
|
||||||
#endif // BADRAND_H
|
#endif // BADRAND_H
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#include <stddef.h>
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void hexdump(const void *data, size_t size) {
|
void hexdump(const void *data, size_t size) {
|
||||||
|
|
|
||||||
|
|
@ -21,12 +21,17 @@ static inline void mmio_write32(uintptr_t addr, uint32_t value) {
|
||||||
*(volatile uint32_t *)addr = 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 low = mmio_read32(VIRT_RTC_BASE + RTC_TIME_LOW);
|
||||||
uint32_t high = mmio_read32(VIRT_RTC_BASE + RTC_TIME_HIGH);
|
uint32_t high = mmio_read32(VIRT_RTC_BASE + RTC_TIME_HIGH);
|
||||||
return ((uint64_t)high << 32) | low;
|
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) {
|
void rtc_alarm_set(uint64_t ns) {
|
||||||
mmio_write32(VIRT_RTC_BASE + RTC_ALARM_HIGH, ns >> 32);
|
mmio_write32(VIRT_RTC_BASE + RTC_ALARM_HIGH, ns >> 32);
|
||||||
mmio_write32(VIRT_RTC_BASE + RTC_ALARM_LOW, ns & 0xffffffff);
|
mmio_write32(VIRT_RTC_BASE + RTC_ALARM_LOW, ns & 0xffffffff);
|
||||||
|
|
|
||||||
11
kern/rtc.h
11
kern/rtc.h
|
|
@ -3,7 +3,16 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#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);
|
void rtc_alarm_set(uint64_t ns);
|
||||||
uint64_t rtc_alarm_read(void);
|
uint64_t rtc_alarm_read(void);
|
||||||
|
|
|
||||||
16
kern/start.c
16
kern/start.c
|
|
@ -1,11 +1,11 @@
|
||||||
#include <endian.h>
|
|
||||||
#include <badrand.h>
|
|
||||||
#include <hexdump.h>
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <badrand.h>
|
||||||
#include <banner.h>
|
#include <banner.h>
|
||||||
#include <buddy.h>
|
#include <buddy.h>
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
#include <endian.h>
|
||||||
#include <freelist.h>
|
#include <freelist.h>
|
||||||
|
#include <hexdump.h>
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
#include <panic.h>
|
#include <panic.h>
|
||||||
#include <proc.h>
|
#include <proc.h>
|
||||||
|
|
@ -49,7 +49,7 @@ void start() {
|
||||||
memory_sweep(heap_start, heap_end);
|
memory_sweep(heap_start, heap_end);
|
||||||
buddy_init(heap_start, heap_end);
|
buddy_init(heap_start, heap_end);
|
||||||
spinlock_init(&sl);
|
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]);
|
for (int i = 0; i < banner_len; i++) uart_putc(banner[i]);
|
||||||
__sync_synchronize();
|
__sync_synchronize();
|
||||||
hold = 0;
|
hold = 0;
|
||||||
|
|
@ -83,8 +83,8 @@ void start() {
|
||||||
buddy_free(mem);
|
buddy_free(mem);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
uint64_t time = rtc_read_time();
|
uint64_t time = rtc_time_read();
|
||||||
time = rtc_read_time();
|
time = rtc_time_read();
|
||||||
rtc_alarm_set(time + 3000000000);
|
rtc_alarm_set(time + 3000000000);
|
||||||
uint64_t alrm = rtc_alarm_read();
|
uint64_t alrm = rtc_alarm_read();
|
||||||
assert(alrm > time);
|
assert(alrm > time);
|
||||||
|
|
@ -100,8 +100,10 @@ void start() {
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
char buffer[128];
|
char buffer[128];
|
||||||
|
memset(buffer, 0, 128);
|
||||||
|
assert(!looks_random(buffer, 128));
|
||||||
badrand_buf(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");
|
kprintf("To exit qemu, press CTRL+a followed by x\n");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue