Skip to content

Commit

Permalink
Merge branch 'master' into heyicong-patch-thread
Browse files Browse the repository at this point in the history
  • Loading branch information
fslongjin committed Oct 31, 2023
2 parents 1b70349 + 77799cc commit 7cb7d23
Show file tree
Hide file tree
Showing 40 changed files with 2,180 additions and 403 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@
"cmd_test.h": "c",
"cmpxchg.h": "c",
"mman.h": "c",
"clocksource.h": "c"
"clocksource.h": "c",
"ata.h": "c"
},
"C_Cpp.errorSquiggles": "enabled",
"esbonio.sphinx.confDir": "",
Expand Down
2 changes: 1 addition & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ida = { path = "src/libs/ida" }
# 构建时依赖项
[build-dependencies]
bindgen = "0.61.0"

cc = { version = "1.0.83", features = ["parallel"] }

[dependencies.lazy_static]
version = "1.4.0"
Expand Down
63 changes: 63 additions & 0 deletions kernel/build.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
extern crate bindgen;
extern crate cc;
// use ::std::env;

use std::path::PathBuf;

use cc::Build;

fn main() {
// Tell cargo to look for shared libraries in the specified directory
println!("cargo:rustc-link-search=src");
println!("cargo:rerun-if-changed=src/include/bindings/wrapper.h");

generate_bindings();
CFilesBuilder::build();
}

fn generate_bindings() {
// let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let out_path = PathBuf::from(String::from("src/include/bindings/"));

Expand Down Expand Up @@ -45,3 +53,58 @@ fn main() {
.expect("Couldn't write bindings!");
}
}

/// 构建项目的c文件
struct CFilesBuilder;

impl CFilesBuilder {
fn build() {
let mut c = cc::Build::new();
Self::setup_global_flags(&mut c);
Self::setup_defines(&mut c);
Self::setup_global_include_dir(&mut c);
Self::setup_files(&mut c);
c.compile("dragonos_kernel_cfiles");
}

fn setup_global_flags(c: &mut Build) {
c.flag("-mcmodel=large")
.flag("-fno-builtin")
.flag("-nostdlib")
.flag("-fno-stack-protector")
.flag("-fno-pie")
.flag("-Wno-expansion-to-defined")
.flag("-Wno-unused-parameter")
.flag("-m64")
.flag("-O1");
}

fn setup_defines(c: &mut Build) {
if let Ok(k) = std::env::var("EMULATOR") {
c.define("EMULATOR", Some(k.as_str()));
} else {
c.define("EMULATOR", "__NO_EMULATION__");
}

{
#[cfg(target_arch = "x86_64")]
c.define("__x86_64__", None);
}

c.define("PIC", "_INTR_APIC_");
}

fn setup_global_include_dir(c: &mut Build) {
c.include("src/include");
c.include("src");
c.include(".");

#[cfg(target_arch = "x86_64")]
c.include("src/arch/x86_64/include");
}

/// 设置需要编译的文件
fn setup_files(c: &mut Build) {
c.file("src/arch/x86_64/driver/hpet.c");
}
}
15 changes: 14 additions & 1 deletion kernel/src/arch/x86_64/c_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
use super::setup::setup_arch;
use crate::time::TimeArch;

use super::{driver::tsc::TSCManager, setup::setup_arch, CurrentTimeArch};

#[no_mangle]
unsafe extern "C" fn rs_setup_arch() -> i32 {
return setup_arch()
.map(|_| 0)
.unwrap_or_else(|e| e.to_posix_errno());
}

/// 获取当前的时间戳
#[no_mangle]
unsafe extern "C" fn rs_get_cycles() -> u64 {
return CurrentTimeArch::get_cycles() as u64;
}

#[no_mangle]
unsafe extern "C" fn rs_tsc_get_cpu_khz() -> u64 {
return TSCManager::cpu_khz();
}
31 changes: 31 additions & 0 deletions kernel/src/arch/x86_64/driver/c_adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use super::{
hpet::{hpet_init, hpet_instance},
tsc::TSCManager,
};

#[no_mangle]
unsafe extern "C" fn rs_hpet_init() -> i32 {
hpet_init()
.map(|_| 0)
.unwrap_or_else(|e| e.to_posix_errno())
}

#[no_mangle]
unsafe extern "C" fn rs_hpet_enable() -> i32 {
hpet_instance()
.hpet_enable()
.map(|_| 0)
.unwrap_or_else(|e| e.to_posix_errno())
}

#[no_mangle]
unsafe extern "C" fn rs_tsc_init() -> i32 {
TSCManager::init()
.map(|_| 0)
.unwrap_or_else(|e| e.to_posix_errno())
}

#[no_mangle]
unsafe extern "C" fn rs_handle_hpet_irq(timer_num: u32) {
hpet_instance().handle_irq(timer_num);
}
26 changes: 26 additions & 0 deletions kernel/src/arch/x86_64/driver/hpet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <common/glib.h>
#include <common/kprint.h>
#include <driver/interrupt/apic/apic.h>

extern void rs_handle_hpet_irq(uint32_t timer_num);

hardware_intr_controller HPET_intr_controller =
{
.enable = apic_ioapic_enable,
.disable = apic_ioapic_disable,
.install = apic_ioapic_install,
.uninstall = apic_ioapic_uninstall,
.ack = apic_ioapic_edge_ack,
};

void HPET_handler(uint64_t number, uint64_t param, struct pt_regs *regs)
{
rs_handle_hpet_irq(param);
}

void c_hpet_register_irq()
{
struct apic_IO_APIC_RTE_entry entry;
apic_make_rte_entry(&entry, 34, IO_APIC_FIXED, DEST_PHYSICAL, IDLE, POLARITY_HIGH, IRR_RESET, EDGE_TRIGGER, MASKED, 0);
irq_register(34, &entry, &HPET_handler, 0, &HPET_intr_controller, "HPET0");
}
Loading

0 comments on commit 7cb7d23

Please sign in to comment.