Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/actions/import-toolchain/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ runs:
with:
path: |
~/opt
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('tools/build_gcc_toolchain.sh') }}-${{ hashFiles('tools/install_musl_gcc.sh') }}
~/.bashrc
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('tools/build_gcc_toolchain.sh') }}

- name: Cache build tools
id: cache-build-tools
Expand All @@ -28,7 +29,8 @@ runs:
~/.cargo
~/.rustup
~/.bashrc
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ env.dadk_version }}-${{ hashFiles('.github/workflows/cache-toolchain.yml') }}
~/opt
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ env.dadk_version }}-${{ hashFiles('.github/workflows/cache-toolchain.yml') }}-${{ hashFiles('tools/install_musl_gcc.sh') }}

- uses: ./.github/actions/install-apt-packages

10 changes: 6 additions & 4 deletions .github/workflows/cache-toolchain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ jobs:
with:
path: |
~/opt
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('tools/build_gcc_toolchain.sh') }}-${{ hashFiles('tools/install_musl_gcc.sh') }}
~/.bashrc
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('tools/build_gcc_toolchain.sh') }}

- if: ${{ steps.cache-dragonos-gcc.outputs.cache-hit != 'true' }}
name: build dragonos-gcc
continue-on-error: true
run: |
bash tools/build_gcc_toolchain.sh -f
bash tools/install_musl_gcc.sh

- uses: ./.github/actions/install-apt-packages

Expand All @@ -40,13 +40,15 @@ jobs:
~/.cargo
~/.rustup
~/.bashrc
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ env.dadk_version }}-${{ hashFiles('.github/workflows/cache-toolchain.yml') }}
~/opt
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ env.dadk_version }}-${{ hashFiles('.github/workflows/cache-toolchain.yml') }}-${{ hashFiles('tools/install_musl_gcc.sh') }}

- if: ${{ steps.cache-build-tools.outputs.cache-hit != 'true' }}
name: Install toolchain
continue-on-error: false
run: |

USE_GITHUB=1 bash tools/install_musl_gcc.sh

cargo install cargo-binutils
rustup toolchain install nightly-x86_64-unknown-linux-gnu
rustup toolchain install nightly-2023-01-21-x86_64-unknown-linux-gnu
Expand Down
15 changes: 12 additions & 3 deletions .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build Check

on:
push:
branches: [ "master", "patch-add-riscv64-github-workflow" ]
branches: [ "master" ]
pull_request:
branches: [ "master" ]

Expand Down Expand Up @@ -50,6 +50,7 @@ jobs:
- uses: ./.github/actions/import-toolchain

- name: Run kernel static test
shell: bash -ileo pipefail {0}
env:
ARCH: ${{ matrix.arch }}
run: bash -c "source ~/.cargo/env && cd kernel && make test"
Expand All @@ -68,7 +69,14 @@ jobs:
- name: build the DragonOS
env:
ARCH: x86_64
run: bash -c "source ~/.cargo/env && export DragonOS_GCC=$HOME/opt/dragonos-gcc/gcc-x86_64-unknown-none/bin && make -j $(nproc) "
shell: bash -ileo pipefail {0}

run: |
source ~/.bashrc
source ~/.cargo/env
export DragonOS_GCC=$HOME/opt/dragonos-gcc/gcc-x86_64-unknown-none/bin

make -j $(nproc)


build-riscv64:
Expand All @@ -84,8 +92,9 @@ jobs:
- uses: ./.github/actions/import-toolchain

- name: build the DragonOS
shell: bash -ileo pipefail {0}
env:
ARCH: riscv64

run: bash -c "source ~/.cargo/env && make kernel -j $(nproc)"
run: source ~/.bashrc && source ~/.cargo/env && make kernel -j $(nproc)

8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@
"clocksource.h": "c",
"ata.h": "c",
"barrier": "c",
"charconv": "c"
"charconv": "c",
"printf.h": "c",
"klog.h": "c",
"sqlite3ext.h": "c",
"malloc.h": "c",
"*.o": "c",
"k_log.h": "c"
},
"C_Cpp.errorSquiggles": "enabled",
"esbonio.sphinx.confDir": "",
Expand Down
1 change: 1 addition & 0 deletions kernel/crates/kdepends/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ description = "需要导出的依赖项(为保持内核依赖版本与调试
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ringbuffer = "0.15.0"
memoffset = "0.9.0"
crc = { path = "../crc" }

Expand Down
2 changes: 2 additions & 0 deletions kernel/crates/kdepends/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ pub extern crate thingbuf;

pub extern crate memoffset;

pub extern crate ringbuffer;

pub extern crate crc;
2 changes: 1 addition & 1 deletion kernel/src/arch/x86_64/ipc/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ impl SignalArch for X86_64SignalArch {
"Error occurred when handling signal: {}, pid={:?}, errcode={:?}",
sig_number as i32,
ProcessManager::current_pcb().pid(),
res.unwrap_err()
res.as_ref().unwrap_err()
);
}
}
Expand Down
3 changes: 3 additions & 0 deletions kernel/src/arch/x86_64/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use x86::time::rdtsc;
use x86_64::registers::model_specific::EferFlags;

use crate::driver::tty::serial::serial8250::send_to_default_serial8250_port;
use crate::filesystem::procfs::kmsg::kmsg_init;
use crate::include::bindings::bindings::{
multiboot2_get_load_base, multiboot2_get_memory, multiboot2_iter, multiboot_mmap_entry_t,
multiboot_tag_load_base_addr_t,
Expand Down Expand Up @@ -419,6 +420,8 @@ pub fn mm_init() {
unsafe { allocator_init() };
// enable mmio
mmio_init();
// enable KMSG
kmsg_init();
}

unsafe fn allocator_init() {
Expand Down
8 changes: 6 additions & 2 deletions kernel/src/driver/net/e1000e/e1000e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,12 @@ pub extern "C" fn rs_e1000e_init() {

pub fn e1000e_init() -> () {
match e1000e_probe() {
Ok(_code) => kinfo!("Successfully init e1000e device!"),
Err(_error) => kinfo!("Error occurred!"),
Ok(_code) => {
kinfo!("Successfully init e1000e device!");
}
Err(_error) => {
kinfo!("Error occurred!");
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/filesystem/fat/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ impl Drop for FATFileSystem {
if r.is_err() {
kerror!(
"Umount FAT filesystem failed: errno={:?}, FS detail:{self:?}",
r.unwrap_err()
r.as_ref().unwrap_err()
);
}
}
Expand Down
151 changes: 151 additions & 0 deletions kernel/src/filesystem/procfs/kmsg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use super::log::{LogLevel, LogMessage};

use crate::libs::spinlock::SpinLock;

use alloc::{borrow::ToOwned, string::ToString, vec::Vec};

use kdepends::ringbuffer::{AllocRingBuffer, RingBuffer};

use system_error::SystemError;

/// 缓冲区容量
const KMSG_BUFFER_CAPACITY: usize = 1024;

/// 全局环形缓冲区
pub static mut KMSG: Option<SpinLock<Kmsg>> = None;

/// 初始化KMSG
pub fn kmsg_init() {
let kmsg = SpinLock::new(Kmsg::new());
unsafe { KMSG = Some(kmsg) };
}

/// 日志
pub struct Kmsg {
/// 环形缓冲区
buffer: AllocRingBuffer<LogMessage>,
/// 缓冲区字节数组
data: Vec<u8>,
/// 能够输出到控制台的日志级别,当console_loglevel为DEFAULT时,表示可以打印所有级别的日志消息到控制台
console_loglevel: LogLevel,
/// 判断buffer在上一次转成字节数组之后是否发生变动
is_changed: bool,
}

impl Kmsg {
pub fn new() -> Self {
Kmsg {
buffer: AllocRingBuffer::new(KMSG_BUFFER_CAPACITY),
data: Vec::new(),
console_loglevel: LogLevel::DEFAULT,
is_changed: false,
}
}

/// 添加日志消息
pub fn push(&mut self, msg: LogMessage) {
self.buffer.push(msg);
self.is_changed = true;
}

/// 读取缓冲区
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize, SystemError> {
self.tobytes();

match self.console_loglevel {
LogLevel::DEFAULT => self.read_all(buf),
_ => self.read_level(buf),
}
}

/// 读取缓冲区所有日志消息
fn read_all(&mut self, buf: &mut [u8]) -> Result<usize, SystemError> {
let len = self.data.len().min(buf.len());

// 拷贝数据
let src = &self.data[0..len];
buf[0..src.len()].copy_from_slice(src);

return Ok(src.len());
}

/// 读取缓冲区特定level的日志消息
fn read_level(&mut self, buf: &mut [u8]) -> Result<usize, SystemError> {
let mut data_level: Vec<u8> = Vec::new();

for msg in self.buffer.iter() {
if msg.level() == self.console_loglevel {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我感觉,level应该是个“大于”的关系。就比如我们使用日志库的时候,设置为debug级别,就是打印优先级大于等于debug的。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我测试了下,在Linux中,(dmesg -l notice) 和 (dmesg -l debug) 都不会打印 info 级别的日志消息(优先级:debug < info < notice)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦哦哦确实噢!

data_level.append(&mut msg.to_string().as_bytes().to_owned());
}
}

let len = data_level.len().min(buf.len());

// 拷贝数据
let src = &data_level[0..len];
buf[0..src.len()].copy_from_slice(src);

// 将控制台输出日志level改回默认,否则之后都是打印特定level的日志消息
self.console_loglevel = LogLevel::DEFAULT;

return Ok(data_level.len());
}

/// 读取并清空缓冲区
pub fn read_clear(&mut self, buf: &mut [u8]) -> Result<usize, SystemError> {
let r = self.read_all(buf);
self.clear()?;

return r;
}

/// 清空缓冲区
pub fn clear(&mut self) -> Result<usize, SystemError> {
self.buffer.clear();
self.data.clear();

return Ok(0);
}

/// 设置输出到控制台的日志级别
pub fn set_level(&mut self, log_level: usize) -> Result<usize, SystemError> {
let log_level = log_level - 1;

self.console_loglevel = match log_level {
0 => LogLevel::EMERG,
1 => LogLevel::ALERT,
2 => LogLevel::CRIT,
3 => LogLevel::ERR,
4 => LogLevel::WARN,
5 => LogLevel::NOTICE,
6 => LogLevel::INFO,
7 => LogLevel::DEBUG,
8 => LogLevel::DEFAULT,
_ => return Err(SystemError::EINVAL),
};

return Ok(0);
}

/// 将环形缓冲区的日志消息转成字节数组以拷入用户buf
fn tobytes(&mut self) -> usize {
if self.is_changed {
self.data.clear();

if self.console_loglevel == LogLevel::DEFAULT {
for msg in self.buffer.iter() {
self.data.append(&mut msg.to_string().as_bytes().to_owned());
}
}

self.is_changed = false;
}

return self.data.len();
}

// 返回内核缓冲区所占字节数
pub fn data_size(&mut self) -> Result<usize, SystemError> {
return Ok(self.tobytes());
}
}
Loading