1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-23 08:26:17 +04:00

Recalibrating timer

This commit is contained in:
gjz010 2021-03-04 19:59:15 +08:00
parent 7707a59b7e
commit fd8407a339
12 changed files with 435 additions and 26 deletions

3
.gitmodules vendored
View File

@ -4,3 +4,6 @@
[submodule "rboot"] [submodule "rboot"]
path = rboot path = rboot
url = https://github.com/rcore-os/rboot.git url = https://github.com/rcore-os/rboot.git
[submodule "submodules/rcore-fs"]
path = submodules/rcore-fs
url = https://github.com/rcore-os/rcore-fs

5
kernel/Cargo.lock generated
View File

@ -530,7 +530,6 @@ dependencies = [
[[package]] [[package]]
name = "rcore-fs" name = "rcore-fs"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/rcore-os/rcore-fs?rev=517af47#517af47ff3f632f05b91b9ea73a047aa8df363f3"
dependencies = [ dependencies = [
"filetime", "filetime",
"spin", "spin",
@ -540,7 +539,6 @@ dependencies = [
[[package]] [[package]]
name = "rcore-fs-devfs" name = "rcore-fs-devfs"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/rcore-os/rcore-fs?rev=517af47#517af47ff3f632f05b91b9ea73a047aa8df363f3"
dependencies = [ dependencies = [
"log", "log",
"rcore-fs", "rcore-fs",
@ -550,7 +548,6 @@ dependencies = [
[[package]] [[package]]
name = "rcore-fs-mountfs" name = "rcore-fs-mountfs"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/rcore-os/rcore-fs?rev=517af47#517af47ff3f632f05b91b9ea73a047aa8df363f3"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"log", "log",
@ -561,7 +558,6 @@ dependencies = [
[[package]] [[package]]
name = "rcore-fs-ramfs" name = "rcore-fs-ramfs"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/rcore-os/rcore-fs?rev=517af47#517af47ff3f632f05b91b9ea73a047aa8df363f3"
dependencies = [ dependencies = [
"log", "log",
"rcore-fs", "rcore-fs",
@ -571,7 +567,6 @@ dependencies = [
[[package]] [[package]]
name = "rcore-fs-sfs" name = "rcore-fs-sfs"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/rcore-os/rcore-fs?rev=517af47#517af47ff3f632f05b91b9ea73a047aa8df363f3"
dependencies = [ dependencies = [
"bitvec", "bitvec",
"log", "log",

View File

@ -63,11 +63,11 @@ pci = { git = "https://github.com/rcore-os/pci-rs", rev = "a4e7cea6" }
pc-keyboard = "0.5" pc-keyboard = "0.5"
rcore-console = { git = "https://github.com/rcore-os/rcore-console", rev = "b7bacf9", default-features = false } rcore-console = { git = "https://github.com/rcore-os/rcore-console", rev = "b7bacf9", default-features = false }
rcore-memory = { path = "../crate/memory" } rcore-memory = { path = "../crate/memory" }
rcore-fs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" } rcore-fs = { path = "../submodules/rcore-fs/rcore-fs" }
rcore-fs-sfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" } rcore-fs-sfs = { path = "../submodules/rcore-fs/rcore-fs-sfs" }
rcore-fs-ramfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" } rcore-fs-ramfs = { path = "../submodules/rcore-fs/rcore-fs-ramfs" }
rcore-fs-mountfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" } rcore-fs-mountfs = { path = "../submodules/rcore-fs/rcore-fs-mountfs" }
rcore-fs-devfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" } rcore-fs-devfs = { path = "../submodules/rcore-fs/rcore-fs-devfs" }
rlibc = "1.0" rlibc = "1.0"
smoltcp = { git = "https://github.com/rcore-os/smoltcp", rev = "5bd87c7c", default-features = false, features = ["alloc", "log", "ethernet", "proto-ipv4", "proto-igmp", "socket-icmp", "socket-udp", "socket-tcp", "socket-raw"] } smoltcp = { git = "https://github.com/rcore-os/smoltcp", rev = "5bd87c7c", default-features = false, features = ["alloc", "log", "ethernet", "proto-ipv4", "proto-igmp", "socket-icmp", "socket-udp", "socket-tcp", "socket-raw"] }
spin = "0.5" spin = "0.5"

363
kernel/Makefile.new Normal file
View File

@ -0,0 +1,363 @@
# Commands:
# make build Build
# make run Build and run in QEMU
# make justrun Run the last build
# make test Build and run in QEMU with specified program
# make justtest Run the last build with specified program
# make doc Generate docs
# make asm Open the deassemble file of the last build
# make header Open 'objdump -h' of the last build
# make addr2line Use addr2line to recover line info in backtrace
# make clean Clean
#
# Options:
# ARCH = x86_64 | riscv32 | riscv64 | aarch64 | mipsel
# D = int | in_asm | ... QEMU debug info
# MODE = debug | release
# LOG = off | error | warn | info | debug | trace
# USER_IMG = <sfsimg> Image path of user programs
# SMP = 1 | 2 | ... SMP core number
# GRAPHIC = on | off | console Enable/disable qemu graphical output, or print console to graphic output
# BOARD = qemu Run on QEMU
# | pc [ x86_64 only] Run on real pc
# | u540 [riscv64 only] Run on HiFive U540, use Sv39
# | raspi3 [aarch64 only] Run on Raspberry Pi 3 Model B/B+
# NET = on | off [ x86_64 only] Enable NIC
# PCI_PASSTHRU = 0000:00:00.1 [ x86_64 only] Passthrough the specified PCI device
# INIT = /bin/ls [riscv64 only] Run specified program instead of user shell
# EXTRA_NIC = on | off [ x86_64 only] Add an additional e1000 nic
# ACCEL = on | off [ x86_64 only] Enable/disable kvm/hvf acceleration
# HYPERVISOR = on | off [ x86_64/riscv64 only] Enable/disable the RVM hypervisor, and set ACCEL to on
# FEATURES = profile | ... Add additional features
ARCH ?= riscv64
MODE ?= release
LOG ?=
GRAPHIC ?= off
SMP ?= 4
PCI_PASSTHRU ?=
INIT ?=
EXTRA_NIC ?= off
ACCEL ?= off
HYPERVISOR ?= off
qemu := qemu-system-$(ARCH)
target := $(ARCH)
build_path := target/$(target)/$(MODE)
kernel := $(build_path)/rcore
kernel_img := $(build_path)/kernel.img
ESP := $(build_path)/esp
OVMF := ../rboot/OVMF.fd
user_dir := ../user
### export environments ###
export USER_IMG = $(user_dir)/build/$(ARCH).img
export USER_QCOW2 = $(user_dir)/build/$(ARCH).qcow2
ifeq ($(ARCH), aarch64)
BOARD ?= raspi3
else ifeq ($(ARCH), mipsel)
BOARD ?= malta
else
BOARD ?= qemu
endif
# currently only mipsel architecture need DTB linked to the kernel
ifeq ($(ARCH), mipsel)
DTB := src/arch/$(ARCH)/board/$(BOARD)/device.dtb
endif
### qemu options ###
qemu_opts := \
-smp cores=$(SMP)
qemu_net_opts :=
ifeq ($(ARCH), x86_64)
qemu_opts += \
-drive if=pflash,format=raw,readonly,file=$(OVMF) \
-drive format=raw,file=fat:rw:$(ESP) \
-serial mon:stdio \
-m 4G \
-device isa-debug-exit
ifeq ($(BOARD), qemu)
qemu_opts += \
-drive format=qcow2,file=$(USER_QCOW2),media=disk,cache=writeback,id=sfsimg,if=none \
-device ahci,id=ahci0 \
-device ide-hd,drive=sfsimg,bus=ahci0.0
endif
ifeq ($(PCI_PASSTHRU), )
qemu_net_opts += \
-netdev type=tap,id=net0,script=no,downscript=no \
-device e1000e,netdev=net0
else
qemu_net_opts += \
-device vfio-pci,host=$(PCI_PASSTHRU)
qemu_ui_opts += \
-vga std
endif
ifeq ($(EXTRA_NIC), on)
qemu_net_opts += \
-netdev type=tap,id=net1,script=no,downscript=no \
-device e1000e,netdev=net1
endif
else ifeq ($(ARCH), riscv32)
qemu_opts += \
-machine virt \
-serial mon:stdio \
-bios default \
-device loader,addr=0x80400000,file=$(kernel_img) \
-drive file=$(USER_QCOW2),format=qcow2,id=sfs \
-device virtio-blk-device,drive=sfs \
-device virtio-gpu-device \
-device virtio-mouse-device
qemu_net_opts += \
-netdev type=tap,id=net0,script=no,downscript=no \
-device virtio-net-device,netdev=net0
else ifeq ($(ARCH), riscv64)
ifeq ($(BOARD), u540)
qemu_opts += \
-machine virt \
-serial mon:stdio \
-bios ../tools/opensbi/fu540.elf \
-device loader,addr=0x80200000,file=$(kernel_img)
else
qemu_opts += \
-machine virt \
-serial mon:stdio \
-bios ../tools/opensbi/fw_jump.elf \
-device loader,addr=0x80200000,file=$(kernel_img) \
-drive file=$(USER_QCOW2),format=qcow2,id=sfs \
-device virtio-blk-device,drive=sfs \
-device virtio-gpu-device \
-device virtio-mouse-device
endif
qemu_net_opts += \
-netdev type=tap,id=net0,script=no,downscript=no \
-device virtio-net-device,netdev=net0
else ifeq ($(ARCH), aarch64)
# raspi must have at least 4 cpus
qemu_opts += \
-machine $(BOARD) \
-smp 4 \
-serial null -serial mon:stdio \
-kernel $(kernel_img) \
-drive file=$(USER_QCOW2),if=sd,format=qcow2,id=sfs
else ifeq ($(ARCH), mipsel)
ifeq ($(BOARD), malta)
qemu_opts += \
-machine $(BOARD) -device VGA \
-hda $(USER_QCOW2) \
-serial null -serial null -serial mon:stdio \
-kernel $(kernel_img)
endif
endif
ifdef D
qemu_opts += -d $(D)
endif
ifeq ($(GRAPHIC), off)
qemu_opts += -nographic
endif
ifeq ($(NET), on)
qemu_opts += $(qemu_net_opts)
qemu := sudo $(qemu)
endif
ifeq ($(HYPERVISOR), on)
ifeq ($(ARCH), x86_64)
FEATURES += hypervisor
ACCEL = on
endif
ifeq ($(ARCH), riscv64)
qemu_opts += -cpu rv64,x-h=true
endif
endif
ifeq ($(ACCEL), on)
ifeq ($(shell uname), Darwin)
qemu_opts += -accel hvf
else
qemu_opts += -accel kvm -cpu host
endif
endif
### build args ###
ifeq ($(GRAPHIC), off)
FEATURES += nographic
endif
ifeq ($(GRAPHIC), console)
FEATURES += consolegraphic
endif
ifneq ($(INIT), )
FEATURES += run_cmdline
endif
FEATURES += board_$(BOARD)
build_args := \
-Z build-std=core,alloc \
--target targets/$(target).json \
--features "$(FEATURES)"
ifeq ($(MODE), release)
build_args += --release
endif
### prefix ###
ifeq ($(ARCH), x86_64)
ifeq ($(shell uname), Darwin)
prefix := x86_64-linux-musl-
endif
else ifeq ($(ARCH), riscv32)
prefix := riscv64-linux-musl-
else ifeq ($(ARCH), riscv64)
prefix := riscv64-unknown-linux-musl-
else ifeq ($(ARCH), mipsel)
prefix ?= mipsel-linux-musln32-
else ifeq ($(ARCH), aarch64)
prefix ?= aarch64-linux-musl-
endif
gdb := riscv64-unknown-linux-musl-gdb
sysroot := $(shell rustc --print sysroot)
objdump := $(shell find $(sysroot) -name llvm-objdump) -print-imm-hex
objcopy := $(shell find $(sysroot) -name llvm-objcopy)
strip := $(shell find $(sysroot) -name llvm-strip)
dtc := dtc
hostcc := gcc
.PHONY: all clean build asm doc debug kernel sfsimg install run justrun test justtest
all: kernel
clean:
@cargo clean
@cd $(user_dir) && make clean
doc:
@cargo doc $(build_args) --no-deps --document-private-items
run: build justrun
test: build justtest
dryrun:
@echo $(qemu) $(qemu_opts)
justrun:
@$(qemu) $(qemu_opts)
justtest:
# unavailable now
@#$(qemu) $(filter-out -serial mon:stdio, $(qemu_opts)) --append $(INIT) -serial file:../tests/stdout -monitor null
debug: $(kernel) $(kernel_img)
@$(qemu) $(qemu_opts) -s -S &
@sleep 1
@$(gdb) $(kernel) -x ../tools/gdbinit
debugserver: $(kernel) $(kernel_img)
@$(qemu) $(qemu_opts) -s -S
debugclient: $(kernel) $(kernel_img)
@$(gdb) $(kernel) -x ../tools/gdbinit
build: $(kernel_img)
asm:
@$(objdump) -d $(kernel) | less
header:
@$(objdump) -h $(kernel)
sym:
@$(objdump) -t $(kernel) | less
### device tree process ###
%.dtb: %.dts
@echo Generating device tree file $@
@$(dtc) -I dts -O dtb -o $@ $<
@rm -rf src/arch/${arch}/boot/dtb.gen.s
### bootloader and kernel image ###
bootloader: $(kernel)
ifeq ($(ARCH), x86_64)
@cd ../rboot && make build
endif
$(kernel_img): kernel bootloader
ifeq ($(ARCH), x86_64)
mkdir -p $(ESP)/EFI/rCore $(ESP)/EFI/Boot
@cp ../rboot/target/x86_64-unknown-uefi/$(MODE)/rboot.efi $(ESP)/EFI/Boot/BootX64.efi
@cp ../rboot/rboot.conf $(ESP)/EFI/Boot/rboot.conf
@cp $(kernel) $(ESP)/EFI/rCore/kernel.elf
else ifeq ($(ARCH), $(filter $(ARCH), riscv32 riscv64))
@$(objcopy) $(kernel) --strip-all -O binary $@
else ifeq ($(ARCH), aarch64)
@$(objcopy) $(kernel) --strip-all -O binary $@
else ifeq ($(ARCH), mipsel)
# qemu-system-mipsel accepts ELF file only, so objcopy is not needed
@$(strip) $(kernel) -o $@
endif
kernel: $(DTB)
@echo Building $(ARCH) kernel
ifeq ($(ARCH), $(filter $(ARCH), riscv32 riscv64))
@cp src/arch/riscv/board/u540/linker.ld src/arch/riscv/boot/linker64.ld
@-patch -p0 -N -b \
$(sysroot)/lib/rustlib/src/rust/src/libcore/sync/atomic.rs \
src/arch/riscv/atomic.patch
else ifeq ($(ARCH), mipsel)
@for file in entry ; do \
$(hostcc) -Dboard_$(BOARD) -E src/arch/$(ARCH)/boot/$${file}.S -o src/arch/$(ARCH)/boot/$${file}.gen.s ; \
done
$(hostcc) -Dboard_$(BOARD) -E src/arch/$(ARCH)/boot/linker.ld.S -o src/arch/$(ARCH)/boot/linker.ld
endif
@cargo build $(build_args)
### user programs ###
sfsimg:
@cd $(user_dir) && make sfsimg
### install ###
ifeq ($(BOARD), raspi3)
sd_card ?=
ifeq ($(shell uname), Darwin)
sd_card := /Volumes/boot
else ifeq ($(shell uname), Linux)
sd_card := /media/$(shell whoami)/boot
endif
ifdef sd_card
.PHONY:
install: $(kernel_img)
cp $(kernel_img) $(sd_card)/kernel8.img
sudo umount $(sd_card)
endif
endif
ifeq ($(BOARD), u540)
.PHONY:
install: $(kernel_img)
@$(objcopy) -S -O binary ../tools/opensbi/fu540.elf $(build_path)/bin
@dd if=$< of=$(build_path)/bin bs=0x20000 seek=16
@../tools/u540/mkimg.sh $(build_path)/bin $(build_path)/u540.img
endif
.PHONY:
addr2line:
@python3 ../tools/addr2line.py $(prefix)addr2line $(ARCH) $(MODE)

View File

@ -63,6 +63,30 @@ pub fn sbi_hart_get_status(hartid: usize) -> SBIRet {
) )
} }
pub fn sbi_set_timer(stime_value: u64) -> SBIRet {
#[cfg(target_pointer_width = "32")]
let ret = sbi_call(
SBICall {
eid: SBI_EID_TIME,
fid: SBI_FID_TIME_SET,
},
stime_value as usize,
(stime_value >> 32) as usize,
0,
);
#[cfg(target_pointer_width = "64")]
let ret = sbi_call(
SBICall {
eid: SBI_EID_TIME,
fid: SBI_FID_TIME_SET,
},
stime_value as usize,
0,
0,
);
ret
}
const SBI_SUCCESS: isize = 0; const SBI_SUCCESS: isize = 0;
const SBI_ERR_FAILED: isize = -1; const SBI_ERR_FAILED: isize = -1;
const SBI_ERR_NOT_SUPPORTED: isize = -2; const SBI_ERR_NOT_SUPPORTED: isize = -2;
@ -75,6 +99,8 @@ const SBI_EID_HSM: usize = 0x48534D;
const SBI_FID_HSM_START: usize = 0; const SBI_FID_HSM_START: usize = 0;
const SBI_FID_HSM_STOP: usize = 1; const SBI_FID_HSM_STOP: usize = 1;
const SBI_FID_HSM_STATUS: usize = 2; const SBI_FID_HSM_STATUS: usize = 2;
const SBI_EID_TIME: usize = 0x54494D45;
const SBI_FID_TIME_SET: usize = 0;
/// Legacy calls. /// Legacy calls.

View File

@ -33,13 +33,11 @@ pub fn init() {
/// Set the next timer interrupt /// Set the next timer interrupt
pub fn set_next() { pub fn set_next() {
// 100Hz @ QEMU // 100Hz @ QEMU
let timebase = 250000; let timebase = 100000;
sbi::set_timer(get_cycle() + timebase); sbi::sbi_set_timer(get_cycle() + timebase);
} }
pub fn timer_now() -> Duration { pub fn timer_now() -> Duration {
// TODO: get actual freq
const FREQUENCY: u16 = 2600;
let time = get_cycle(); let time = get_cycle();
Duration::from_nanos(time * 1000 / FREQUENCY as u64) Duration::from_nanos(time * 100 )
} }

View File

@ -43,10 +43,11 @@ impl RtcDriver for RtcGoldfish {
} }
fn init_dt(dt: &Node) { fn init_dt(dt: &Node) {
use crate::memory::phys_to_virt;
let addr = dt.prop_u64("reg").unwrap() as usize; let addr = dt.prop_u64("reg").unwrap() as usize;
RTC_DRIVERS RTC_DRIVERS.write().push(Arc::new(RtcGoldfish {
.write() base: phys_to_virt(addr),
.push(Arc::new(RtcGoldfish { base: addr })); }));
} }
pub fn driver_init() { pub fn driver_init() {

View File

@ -190,7 +190,7 @@ impl Syscall<'_> {
for elm in slice { for elm in slice {
unsafe { unsafe {
// to prevent overflow // to prevent overflow
*elm = (i + crate::trap::TICK as u8 as u16) as u8; *elm = (i + crate::trap::wall_tick() as u8 as u16) as u8;
} }
i += 1; i += 1;
} }

View File

@ -46,7 +46,7 @@ impl Syscall<'_> {
let rusage = unsafe { self.vm().check_write_ptr(rusage)? }; let rusage = unsafe { self.vm().check_write_ptr(rusage)? };
let tick_base = *TICK_BASE; let tick_base = *TICK_BASE;
let tick = unsafe { crate::trap::TICK as u64 }; let tick = unsafe { crate::trap::wall_tick() as u64 };
let usec = (tick - tick_base) * USEC_PER_TICK as u64; let usec = (tick - tick_base) * USEC_PER_TICK as u64;
let new_rusage = RUsage { let new_rusage = RUsage {
@ -68,7 +68,7 @@ impl Syscall<'_> {
let buf = unsafe { self.vm().check_write_ptr(buf)? }; let buf = unsafe { self.vm().check_write_ptr(buf)? };
let _tick_base = *TICK_BASE; let _tick_base = *TICK_BASE;
let tick = unsafe { crate::trap::TICK as u64 }; let tick = unsafe { crate::trap::wall_tick() as u64 };
let new_buf = Tms { let new_buf = Tms {
tms_utime: 0, tms_utime: 0,
@ -85,7 +85,7 @@ impl Syscall<'_> {
// should be initialized together // should be initialized together
lazy_static! { lazy_static! {
pub static ref EPOCH_BASE: u64 = crate::drivers::rtc::read_epoch(); pub static ref EPOCH_BASE: u64 = crate::drivers::rtc::read_epoch();
pub static ref TICK_BASE: u64 = unsafe { crate::trap::TICK as u64 }; pub static ref TICK_BASE: u64 = unsafe { crate::trap::wall_tick() as u64 };
} }
// 1ms msec // 1ms msec
@ -102,7 +102,7 @@ const NSEC_PER_MSEC: u64 = 1_000_000;
fn get_epoch_usec() -> u64 { fn get_epoch_usec() -> u64 {
let tick_base = *TICK_BASE; let tick_base = *TICK_BASE;
let epoch_base = *EPOCH_BASE; let epoch_base = *EPOCH_BASE;
let tick = unsafe { crate::trap::TICK as u64 }; let tick = unsafe { crate::trap::wall_tick() as u64 };
(tick - tick_base) * USEC_PER_TICK as u64 + epoch_base * USEC_PER_SEC (tick - tick_base) * USEC_PER_TICK as u64 + epoch_base * USEC_PER_SEC
} }

View File

@ -7,15 +7,32 @@ use core::time::Duration;
use naive_timer::Timer; use naive_timer::Timer;
use trapframe::TrapFrame; use trapframe::TrapFrame;
use trapframe::UserContext; use trapframe::UserContext;
use core::sync::atomic::{Ordering, AtomicUsize};
pub static TICK: AtomicUsize = AtomicUsize::new(0);
pub static TICK_ALL_PROCESSORS: AtomicUsize = AtomicUsize::new(0);
pub static mut TICK: usize = 0; pub unsafe fn wall_tick()->usize{
return TICK.load(Ordering::Relaxed);
}
pub fn cpu_tick()->usize{
return TICK_ALL_PROCESSORS.load(Ordering::Relaxed);
}
pub fn do_tick(){
if crate::arch::cpu::id() == 0 {
let ret=TICK.fetch_add(1, Ordering::Relaxed);
if ret%100==0{
info!("{}", ret);
}
}
TICK_ALL_PROCESSORS.fetch_add(1, Ordering::Relaxed);
}
lazy_static! { lazy_static! {
pub static ref TICK_ACTIVITY: Condvar = Condvar::new(); pub static ref TICK_ACTIVITY: Condvar = Condvar::new();
} }
pub fn uptime_msec() -> usize { pub fn uptime_msec() -> usize {
unsafe { crate::trap::TICK * crate::consts::USEC_PER_TICK / 1000 } unsafe { crate::trap::wall_tick() * crate::consts::USEC_PER_TICK / 1000 }
} }
lazy_static! { lazy_static! {
@ -23,6 +40,9 @@ lazy_static! {
} }
pub fn timer() { pub fn timer() {
do_tick();
//let ret=unsafe{wall_tick()};
let now = crate::arch::timer::timer_now(); let now = crate::arch::timer::timer_now();
NAIVE_TIMER.lock().expire(now); NAIVE_TIMER.lock().expire(now);
} }

1
submodules/rcore-fs Submodule

@ -0,0 +1 @@
Subproject commit 517af47ff3f632f05b91b9ea73a047aa8df363f3

2
tools/gdbinit-sbi Normal file
View File

@ -0,0 +1,2 @@
file ../tools/sbi/fw_jump.elf
target remote 127.0.0.1:1234