使用cargo管理一些C文件的编译,并且移动部分汇编到arch目录 (#447)

* 使用cargo管理main.c的编译

* 使用build-scripts编译架构相关的c代码

* 删除elf.h
This commit is contained in:
LoGin 2023-11-17 21:25:15 +08:00 committed by GitHub
parent e4600f7f7d
commit 46e234aef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 556 additions and 1683 deletions

View File

@ -124,12 +124,9 @@
"mm-types.h": "c",
"vfs.h": "c",
"current.h": "c",
"proc-types.h": "c",
"traceback.h": "c",
"bitcount.h": "c",
"limits.h": "c",
"block.h": "c",
"blk_types.h": "c",
"mutex.h": "c",
"mount.h": "c",
"internal.h": "c",
@ -173,7 +170,8 @@
"cmpxchg.h": "c",
"mman.h": "c",
"clocksource.h": "c",
"ata.h": "c"
"ata.h": "c",
"barrier": "c"
},
"C_Cpp.errorSquiggles": "enabled",
"esbonio.sphinx.confDir": "",

View File

@ -2,7 +2,7 @@ use std::path::PathBuf;
use cc::Build;
use crate::utils::FileUtils;
use crate::{constant::ARCH_DIR_X86_64, utils::FileUtils};
use super::CFilesArch;
@ -18,10 +18,26 @@ impl CFilesArch for X86_64CFilesArch {
}
fn setup_files(&self, _c: &mut Build, files: &mut Vec<PathBuf>) {
files.push(PathBuf::from("src/arch/x86_64/driver/hpet.c"));
files.push(arch_path("driver/hpet.c"));
// 获取`kernel/src/arch/x86_64/driver/apic`下的所有C文件
files.append(&mut FileUtils::list_all_files(
&PathBuf::from("src/arch/x86_64/driver/apic"),
&arch_path("driver/apic"),
Some("c"),
true,
));
files.append(&mut FileUtils::list_all_files(
&arch_path("init"),
Some("c"),
true,
));
files.append(&mut FileUtils::list_all_files(
&arch_path("asm"),
Some("c"),
true,
));
files.append(&mut FileUtils::list_all_files(
&arch_path("interrupt"),
Some("c"),
true,
));
@ -36,3 +52,7 @@ impl CFilesArch for X86_64CFilesArch {
c.asm_flag("-m64");
}
}
fn arch_path(relative_path: &str) -> PathBuf {
PathBuf::from(format!("{}/{}", ARCH_DIR_X86_64, relative_path))
}

View File

@ -0,0 +1 @@
pub const ARCH_DIR_X86_64: &str = "src/arch/x86_64";

View File

@ -4,6 +4,7 @@ extern crate cc;
mod bindgen;
mod cfiles;
mod constant;
mod kconfig;
mod utils;

View File

@ -26,21 +26,16 @@ export ASFLAGS := --64
LD_LIST := ""
kernel_subdirs := common driver debug arch exception smp syscall ktest libs time
kernel_subdirs := common driver debug exception smp syscall ktest libs time
main.o: main.c
# -fno-builtin: 不使用C语言内建函数
# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMDs x86-64 architecture.
$(CC) $(CFLAGS) -c main.c -o main.o
kernel_rust:
RUSTFLAGS="$(RUSTFLAGS_UNWIND)" cargo +nightly-2023-01-21 build --release --target ./arch/x86_64/x86_64-unknown-none.json
all: kernel
@echo "Linking kernel..."
$(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a -T link.lds --no-relax
$(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a -T link.lds --no-relax
# 生成kallsyms
current_dir=$(pwd)
@ -54,7 +49,7 @@ all: kernel
# 重新链接
@echo "Re-Linking kernel..."
@echo $(shell find . -name "*.o")
$(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a ./debug/kallsyms.o -T link.lds --no-relax
$(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a ./debug/kallsyms.o -T link.lds --no-relax
@echo "Generating kernel ELF file..."
# 生成内核文件
ifeq ($(UNWIND_ENABLE), yes)
@ -71,7 +66,7 @@ $(kernel_subdirs): ECHO
$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" kernel_root_path="$(shell pwd)"
kernel: main.o $(kernel_subdirs) kernel_rust
kernel: $(kernel_subdirs) kernel_rust

View File

@ -1,17 +0,0 @@
CFLAGS += -I .
ifeq ($(ARCH), __x86_64__)
kernel_arch_subdirs:=x86_64
endif
all:
@list='$(kernel_arch_subdirs)'; for subdir in $$list; do \
echo "make all in $$subdir";\
cd $$subdir;\
$(MAKE) all CFLAGS="$(CFLAGS)" ;\
cd ..;\
done
clean:
echo "Done."

View File

@ -1,23 +0,0 @@
CFLAGS += -I .
kernel_arch_x86_64_subdirs:= asm
kernel_arch_x86_64_objs:= $(shell find ./*.c)
ECHO:
@echo "$@"
$(kernel_arch_x86_64_objs): ECHO
$(CC) $(CFLAGS) -c $@ -o $@.o
$(kernel_arch_x86_64_subdirs): ECHO
$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)"
all: $(kernel_arch_x86_64_objs) $(kernel_arch_x86_64_subdirs)
clean:
echo "Done."

View File

@ -1,22 +0,0 @@
CFLAGS += -I .
# kernel_arch_x86_64_asm_subdirs:=
kernel_arch_x86_64_asm_objs:= $(shell find ./*.c)
ECHO:
@echo "$@"
$(kernel_arch_x86_64_asm_objs): ECHO
$(CC) $(CFLAGS) -c $@ -o $@.o
# $(kernel_arch_x86_64_asm_subdirs): ECHO
# $(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)"
all: $(kernel_arch_x86_64_asm_objs)
clean:
echo "Done."

View File

@ -300,7 +300,7 @@ ENTRY(_start64)
// 50-100M025
mov $12800, %ecx
.fill_pt_64_2:
mov $0, 0(%eax)
movq $0, 0(%eax)
add $8, %eax
loop .fill_pt_64_2

View File

@ -1,16 +1,26 @@
#pragma once
#include <DragonOS/stdint.h>
#include <stdbool.h>
#include <common/stddef.h>
// 定义类型的缩写
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ul;
typedef unsigned long long int ull;
typedef long long int ll;
#define sti() __asm__ __volatile__("sti\n\t" :: \
: "memory") //开启外部中断
: "memory") // 开启外部中断
#define cli() __asm__ __volatile__("cli\n\t" :: \
: "memory") //关闭外部中断
: "memory") // 关闭外部中断
#define nop() __asm__ __volatile__("nop\n\t")
#define hlt() __asm__ __volatile__("hlt\n\t")
#define pause() asm volatile("pause\n\t"); // 处理器等待一段时间
//内存屏障
// 内存屏障
#define io_mfence() __asm__ __volatile__("mfence\n\t" :: \
: "memory") // 在mfence指令前的读写操作必须在mfence指令后的读写操作前完成。
#define io_sfence() __asm__ __volatile__("sfence\n\t" :: \
@ -54,6 +64,22 @@ unsigned long *get_rsp()
return tmp;
}
/**
* @brief
*
* @param addr_start
* @param length
* @return true
* @return false
*/
bool verify_area(uint64_t addr_start, uint64_t length)
{
if ((addr_start + length) <= 0x00007fffffffffffUL) // 用户程序可用的的地址空间应<= 0x00007fffffffffffUL
return true;
else
return false;
}
/**
* @brief rbp寄存器的值
*
@ -147,3 +173,247 @@ uint64_t get_rflags()
: "=r"(tmp)::"memory");
return tmp;
}
void *memset(void *dst, unsigned char C, ul size)
{
int d0, d1;
unsigned long tmp = C * 0x0101010101010101UL;
__asm__ __volatile__("cld \n\t"
"rep \n\t"
"stosq \n\t"
"testb $4, %b3 \n\t"
"je 1f \n\t"
"stosl \n\t"
"1:\ttestb $2, %b3 \n\t"
"je 2f\n\t"
"stosw \n\t"
"2:\ttestb $1, %b3 \n\t"
"je 3f \n\t"
"stosb \n\t"
"3: \n\t"
: "=&c"(d0), "=&D"(d1)
: "a"(tmp), "q"(size), "0"(size / 8), "1"(dst)
: "memory");
return dst;
}
void *memset_c(void *dst, uint8_t c, size_t count)
{
uint8_t *xs = (uint8_t *)dst;
while (count--)
*xs++ = c;
return dst;
}
/**
* @brief
*
* @param dst
* @param src
* @param Num
* @return void*
*/
static void *memcpy(void *dst, const void *src, long Num)
{
int d0 = 0, d1 = 0, d2 = 0;
__asm__ __volatile__("cld \n\t"
"rep \n\t"
"movsq \n\t"
"testb $4,%b4 \n\t"
"je 1f \n\t"
"movsl \n\t"
"1:\ttestb $2,%b4 \n\t"
"je 2f \n\t"
"movsw \n\t"
"2:\ttestb $1,%b4 \n\t"
"je 3f \n\t"
"movsb \n\t"
"3: \n\t"
: "=&c"(d0), "=&D"(d1), "=&S"(d2)
: "0"(Num / 8), "q"(Num), "1"(dst), "2"(src)
: "memory");
return dst;
}
// 从io口读入8个bit
unsigned char io_in8(unsigned short port)
{
unsigned char ret = 0;
__asm__ __volatile__("inb %%dx, %0 \n\t"
"mfence \n\t"
: "=a"(ret)
: "d"(port)
: "memory");
return ret;
}
// 从io口读入32个bit
unsigned int io_in32(unsigned short port)
{
unsigned int ret = 0;
__asm__ __volatile__("inl %%dx, %0 \n\t"
"mfence \n\t"
: "=a"(ret)
: "d"(port)
: "memory");
return ret;
}
// 输出8个bit到输出端口
void io_out8(unsigned short port, unsigned char value)
{
__asm__ __volatile__("outb %0, %%dx \n\t"
"mfence \n\t"
:
: "a"(value), "d"(port)
: "memory");
}
// 输出32个bit到输出端口
void io_out32(unsigned short port, unsigned int value)
{
__asm__ __volatile__("outl %0, %%dx \n\t"
"mfence \n\t"
:
: "a"(value), "d"(port)
: "memory");
}
/**
* @brief n个word到buffer
*
*/
#define io_insw(port, buffer, nr) \
__asm__ __volatile__("cld;rep;insw;mfence;" ::"d"(port), "D"(buffer), "c"(nr) \
: "memory")
/**
* @brief buffer中的n个word到端口
*
*/
#define io_outsw(port, buffer, nr) \
__asm__ __volatile__("cld;rep;outsw;mfence;" ::"d"(port), "S"(buffer), "c"(nr) \
: "memory")
/**
* @brief
*
* @param dst
* @param src
* @param size
* @return uint64_t
*/
static inline uint64_t copy_from_user(void *dst, void *src, uint64_t size)
{
uint64_t tmp0, tmp1;
if (!verify_area((uint64_t)src, size))
return 0;
/**
* @brief 8 bytesbyte搬运
*
*/
asm volatile("rep \n\t"
"movsq \n\t"
"movq %3, %0 \n\t"
"rep \n\t"
"movsb \n\t"
: "=&c"(size), "=&D"(tmp0), "=&S"(tmp1)
: "r"(size & 7), "0"(size >> 3), "1"(dst), "2"(src)
: "memory");
return size;
}
/**
* @brief
*
* @param dst
* @param src
* @param size
* @return uint64_t
*/
static inline uint64_t copy_to_user(void *dst, void *src, uint64_t size)
{
if (verify_area((uint64_t)src, size))
return 0;
/**
* @brief 8 bytesbyte搬运
*
*/
// todo:编译有bug
// asm volatile("rep \n\t"
// "movsq \n\t"
// "movq %3, %0 \n\t"
// "rep \n\t"
// "movsb \n\t"
// : "=&c"(size), "=&D"(tmp0), "=&S"(tmp1)
// : "r"(size & 7), "0"(size >> 3), "1"(dst), "2"(src)
// : "memory");
memcpy(dst, src, size);
return size;
}
/**
* @brief 8
* 访mmio的时候
*
* @param vaddr
* @param value
*/
static __always_inline void __write8b(uint64_t vaddr, uint64_t value)
{
asm volatile("movq %%rdx, 0(%%rax)" ::"a"(vaddr), "d"(value)
: "memory");
}
/**
* @brief 4
* 访mmio的时候
*
* @param vaddr
* @param value
*/
static __always_inline void __write4b(uint64_t vaddr, uint32_t value)
{
asm volatile("movl %%edx, 0(%%rax)" ::"a"(vaddr), "d"(value)
: "memory");
}
/**
* @brief 8
* 访mmio的时候
*
* @param vaddr
* @return uint64_t
*/
static __always_inline uint64_t __read8b(uint64_t vaddr)
{
uint64_t retval;
asm volatile("movq 0(%%rax), %0"
: "=r"(retval)
: "a"(vaddr)
: "memory");
return retval;
}
/**
* @brief 4
* 访mmio的时候
*
* @param vaddr
* @return uint64_t
*/
static __always_inline uint32_t __read4b(uint64_t vaddr)
{
uint32_t retval;
asm volatile("movl 0(%%rax), %0"
: "=d"(retval)
: "a"(vaddr)
: "memory");
return retval;
}

View File

@ -0,0 +1,219 @@
//
// Created by longjin on 2022/1/20.
//
#include "common/glib.h"
#include "common/kprint.h"
#include "common/printk.h"
#include "exception/gate.h"
#include "exception/irq.h"
#include "exception/trap.h"
#include "mm/mm.h"
#include "mm/slab.h"
#include "process/process.h"
#include "smp/smp.h"
#include "syscall/syscall.h"
#include <exception/softirq.h>
#include <libs/lib_ui/screen_manager.h>
#include <libs/lib_ui/textui.h>
#include <sched/sched.h>
#include <smp/ipi.h>
#include <filesystem/vfs/VFS.h>
#include "driver/acpi/acpi.h"
#include "driver/disk/ata.h"
#include "driver/keyboard/ps2_keyboard.h"
#include "driver/mouse/ps2_mouse.h"
#include "driver/multiboot2/multiboot2.h"
#include <time/timer.h>
#include <arch/x86_64/driver/apic/apic_timer.h>
#include <virt/kvm/kvm.h>
#include <debug/bug.h>
extern int rs_driver_init();
extern void rs_softirq_init();
extern void rs_mm_init();
extern void rs_kthread_init();
extern void rs_init_intertrait();
extern void rs_init_before_mem_init();
extern int rs_setup_arch();
extern void rs_futex_init();
extern int rs_hpet_init();
extern int rs_hpet_enable();
extern int rs_tsc_init();
extern void rs_clocksource_boot_finish();
extern void rs_timekeeping_init();
extern void rs_process_init();
extern void rs_textui_init();
extern void rs_pci_init();
ul bsp_idt_size, bsp_gdt_size;
#pragma GCC push_options
#pragma GCC optimize("O0")
struct gdtr gdtp;
struct idtr idtp;
ul _stack_start;
void reload_gdt()
{
gdtp.size = bsp_gdt_size - 1;
gdtp.gdt_vaddr = (ul)phys_2_virt((ul)&GDT_Table);
asm volatile("lgdt (%0) \n\t" ::"r"(&gdtp) : "memory");
}
void reload_idt()
{
idtp.size = bsp_idt_size - 1;
idtp.idt_vaddr = (ul)phys_2_virt((ul)&IDT_Table);
// kdebug("gdtvaddr=%#018lx", p.gdt_vaddr);
// kdebug("gdt size=%d", p.size);
asm volatile("lidt (%0) \n\t" ::"r"(&idtp) : "memory");
}
// 初始化系统各模块
void system_initialize()
{
rs_init_before_mem_init();
_stack_start =
head_stack_start; // 保存init
// proc的栈基地址由于之后取消了地址重映射因此必须在这里重新保存
kdebug("_stack_start=%#018lx", _stack_start);
set_current_core_tss(_stack_start, 0);
rs_load_current_core_tss();
cpu_core_info[0].stack_start = _stack_start;
// 初始化中断描述符表
sys_vector_init();
// 初始化内存管理单元
// mm_init();
rs_mm_init();
// 内存管理单元初始化完毕后,需要立即重新初始化显示驱动。
// 原因是系统启动初期framebuffer被映射到48M地址处
// mm初始化完毕后若不重新初始化显示驱动将会导致错误的数据写入内存从而造成其他模块崩溃
// 对显示模块进行低级初始化不启用double buffer
io_mfence();
scm_reinit();
rs_textui_init();
rs_init_intertrait();
// kinfo("vaddr:%#018lx", video_frame_buffer_info.vaddr);
io_mfence();
vfs_init();
rs_driver_init();
acpi_init();
rs_setup_arch();
io_mfence();
irq_init();
rs_process_init();
sched_init();
sti();
io_mfence();
rs_softirq_init();
syscall_init();
io_mfence();
rs_timekeeping_init();
io_mfence();
rs_timer_init();
io_mfence();
rs_jiffies_init();
io_mfence();
rs_kthread_init();
io_mfence();
io_mfence();
rs_clocksource_boot_finish();
io_mfence();
cpu_init();
ps2_keyboard_init();
io_mfence();
rs_pci_init();
// 这里必须加内存屏障,否则会出错
io_mfence();
smp_init();
io_mfence();
rs_futex_init();
cli();
rs_hpet_init();
rs_hpet_enable();
rs_tsc_init();
io_mfence();
kvm_init();
io_mfence();
// 系统初始化到此结束,剩下的初始化功能应当放在初始内核线程中执行
apic_timer_init();
// while(1);
io_mfence();
sti();
while (1)
;
}
// 操作系统内核从这里开始执行
void Start_Kernel(void)
{
// 获取multiboot2的信息
uint64_t mb2_info, mb2_magic;
__asm__ __volatile__("movq %%r15, %0 \n\t"
"movq %%r14, %1 \n\t"
"movq %%r13, %2 \n\t"
"movq %%r12, %3 \n\t"
: "=r"(mb2_info), "=r"(mb2_magic), "=r"(bsp_gdt_size),
"=r"(bsp_idt_size)::"memory");
reload_gdt();
reload_idt();
mb2_info &= 0xffffffff;
mb2_magic &= 0xffffffff;
multiboot2_init(mb2_info, mb2_magic);
io_mfence();
system_initialize();
io_mfence();
// idle
while (1)
{
// 如果调用的时候启用了中断则hlt。否则认为是bug
if (get_rflags() & 0x200)
{
// kdebug("hlt");
hlt();
}
else
{
BUG_ON(1);
pause();
}
}
}
#pragma GCC pop_options

View File

@ -1,84 +0,0 @@
#pragma once
#include <DragonOS/stdint.h>
#include <common/glib.h>
#define BLK_TYPE_AHCI 0
#define DISK_NAME_LEN 32 // 磁盘名称的最大长度
struct blk_gendisk;
struct block_device_operation
{
long (*open)();
long (*close)();
long (*ioctl)(long cmd, long arg);
/**
* @brief
*
* @param gd
* @param cmd
* @param base_addr 48LBA地址
* @param count total sectors to read
* @param buf 线
* @return long
*/
long (*transfer)(struct blk_gendisk *gd, long cmd, uint64_t base_addr, uint64_t count, uint64_t buf);
};
/**
* @brief packet
*
*/
struct block_device_request_packet
{
uchar cmd;
uint64_t LBA_start;
uint32_t count;
uint64_t buffer_vaddr;
uint8_t device_type; // 0: ahci
void (*end_handler)(ul num, ul arg);
};
/**
* @brief
*
*/
struct block_device_request_queue
{
struct block_device_request_packet *in_service; // 正在请求的结点
ul request_count;
};
/**
* @brief
*
*/
struct block_device
{
sector_t bd_start_sector; // 该分区的起始扇区
uint64_t bd_start_LBA; // 起始LBA号
sector_t bd_sectors_num; // 该分区的扇区数
struct vfs_superblock_t *bd_superblock; // 执行超级块的指针
struct blk_gendisk *bd_disk; // 当前分区所属的磁盘
uint16_t bd_partno; // 在磁盘上的分区号
};
// 定义blk_gendisk中的标志位
#define BLK_GF_AHCI (1 << 0)
/**
* @brief
*
*/
struct blk_gendisk
{
char disk_name[DISK_NAME_LEN]; // 磁盘驱动器名称
uint16_t part_cnt; // 磁盘分区计数
uint16_t flags;
struct block_device *partition; // 磁盘分区数组
const struct block_device_operation *fops; // 磁盘操作
void *private_data;
};

View File

@ -1,10 +0,0 @@
#pragma once
#include "blk_types.h"
/**
* @brief
*
* @param gendisk
* @return int
*/
int blk_register_gendisk(struct blk_gendisk * gendisk);

View File

@ -1,360 +0,0 @@
#pragma once
#include <common/glib.h>
// --> begin ==============EHDR=====================
// Reference: https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-43405.html#scrolltoc
// ====== ELF32 Header中的数据类型定义 ====
typedef uint32_t Elf32_Addr;
typedef uint16_t Elf32_Half;
typedef uint32_t Elf32_Off;
typedef uint32_t Elf32_SWord;
typedef uint32_t Elf32_Word;
// ====== ELF64 Header中的数据类型定义 ====
typedef uint64_t Elf64_Addr;
typedef uint16_t Elf64_Half;
typedef uint64_t Elf64_Off;
typedef uint32_t Elf64_Sword;
typedef uint32_t Elf64_Word;
typedef uint64_t Elf64_Xword;
typedef uint64_t Elf64_Sxword;
// ====== ELF Identification Index ======
// Purpose: File identification
#define EI_MAG0 0
#define EI_MAG1 1
#define EI_MAG2 2
#define EI_MAG3 3
// Purpose: File class
#define EI_CLASS 4
// Purpose: Data encoding
#define EI_DATA 5
// Purpose: File version
#define EI_VERSION 6 // e_ident[EI_VERSION]指定了ELF header的版本号 当前这个值必须是EV_CURRENT
// Purpose: Operating system/ABI identification
#define EI_OSABI 7 // e_ident[EI_OSABI]指定了操作系统以及对象所对应的ABI
// Purpose: ABI version
#define EI_ABIVERSION 8 // e_ident[EI_ABIVERSION] 指定了对象所对应的ABI版本.
// Purpose: Start of padding bytes
#define EI_PAD 9 // 这个值标志了e_ident中未使用字节的的起始下标
// Purpose: Size of e_ident[]
#define EI_NIDENT 16
// EI_MAG0 - EI_MAG3 这是一个4byte的 magic number
#define ELFMAG0 0x7f
#define ELFMAG1 'E'
#define ELFMAG2 'L'
#define ELFMAG3 'F'
// EI_CLASS e_ident[EI_CLASS]指明了文件的类型或capacity
#define ELFCLASSNONE 0 // Invalid class
#define ELFCLASS32 1 // 32bit objects
#define ELFCLASS64 2 // 64bit objects
// EI_DATA e_ident[EI_DATA]指明了与处理器相关的数据的编码方式
#define ELFDATANONE 0
#define ELFDATA2LSB 1 // 小端对齐
#define ELFDATA2MSB 2 // 大端对齐
// ELF e_type的类型定义
#define ET_NONE 0 // No file type
#define ET_REL 1 // Relocatable file
#define ET_EXEC 2 // Executable file
#define ET_DYN 3 // Shared object file
#define ET_CORE 4 // Core file
#define ET_LOPROC 0xff00 // Processor-specific
#define ET_HIPROC 0xffff // Processor-specific
// e_machine的类型定义
#define EM_NONE 0 // No machine
#define EM_SPARC 2 // SPARC
#define EM_386 3 // Intel 80386
#define EM_SPARC32PLUS 18 // Sun SPARC 32+
#define EM_SPARCV9 43 // SPARC V9
#define EM_AMD64 62 // AMD 64
// e_version的类型定义
#define EV_NONE 0 // Invalid Version
// EV_CURRENT: Value>=1 means current version
// e_flags 定义
// e_flags for SPARC
#define EF_SPARC_EXT_MASK 0xffff00 // Vendor Extension mask
#define EF_SPARC_32PLUS 0x000100 // Generic V8+ features
#define EF_SPARC_SUN_US1 0x000200 // Sun UltraSPARC 1 Extensions
#define EF_SPARC_HAL_R1 0x000400 // HAL R1 Extensions
#define EF_SPARC_SUN_US3 0x000800 // Sun UltraSPARC 3 Extensions
#define EF_SPARCV9_MM 0x3 // Mask for Memory Model
#define EF_SPARCV9_TSO 0x0 // Total Store Ordering
#define EF_SPARCV9_PSO 0x1 // Partial Store Ordering
#define EF_SPARCV9_RMO 0x2 // Relaxed Memory Ordering
#define PN_XNUM 0xffff
typedef struct
{
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
} Elf32_Ehdr;
typedef struct
{
unsigned char e_ident[EI_NIDENT]; // 标志字节,这些字节与机器架构类型无关。目的是为了告诉我们如何解析这个文件的内容
Elf64_Half e_type; // 文件类型标志符
Elf64_Half e_machine; // 该文件依赖的处理器架构类型
Elf64_Word e_version; // 对象文件的版本
Elf64_Addr e_entry; // 进程的虚拟地址入点使用字节偏移量表示。如果没有entry point则该值为0
Elf64_Off e_phoff; // The program header table's file offset in bytes. 若没有则为0
Elf64_Off e_shoff; // The section header table's file offset in bytes. 若没有则为0
Elf64_Word e_flags; // 与处理器相关联的flags。格式为 EF_machine_flag 如果是x86架构那么该值为0
Elf64_Half e_ehsize; // ELF Header的大小单位字节
Elf64_Half e_phentsize; // 程序的program header table中的一个entry的大小所有的entry大小相同
Elf64_Half e_phnum; // program header table的entry数量
// e_phentsize*e_phnum=program header table的大小
// 如果没有program header table该值为0
// 如果entry num>=PN_XNUM(0xffff), 那么该值为0xffff且真实的pht的entry数量存储在section header的sh_info中index=0
// 其他情况下第一个section header entry的sh_info的值为0
Elf64_Half e_shentsize; // 每个section header的大小字节
// 每个section header是section header table的一个entry
Elf64_Half e_shnum; // section header table的entry数量
// e_shentsize*e_shnum=section header table的大小
// 如果没有section header table那么该值为0
// 如果section的数量>=SHN_LORESERVE(0xff00)那么该值为0且真实的section数量存储在
// section header at index 0的sh_size变量中否则第一个sh_size为0
Elf64_Half e_shstrndx; // 与section name string表相关联的section header table的entry的索引下标
// 如果没有name string table,那么该值等于SHN_UNDEF
// 如果对应的index>=SHN_LORESERVE(0xff00) 那么该变量值为SHN_XINDEX(0xffff)
// 且真正的section name string table的index被存放在section header的index=0处的sh_link变量中
// 否则初始section header entry的sh_link变量为0
} Elf64_Ehdr;
// --> end ==============EHDR=====================
// --> begin ==============SHDR=====================
// reference: https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-94076.html#scrolltoc
// ===== ELF Special Section Indexes =====
#define SHN_UNDEF 0 // An undefined, missing, irrelevant, or otherwise meaningless section reference.
#define SHN_LORESERVE 0xff00 // The lower boundary of the range of reserved indexes.
// The system reserves indexes between SHN_LORESERVE and SHN_HIRESERVE, inclusive.
#define SHN_LOPROC 0xff00 // SHN_LOPROC - SHN_HIPROC 这个范围以内的数据为处理器特定的语义所保留
#define SHN_BEFORE 0xff00 // SHN_BEFORE, SHN_AFTER 与SHF_LINK_ORDER及SHF_ORDERED section flags一起提供初始和终止section的
#define SHN_AFTER 0xff01
#define SHN_AMD64_LCOMMON 0xff02 // x64 specific common block label. This label is similar to SHN_COMMON, but provides for identifying a large common block.
#define SHN_HIPROC 0xff1f
#define SHN_LOOS 0xff20 // SHN_LOOS - SHN_HIOS 这个范围你的数为操作系统特定的语义所保留
#define SHN_LOSUNW 0xff3f // SHN_LOSUNW - SHN_HISUNW Values in this inclusive range are reserved for Sun-specific semantics.
#define SHN_SUNW_IGNORE 0xff3f // This section index provides a temporary symbol definition within relocatable objects. Reserved for internal use by dtrace(1M).
#define SHN_HISUNW 0xff3f
#define SHN_HIOS 0xff3f
#define SHN_ABS 0xfff1 // 对应的引用的绝对值。 举个例子symbols defined relative to section number SHN_ABS have absolute values and are not affected by relocation.
#define SHN_COMMON 0xfff2 // Symbols defined relative to this section are common symbols
#define SHN_XINDEX 0xffff
#define SHN_HIRESERVE 0xffff // The upper boundary of the range of reserved indexes.
/*
Note -
Although index 0 is reserved as the undefined value,
the section header table contains an entry for index 0.
That is, if the e_shnum member of the ELF header indicates
a file has 6 entries in the section header table, the sections
have the indexes 0 through 5. The contents of the initial entry
are specified later in this section.
*/
typedef struct
{
Elf32_Word sh_name;
Elf32_Word sh_type;
Elf32_Word sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
Elf32_Word sh_size;
Elf32_Word sh_link;
Elf32_Word sh_info;
Elf32_Word sh_addralign;
Elf32_Word sh_entsize;
} Elf32_Shdr;
typedef struct
{
Elf64_Word sh_name; // 段名
Elf64_Word sh_type; // 段的类型(按照内容和语义来分类)
Elf64_Xword sh_flags;
Elf64_Addr sh_addr; // 该section在进程的内存空间中的基地址。如果该段不需要出现在内存中该值为0
Elf64_Off sh_offset; // The byte offset from the beginning of the file to the first byte in the section
// 对于一个 SHT_NOBITS section这个变量指的是概念上的偏移量。因为这种段并不是真正存在于文件中
Elf64_Xword sh_size; // The section's size in bytes(如果是SHT_NOBITS类型的sectionsection不会在文件中真正占用sh_size的空间)
Elf64_Word sh_link; // A section header table index link, whose interpretation depends on the section type.
Elf64_Word sh_info; // 依赖于section type来解析的额外的信息。如果sh_flags有SHF_INFO_LINK属性那么这个变量代表一个section header table index.
Elf64_Xword sh_addralign; // 地址按照多少bytes对齐。只允许使用2的n次幂的值。如果值为0或1则意味着地址没有对齐要求。
Elf64_Xword sh_entsize; // 如果某个段拥有指定size的entry则在这里指定否则为0
} Elf64_Shdr;
// ELF Section Types, sh_type
#define SHT_NULL 0
#define SHT_PROGBITS 1
#define SHT_SYMTAB 2 // Identifies a symbol table
#define SHT_STRTAB 3 // Identifies a string table.
#define SHT_RELA 4
#define SHT_HASH 5
#define SHT_DYNAMIC 6
#define SHT_NOTE 7
#define SHT_NOBITS 8
#define SHT_REL 9
#define SHT_SHLIB 10
#define SHT_DYNSYM 11 // Identifies a symbol table
#define SHT_INIT_ARRAY 14
#define SHT_FINI_ARRAY 15
#define SHT_PREINIT_ARRAY 16
#define SHT_GROUP 17
#define SHT_SYMTAB_SHNDX 18
#define SHT_LOOS 0x60000000
#define SHT_LOSUNW 0x6fffffef
#define SHT_SUNW_capchain 0x6fffffef
#define SHT_SUNW_capinfo 0x6ffffff0
#define SHT_SUNW_symsort 0x6ffffff1
#define SHT_SUNW_tlssort 0x6ffffff2
#define SHT_SUNW_LDYNSYM 0x6ffffff3 // Identifies a symbol table
#define SHT_SUNW_dof 0x6ffffff4
#define SHT_SUNW_cap 0x6ffffff5
#define SHT_SUNW_SIGNATURE 0x6ffffff6
#define SHT_SUNW_ANNOTATE 0x6ffffff7
#define SHT_SUNW_DEBUGSTR 0x6ffffff8
#define SHT_SUNW_DEBUG 0x6ffffff9
#define SHT_SUNW_move 0x6ffffffa
#define SHT_SUNW_COMDAT 0x6ffffffb
#define SHT_SUNW_syminfo 0x6ffffffc
#define SHT_SUNW_verdef 0x6ffffffd
#define SHT_SUNW_verneed 0x6ffffffe
#define SHT_SUNW_versym 0x6fffffff
#define SHT_HISUNW 0x6fffffff
#define SHT_HIOS 0x6fffffff
#define SHT_LOPROC 0x70000000
#define SHT_SPARC_GOTDATA 0x70000000
#define SHT_AMD64_UNWIND 0x70000001
#define SHT_HIPROC 0x7fffffff
#define SHT_LOUSER 0x80000000
#define SHT_HIUSER 0xffffffff
// ELF Section Attribute Flags
#define SHF_WRITE 0x1 // Identifies a section that should be writable during process execution
#define SHF_ALLOC 0x2 // Identifies a section that occupies memory during process execution
#define SHF_EXECINSTR 0x4 // contains executable machine instructions
#define SHF_MERGE 0x10
#define SHF_STRINGS 0x20
#define SHF_INFO_LINK 0x40 // This section headers sh_info field holds a section header table index
#define SHF_LINK_ORDER 0x80 // This section adds special ordering requirements to the link-editor
#define SHF_OS_NONCONFORMING 0x100
#define SHF_GROUP 0x200
#define SHF_TLS 0x400
#define SHF_MASKOS 0x0ff00000
#define SHF_AMD64_LARGE 0x10000000 // identifies a section that can hold more than 2 Gbyte
#define SHF_ORDERED 0x40000000
#define SHF_EXCLUDE 0x80000000
#define SHF_MASKPROC 0xf0000000
// --> end ==============SHDR=====================
// --> begin ========== symbol table section ======
typedef struct
{
Elf32_Word st_name;
Elf32_Addr st_value;
Elf32_Word st_size;
unsigned char st_info;
unsigned char st_other;
Elf32_Half st_shndx;
} Elf32_Sym;
typedef struct
{
Elf64_Word st_name;
unsigned char st_info;
unsigned char st_other;
Elf64_Half st_shndx;
Elf64_Addr st_value;
Elf64_Xword st_size;
} Elf64_Sym;
// --> end ========== symbol table section ======
// --> begin ========== program header =========
// Ref: https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-83432.html#scrolltoc
typedef struct
{
Elf32_Word p_type;
Elf32_Off p_offset;
Elf32_Addr p_vaddr;
Elf32_Addr p_paddr;
Elf32_Word p_filesz;
Elf32_Word p_memsz;
Elf32_Word p_flags;
Elf32_Word p_align;
} Elf32_Phdr;
typedef struct
{
Elf64_Word p_type;
Elf64_Word p_flags;
Elf64_Off p_offset;
Elf64_Addr p_vaddr;
Elf64_Addr p_paddr;
Elf64_Xword p_filesz;
Elf64_Xword p_memsz;
Elf64_Xword p_align;
} Elf64_Phdr;
// ELF segment types
#define PT_NULL 0
#define PT_LOAD 1 // Specifies a loadable segment
#define PT_DYNAMIC 2 // Specifies dynamic linking information.
#define PT_INTERP 3 // Specifies the location and size of a null-terminated path name to invoke as an interpreter
#define PT_NOTE 4 // Specifies the location and size of auxiliary information
#define PT_SHLIB 5
#define PT_PHDR 6 // Specifies the location and size of the program header table
#define PT_TLS 7 // Specifies a thread-local storage template
/*
PT_LOOS - PT_HIOS
Values in this inclusive range are reserved for OS-specific semantics.
*/
#define PT_LOOS 0x60000000
#define PT_SUNW_UNWIND 0x6464e550
#define PT_SUNW_EH_FRAME 0x6474e550
#define PT_LOSUNW 0x6ffffffa
#define PT_SUNWBSS 0x6ffffffa
#define PT_SUNWSTACK 0x6ffffffb
#define PT_SUNWDTRACE 0x6ffffffc
#define PT_SUNWCAP 0x6ffffffd
#define PT_HISUNW 0x6fffffff
#define PT_HIOS 0x6fffffff
#define PT_LOPROC 0x70000000
#define PT_HIPROC 0x7fffffff
// ELF Segment Flags
#define PF_X 0x1 // Execute
#define PF_W 0x2 // Write
#define PF_R 0x4 // Read
#define PF_MASKPROC 0xf0000000 // Unspecified
// --> end ========== program header =========

View File

@ -11,7 +11,6 @@
#include <common/stddef.h>
#include <arch/arch.h>
#include <common/compiler.h>
#include <common/list.h>
#include <asm/asm.h>
@ -29,13 +28,7 @@
(type *)((unsigned long)p - (unsigned long)&(((type *)0)->member)); \
})
// 定义类型的缩写
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ul;
typedef unsigned long long int ull;
typedef long long int ll;
#define ABS(x) ((x) > 0 ? (x) : -(x)) // 绝对值
// 最大最小值
@ -63,272 +56,6 @@ static __always_inline ul ALIGN(const ul addr, const ul _align)
return (ul)((addr + _align - 1) & (~(_align - 1)));
}
void *memset(void *dst, unsigned char C, ul size)
{
int d0, d1;
unsigned long tmp = C * 0x0101010101010101UL;
__asm__ __volatile__("cld \n\t"
"rep \n\t"
"stosq \n\t"
"testb $4, %b3 \n\t"
"je 1f \n\t"
"stosl \n\t"
"1:\ttestb $2, %b3 \n\t"
"je 2f\n\t"
"stosw \n\t"
"2:\ttestb $1, %b3 \n\t"
"je 3f \n\t"
"stosb \n\t"
"3: \n\t"
: "=&c"(d0), "=&D"(d1)
: "a"(tmp), "q"(size), "0"(size / 8), "1"(dst)
: "memory");
return dst;
}
void *memset_c(void *dst, uint8_t c, size_t count)
{
uint8_t *xs = (uint8_t *)dst;
while (count--)
*xs++ = c;
return dst;
}
/**
* @brief
*
* @param dst
* @param src
* @param Num
* @return void*
*/
static void *memcpy(void *dst, const void *src, long Num)
{
int d0 = 0, d1 = 0, d2 = 0;
__asm__ __volatile__("cld \n\t"
"rep \n\t"
"movsq \n\t"
"testb $4,%b4 \n\t"
"je 1f \n\t"
"movsl \n\t"
"1:\ttestb $2,%b4 \n\t"
"je 2f \n\t"
"movsw \n\t"
"2:\ttestb $1,%b4 \n\t"
"je 3f \n\t"
"movsb \n\t"
"3: \n\t"
: "=&c"(d0), "=&D"(d1), "=&S"(d2)
: "0"(Num / 8), "q"(Num), "1"(dst), "2"(src)
: "memory");
return dst;
}
// 从io口读入8个bit
unsigned char io_in8(unsigned short port)
{
unsigned char ret = 0;
__asm__ __volatile__("inb %%dx, %0 \n\t"
"mfence \n\t"
: "=a"(ret)
: "d"(port)
: "memory");
return ret;
}
// 从io口读入32个bit
unsigned int io_in32(unsigned short port)
{
unsigned int ret = 0;
__asm__ __volatile__("inl %%dx, %0 \n\t"
"mfence \n\t"
: "=a"(ret)
: "d"(port)
: "memory");
return ret;
}
// 输出8个bit到输出端口
void io_out8(unsigned short port, unsigned char value)
{
__asm__ __volatile__("outb %0, %%dx \n\t"
"mfence \n\t"
:
: "a"(value), "d"(port)
: "memory");
}
// 输出32个bit到输出端口
void io_out32(unsigned short port, unsigned int value)
{
__asm__ __volatile__("outl %0, %%dx \n\t"
"mfence \n\t"
:
: "a"(value), "d"(port)
: "memory");
}
/**
* @brief n个word到buffer
*
*/
#define io_insw(port, buffer, nr) \
__asm__ __volatile__("cld;rep;insw;mfence;" ::"d"(port), "D"(buffer), "c"(nr) \
: "memory")
/**
* @brief buffer中的n个word到端口
*
*/
#define io_outsw(port, buffer, nr) \
__asm__ __volatile__("cld;rep;outsw;mfence;" ::"d"(port), "S"(buffer), "c"(nr) \
: "memory")
/**
* @brief
*
* @param addr_start
* @param length
* @return true
* @return false
*/
bool verify_area(uint64_t addr_start, uint64_t length)
{
if ((addr_start + length) <= 0x00007fffffffffffUL) // 用户程序可用的的地址空间应<= 0x00007fffffffffffUL
return true;
else
return false;
}
/**
* @brief
*
* @param dst
* @param src
* @param size
* @return uint64_t
*/
static inline uint64_t copy_from_user(void *dst, void *src, uint64_t size)
{
uint64_t tmp0, tmp1;
if (!verify_area((uint64_t)src, size))
return 0;
/**
* @brief 8 bytesbyte搬运
*
*/
asm volatile("rep \n\t"
"movsq \n\t"
"movq %3, %0 \n\t"
"rep \n\t"
"movsb \n\t"
: "=&c"(size), "=&D"(tmp0), "=&S"(tmp1)
: "r"(size & 7), "0"(size >> 3), "1"(dst), "2"(src)
: "memory");
return size;
}
/**
* @brief
*
* @param dst
* @param src
* @param size
* @return uint64_t
*/
static inline uint64_t copy_to_user(void *dst, void *src, uint64_t size)
{
if (verify_area((uint64_t)src, size))
return 0;
/**
* @brief 8 bytesbyte搬运
*
*/
// todo:编译有bug
// asm volatile("rep \n\t"
// "movsq \n\t"
// "movq %3, %0 \n\t"
// "rep \n\t"
// "movsb \n\t"
// : "=&c"(size), "=&D"(tmp0), "=&S"(tmp1)
// : "r"(size & 7), "0"(size >> 3), "1"(dst), "2"(src)
// : "memory");
memcpy(dst, src, size);
return size;
}
/**
* @brief
*
* @param times
*/
void __experimental_beep(uint64_t times);
/**
* @brief 8
* 访mmio的时候
*
* @param vaddr
* @param value
*/
static __always_inline void __write8b(uint64_t vaddr, uint64_t value)
{
asm volatile("movq %%rdx, 0(%%rax)" ::"a"(vaddr), "d"(value)
: "memory");
}
/**
* @brief 4
* 访mmio的时候
*
* @param vaddr
* @param value
*/
static __always_inline void __write4b(uint64_t vaddr, uint32_t value)
{
asm volatile("movl %%edx, 0(%%rax)" ::"a"(vaddr), "d"(value)
: "memory");
}
/**
* @brief 8
* 访mmio的时候
*
* @param vaddr
* @return uint64_t
*/
static __always_inline uint64_t __read8b(uint64_t vaddr)
{
uint64_t retval;
asm volatile("movq 0(%%rax), %0"
: "=r"(retval)
: "a"(vaddr)
: "memory");
return retval;
}
/**
* @brief 4
* 访mmio的时候
*
* @param vaddr
* @return uint64_t
*/
static __always_inline uint32_t __read4b(uint64_t vaddr)
{
uint32_t retval;
asm volatile("movl 0(%%rax), %0"
: "=d"(retval)
: "a"(vaddr)
: "memory");
return retval;
}
/**
* @brief src搬运到dst

View File

@ -1,354 +0,0 @@
#pragma once
#include <common/stddef.h>
#include <asm/asm.h>
#include <common/compiler.h>
//链表数据结构
struct List
{
struct List *prev, *next;
};
//初始化循环链表
static inline void list_init(struct List *list)
{
list->next = list;
io_mfence();
list->prev = list;
}
/**
* @brief
* @param entry
* @param node
**/
static inline void list_add(struct List *entry, struct List *node)
{
node->next = entry->next;
barrier();
node->prev = entry;
barrier();
node->next->prev = node;
barrier();
entry->next = node;
}
/**
* @brief node添加到给定的list的结尾()
* @param entry
* @param node
*/
static inline void list_append(struct List *entry, struct List *node)
{
struct List *tail = entry->prev;
list_add(tail, node);
}
/**
* @brief
* @param entry
*/
static inline void list_del(struct List *entry)
{
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
}
/**
* @brief
*
*/
#define list_del_init(entry) \
list_del(entry); \
list_init(entry);
/**
* @brief 使NULL
*
* @param old
* @param new
*/
static inline void list_replace(struct List *old, struct List *new)
{
if (old->prev != NULL)
old->prev->next = new;
new->prev = old->prev;
if (old->next != NULL)
old->next->prev = new;
new->next = old->next;
old->prev = NULL;
old->next = NULL;
}
static inline bool list_empty(struct List *entry)
{
/**
* @brief
* @param entry
*/
if (entry == entry->next && entry->prev == entry)
return true;
else
return false;
}
/**
* @brief
*
* @param entry
* @return
*/
static inline struct List *list_prev(struct List *entry)
{
if (entry->prev != NULL)
return entry->prev;
else
return NULL;
}
/**
* @brief
*
* @param entry
* @return
*/
static inline struct List *list_next(struct List *entry)
{
if (entry->next != NULL)
return entry->next;
else
return NULL;
}
/**
* @brief entry的链表结构体
*
* @param ptr List结构体的指针
* @param type List结构体的外层结构体的类型
* @param member List结构体在上述的list结构体的结构体
*/
#define list_entry(ptr, type, member) container_of(ptr, type, member)
/**
* @brief
*
*
* @param ptr
* @param type List结构体的外层结构体的类型
* @param member List结构体在上述的list结构体的结构体
*/
#define list_first_entry(ptr, type, member) list_entry((ptr)->next, type, member)
/**
* @brief
* NULL
*
* @param ptr
* @param type List结构体的外层结构体的类型
* @param member List结构体在上述的list结构体的结构体
*/
#define list_first_entry_or_null(ptr, type, member) (!list_empty(ptr) ? list_entry((ptr)->next, type, member) : NULL)
/**
* @brief
*
*
* @param ptr
* @param type List结构体的外层结构体的类型
* @param member List结构体在上述的list结构体的结构体
*/
#define list_last_entry(ptr, type, member) list_entry((ptr)->prev, type, member)
/**
* @brief
* NULL
*
* @param ptr
* @param type List结构体的外层结构体的类型
* @param member List结构体在上述的list结构体的结构体
*/
#define list_last_entry_or_full(ptr, type, member) (!list_empty(ptr) ? list_entry((ptr)->prev, type, member) : NULL)
/**
* @brief
*
* @param pos
* @param member
*/
#define list_next_entry(pos, member) list_entry((pos)->member.next, typeof(*(pos)), member)
/**
* @brief
*
* @param pos
* @param member
*/
#define list_prev_entry(pos, member) list_entry((pos)->member.prev, typeof(*(pos)), member)
/**
* @brief
*
* @param ptr the &struct list_head to use as a loop cursor.
* @param head the head for your list.
*/
#define list_for_each(ptr, head) \
for ((ptr) = (head)->next; (ptr) != (head); (ptr) = (ptr)->next)
/**
* @brief
*
* @param ptr the &struct list_head to use as a loop cursor.
* @param head the head for your list.
*/
#define list_for_each_prev(ptr, head) \
for ((ptr) = (head)->prev; (ptr) != (head); (ptr) = (ptr)->prev)
/**
* @brief
* ptr所指向的链表结点从而造成错误
*
* @param ptr the &struct list_head to use as a loop cursor.
* @param n List类型的指针
* @param head the head for your list.
*/
#define list_for_each_safe(ptr, n, head) \
for ((ptr) = (head)->next, (n) = (ptr)->next; (ptr) != (head); (ptr) = n, n = (ptr)->next)
/**
* @brief
* ptr所指向的链表结点从而造成错误
*
* @param ptr the &struct list_head to use as a loop cursor.
* @param n List类型的指针
* @param head the head for your list.
*/
#define list_for_each_prev_safe(ptr, n, head) \
for ((ptr) = (head)->prev, (n) = (ptr)->prev; (ptr) != (head); (ptr) = n, n = (ptr)->prev)
/**
* @brief
*
* @param pos
* @param head
* @param member struct List在pos的结构体中的成员变量名
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* @brief
*
* @param pos
* @param n pos相同类型的指针
* @param head
* @param member struct List在pos的结构体中的成员变量名
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member), n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* @brief
*
* @param pos
* @param head
* @param member struct List在pos的结构体中的成员变量名
*/
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* @brief list_for_each_entry_continue()'pos'
*
* @param pos
* @param head struct List结构体的指针
* @param member struct List在pos的结构体中的成员变量名
*/
#define list_prepare_entry(pos, head, member) \
((pos) ? pos : list_entry(head, typeof(*pos), member))
/**
* @brief [],
*
* @param pos
* @param head struct List的指针
* @param member struct List在pos指向的结构体中的成员变量名
*/
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_next_entry(pos, member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* @brief [],
*
* @param pos
* @param n pos相同类型的指针
* @param head struct List的指针
* @param member struct List在pos指向的结构体中的成员变量名
*/
#define list_for_each_entry_safe_continue(pos, n, head, member) \
for (pos = list_next_entry(pos, member), n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* @brief [],
*
* @param pos
* @param head struct List的指针
* @param member struct List在pos指向的结构体中的成员变量名
*/
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* @brief [],
*
* @param pos
* @param head struct List的指针
* @param member struct List在pos指向的结构体中的成员变量名
*/
#define list_for_each_entry_safe_continue_reverse(pos, n, head, member) \
for (pos = list_prev_entry(pos, member), n = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_prev_entry(n, member))
/**
* @brief ,
*
* @param pos
* @param head struct List的指针
* @param member struct List在pos指向的结构体中的成员变量名
*/
#define list_for_each_entry_from(pos, head, member) \
for (; \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* @brief ,.
*
* @param pos
* @param n pos相同类型的指针
* @param head struct List的指针
* @param member struct List在pos指向的结构体中的成员变量名
*/
#define list_for_each_entry_safe_from(pos, n, head, member) \
for (n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))

View File

@ -13,20 +13,6 @@
#include <syscall/syscall.h>
#include <syscall/syscall_num.h>
/**
* @brief fork当前进程
*
* @return pid_t
*/
pid_t fork(void);
/**
* @brief vfork当前进程
*
* @return pid_t
*/
pid_t vfork(void);
/**
* @brief n字节
* @param src

View File

@ -17,9 +17,6 @@ pub struct Partition {
pub sectors_num: u64, // 该分区的扇区数
disk: Weak<dyn BlockDevice>, // 当前分区所属的磁盘
pub partno: u16, // 在磁盘上的分区号
// struct block_device_request_queue *bd_queue; // 请求队列
// struct vfs_superblock_t *bd_superblock; // 执行超级块的指针
}
/// @brief: 分区信息 - 成员函数

View File

@ -44,7 +44,7 @@ hardware_intr_controller ps2_keyboard_intr_controller =
* @param filp
* @return long
*/
long ps2_keyboard_open(struct vfs_index_node_t *inode, struct vfs_file_t *filp)
long ps2_keyboard_open(void *inode, void *filp)
{
ps2_keyboard_reset_buffer(&kb_buf);
return 0;
@ -57,7 +57,7 @@ long ps2_keyboard_open(struct vfs_index_node_t *inode, struct vfs_file_t *filp)
* @param filp
* @return long
*/
long ps2_keyboard_close(struct vfs_index_node_t *inode, struct vfs_file_t *filp)
long ps2_keyboard_close(void *inode, void *filp)
{
ps2_keyboard_reset_buffer(&kb_buf);
return 0;
@ -72,7 +72,7 @@ long ps2_keyboard_close(struct vfs_index_node_t *inode, struct vfs_file_t *filp)
* @param arg
* @return long
*/
long ps2_keyboard_ioctl(struct vfs_index_node_t *inode, struct vfs_file_t *filp, uint64_t cmd, uint64_t arg)
long ps2_keyboard_ioctl(void *inode, void *filp, uint64_t cmd, uint64_t arg)
{
switch (cmd)
{
@ -95,7 +95,7 @@ long ps2_keyboard_ioctl(struct vfs_index_node_t *inode, struct vfs_file_t *filp,
* @param position
* @return long
*/
long ps2_keyboard_read(struct vfs_file_t *filp, char *buf, int64_t count, long *position)
long ps2_keyboard_read(void *filp, char *buf, int64_t count, long *position)
{
// 缓冲区为空则等待
while (kfifo_empty(&kb_buf))
@ -114,7 +114,7 @@ long ps2_keyboard_read(struct vfs_file_t *filp, char *buf, int64_t count, long *
* @param position
* @return long
*/
long ps2_keyboard_write(struct vfs_file_t *filp, char *buf, int64_t count, long *position)
long ps2_keyboard_write(void *filp, char *buf, int64_t count, long *position)
{
return 0;
}

View File

@ -1,4 +1,4 @@
use core::sync::atomic::AtomicI32;
use core::{ffi::c_void, sync::atomic::AtomicI32};
use alloc::sync::{Arc, Weak};
@ -11,7 +11,7 @@ use crate::{
Metadata, PollStatus,
},
},
include::bindings::bindings::{vfs_file_operations_t, vfs_file_t, vfs_index_node_t},
include::bindings::bindings::vfs_file_operations_t,
libs::{keyboard_parser::TypeOneFSM, rwlock::RwLock, spinlock::SpinLock},
syscall::SystemError,
time::TimeSpec,
@ -104,7 +104,7 @@ impl IndexNode for LockedPS2KeyBoardInode {
let func = guard.f_ops.read.unwrap();
let r = unsafe {
func(
0 as *mut vfs_file_t,
0 as *mut c_void,
&mut buf[0..len] as *mut [u8] as *mut i8,
len as i64,
0 as *mut i64,
@ -133,7 +133,7 @@ impl IndexNode for LockedPS2KeyBoardInode {
// 第一次打开,需要初始化
let guard = self.0.write();
let func = guard.f_ops.open.unwrap();
let _ = unsafe { func(0 as *mut vfs_index_node_t, 0 as *mut vfs_file_t) };
let _ = unsafe { func(0 as *mut c_void, 0 as *mut c_void) };
}
return Ok(());
}
@ -147,7 +147,7 @@ impl IndexNode for LockedPS2KeyBoardInode {
// 最后一次关闭,需要释放
let guard = self.0.write();
let func = guard.f_ops.close.unwrap();
let _ = unsafe { func(0 as *mut vfs_index_node_t, 0 as *mut vfs_file_t) };
let _ = unsafe { func(0 as *mut c_void, 0 as *mut c_void) };
}
return Ok(());
}

View File

@ -10,22 +10,15 @@ use alloc::{boxed::Box, sync::Arc};
use num_traits::FromPrimitive;
use crate::{
arch::interrupt::{cli, sti},
arch::CurrentIrqArch,
exception::InterruptArch,
include::bindings::bindings::MAX_CPU_NUM,
kdebug, kinfo,
libs::rwlock::RwLock,
process::ProcessManager,
smp::core::smp_get_processor_id,
syscall::SystemError,
time::timer::clock,
arch::CurrentIrqArch, exception::InterruptArch, kdebug, kinfo, libs::rwlock::RwLock,
mm::percpu::PerCpu, process::ProcessManager, smp::core::smp_get_processor_id,
syscall::SystemError, time::timer::clock,
};
const MAX_SOFTIRQ_NUM: u64 = 64;
const MAX_SOFTIRQ_RESTART: i32 = 20;
static mut __CPU_PENDING: Option<Box<[VecStatus; MAX_CPU_NUM as usize]>> = None;
static mut __CPU_PENDING: Option<Box<[VecStatus; PerCpu::MAX_CPU_NUM]>> = None;
static mut __SORTIRQ_VECTORS: *mut Softirq = null_mut();
#[no_mangle]
@ -37,9 +30,11 @@ pub fn softirq_init() -> Result<(), SystemError> {
kinfo!("Initializing softirq...");
unsafe {
__SORTIRQ_VECTORS = Box::leak(Box::new(Softirq::new()));
__CPU_PENDING = Some(Box::new([VecStatus::default(); MAX_CPU_NUM as usize]));
__CPU_PENDING = Some(Box::new(
[VecStatus::default(); PerCpu::MAX_CPU_NUM as usize],
));
let cpu_pending = __CPU_PENDING.as_mut().unwrap();
for i in 0..MAX_CPU_NUM {
for i in 0..PerCpu::MAX_CPU_NUM {
cpu_pending[i as usize] = VecStatus::default();
}
}
@ -177,7 +172,7 @@ impl Softirq {
cpu_pending(cpu_id as usize).bits = 0;
compiler_fence(Ordering::SeqCst);
sti();
unsafe { CurrentIrqArch::interrupt_enable() };
if pending != 0 {
for i in 0..MAX_SOFTIRQ_NUM {
if pending & (1 << i) == 0 {
@ -205,7 +200,7 @@ impl Softirq {
}
}
}
cli();
unsafe { CurrentIrqArch::interrupt_disable() };
max_restart -= 1;
compiler_fence(Ordering::SeqCst);
if cpu_pending(cpu_id as usize).is_empty() {

View File

@ -11,184 +11,14 @@
#pragma once
#include <common/blk_types.h>
#include <common/fcntl.h>
#include <common/glib.h>
#include <common/lockref.h>
#include <DragonOS/stdint.h>
#include <mm/slab.h>
#define VFS_DPT_MBR 0 // MBR分区表
#define VFS_DPT_GPT 1 // GPT分区表
#define VFS_MAX_PATHLEN 4096
/**
* @brief inode的属性
*
*/
#define VFS_IF_FILE (1UL << 0)
#define VFS_IF_DIR (1UL << 1) // 文件夹
#define VFS_IF_DEVICE (1UL << 2)
#define VFS_IF_DEAD (1UL << 3) /* removed, but still open directory */
struct vfs_super_block_operations_t;
struct vfs_inode_operations_t;
struct vfs_index_node_t;
struct vfs_dir_entry_operations_t;
#define VFS_DF_MOUNTED (1 << 0) // 当前dentry是一个挂载点
#define VFS_DF_CANNOT_MOUNT (1 << 1) // 当前dentry是一个挂载点
struct vfs_dir_entry_t
{
char *name;
int name_length; // 名字的长度(不包含字符串末尾的'\0'
uint32_t d_flags; // dentry标志位
struct List child_node_list;
struct List subdirs_list;
struct lockref lockref; // 该lockref包含了dentry的自旋锁以及引用计数
struct vfs_index_node_t *dir_inode;
struct vfs_dir_entry_t *parent;
struct vfs_dir_entry_operations_t *dir_ops;
};
struct vfs_superblock_t
{
struct vfs_dir_entry_t *root;
struct vfs_super_block_operations_t *sb_ops;
struct vfs_dir_entry_operations_t *dir_ops; // dentry's operations
struct block_device *blk_device;
void *private_sb_info;
};
/**
* @brief inode结构体
*
*/
struct vfs_index_node_t
{
uint64_t file_size; // 文件大小
uint64_t blocks; // 占用的扇区数
uint64_t attribute;
struct lockref lockref; // 自旋锁与引用计数
struct vfs_superblock_t *sb;
struct vfs_file_operations_t *file_ops;
struct vfs_inode_operations_t *inode_ops;
void *private_inode_info;
};
/**
* @brief mode
*
*/
#define VFS_FILE_MODE_READ (1 << 0)
#define VFS_FILE_MODE_WRITE (1 << 1)
#define VFS_FILE_MODE_RW (VFS_FILE_MODE_READ | VFS_FILE_MODE_WRITE)
#define vfs_file_can_read(file) (((file)->mode) & VFS_FILE_MODE_READ)
#define vfs_file_can_write(file) (((file)->mode) & VFS_FILE_MODE_WRITE)
#define vfs_file_can_rw(file) ((((file)->mode) & VFS_FILE_MODE_RW) == VFS_FILE_MODE_RW)
/**
* @brief
*
*/
struct vfs_file_t
{
long position;
uint64_t mode;
struct vfs_dir_entry_t *dEntry;
struct vfs_file_operations_t *file_ops;
void *private_data;
};
struct vfs_filesystem_type_t
{
char *name;
int fs_flags;
struct vfs_superblock_t *(*read_superblock)(
struct block_device *blk); // 解析文件系统引导扇区的函数,为文件系统创建超级块结构。
struct vfs_filesystem_type_t *next;
};
struct vfs_super_block_operations_t
{
void (*write_superblock)(struct vfs_superblock_t *sb); // 将超级块信息写入磁盘
void (*put_superblock)(struct vfs_superblock_t *sb);
void (*write_inode)(struct vfs_index_node_t *inode); // 将inode信息写入磁盘
};
/**
* @brief vfs的inode的操作抽象
*
*/
struct vfs_inode_operations_t
{
/**
* @brief
* @param parent_inode inode结构体
* @param dest_dEntry dentry
* @param mode
*/
long (*create)(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry, int mode);
/**
* @brief
* @param parent_inode
* @param dest_dEntry
*
*/
struct vfs_dir_entry_t *(*lookup)(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry);
/**
* @brief
* @param inode inode
* @param dEntry dentry
* @param mode mode
*/
long (*mkdir)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry, int mode);
long (*rmdir)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry);
long (*rename)(struct vfs_index_node_t *old_inode, struct vfs_dir_entry_t *old_dEntry,
struct vfs_index_node_t *new_inode, struct vfs_dir_entry_t *new_dEntry);
long (*getAttr)(struct vfs_dir_entry_t *dEntry, uint64_t *attr);
long (*setAttr)(struct vfs_dir_entry_t *dEntry, uint64_t *attr);
/**
* @brief inode和dentry之间的链接关系
*
* @param inode
* @param dentry
*/
long (*unlink)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dentry);
};
struct vfs_dir_entry_operations_t
{
long (*compare)(struct vfs_dir_entry_t *parent_dEntry, char *source_filename, char *dest_filename);
long (*hash)(struct vfs_dir_entry_t *dEntry, char *filename);
long (*release)(struct vfs_dir_entry_t *dEntry);
long (*iput)(struct vfs_dir_entry_t *dEntry, struct vfs_index_node_t *inode);
};
/**
* @brief dirent的函数指针的类型定义
*
*/
typedef int (*vfs_filldir_t)(void *buf, ino_t d_ino, char *name, int namelen, unsigned char type, off_t offset);
struct vfs_file_operations_t
{
long (*open)(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr);
long (*close)(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr);
long (*read)(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position);
long (*write)(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position);
long (*lseek)(struct vfs_file_t *file_ptr, long offset, long origin);
long (*ioctl)(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr, uint64_t cmd, uint64_t arg);
long (*readdir)(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t filler); // 读取文件夹
long (*open)(void *not_used, void *not_used1);
long (*close)(void *not_used, void *not_used1);
long (*read)(void *not_used1, char *buf, int64_t count, long *position);
long (*write)(void *not_used1, char *buf, int64_t count, long *position);
long (*lseek)(void *not_used1, long offset, long origin);
long (*ioctl)(void *not_used, void *not_used1, uint64_t cmd, uint64_t arg);
};
/**

View File

@ -9,7 +9,7 @@ use alloc::{
use crate::{
driver::base::{block::SeekFrom, device::DeviceNumber},
filesystem::vfs::file::FileDescriptorVec,
include::bindings::bindings::{verify_area, PROC_MAX_FD_NUM},
include::bindings::bindings::verify_area,
kerror,
libs::rwlock::RwLockWriteGuard,
mm::VirtAddr,
@ -465,7 +465,7 @@ impl Syscall {
let dirent =
unsafe { (buf.as_mut_ptr() as *mut Dirent).as_mut() }.ok_or(SystemError::EFAULT)?;
if fd < 0 || fd as u32 > PROC_MAX_FD_NUM {
if fd < 0 || fd as usize > FileDescriptorVec::PROCESS_MAX_FD {
return Err(SystemError::EBADF);
}

View File

@ -10,7 +10,6 @@
*/
#pragma once
#include <common/blk_types.h>
#include <common/crc16.h>
#include <common/crc32.h>
#include <common/crc64.h>
@ -19,7 +18,6 @@
#include <common/glib.h>
#include <common/idr.h>
#include <common/kfifo.h>
#include <common/list.h>
#include <common/lz4.h>
#include <common/printk.h>
#include <common/spinlock.h>
@ -31,7 +29,6 @@
#include <exception/gate.h>
#include <include/DragonOS/refcount.h>
#include <libs/lib_ui/textui.h>
#include <mm/mm-types.h>
#include <mm/mm.h>
#include <mm/mmio.h>
#include <mm/slab.h>

View File

@ -1,31 +1,6 @@
#include <common/glib.h>
#include <common/string.h>
/**
* @brief
*
* @param times
*/
void __experimental_beep(uint64_t times)
{
io_out8(0x43, 182&0xff);
io_out8(0x42, 2280&0xff);
io_out8(0x42, (2280>>8)&0xff);
uint32_t x = io_in8(0x61)&0xff;
x |= 3;
io_out8(0x61, x&0xff);
times *= 10000;
for(uint64_t i=0;i<times;++i)
pause();
x = io_in8(0x61);
x &= 0xfc;
io_out8(0x61, x&0xff);
// 延迟一段时间
for(uint64_t i=0;i<times;++i)
pause();
}
/**
* @brief src搬运到dst

View File

@ -1,25 +1,6 @@
#include <common/unistd.h>
#include <common/glib.h>
/**
* @brief fork当前进程
*
* @return pid_t
*/
pid_t fork(void)
{
return (pid_t)enter_syscall_int(SYS_FORK, 0, 0, 0, 0, 0, 0, 0, 0);
}
/**
* @brief vfork当前进程
*
* @return pid_t
*/
pid_t vfork(void)
{
return (pid_t)enter_syscall_int(SYS_VFORK, 0, 0, 0, 0, 0, 0, 0, 0);
}
void swab(void *restrict src, void *restrict dest, ssize_t nbytes)
{

View File

@ -1,215 +0,0 @@
//
// Created by longjin on 2022/1/20.
//
#include "common/glib.h"
#include "common/kprint.h"
#include "common/printk.h"
#include "exception/gate.h"
#include "exception/irq.h"
#include "exception/trap.h"
#include "mm/mm.h"
#include "mm/slab.h"
#include "process/process.h"
#include "smp/smp.h"
#include "syscall/syscall.h"
#include <exception/softirq.h>
#include <libs/lib_ui/screen_manager.h>
#include <libs/lib_ui/textui.h>
#include <sched/sched.h>
#include <smp/ipi.h>
#include <filesystem/vfs/VFS.h>
#include "driver/acpi/acpi.h"
#include "driver/disk/ata.h"
#include "driver/keyboard/ps2_keyboard.h"
#include "driver/mouse/ps2_mouse.h"
#include "driver/multiboot2/multiboot2.h"
#include <time/timer.h>
#include <arch/x86_64/driver/apic/apic_timer.h>
#include <virt/kvm/kvm.h>
extern int rs_driver_init();
extern void rs_softirq_init();
extern void rs_mm_init();
extern void rs_kthread_init();
extern void rs_init_intertrait();
extern void rs_init_before_mem_init();
extern int rs_setup_arch();
extern void rs_futex_init();
extern int rs_hpet_init();
extern int rs_hpet_enable();
extern int rs_tsc_init();
ul bsp_idt_size, bsp_gdt_size;
#pragma GCC push_options
#pragma GCC optimize("O0")
struct gdtr gdtp;
struct idtr idtp;
ul _stack_start;
void reload_gdt()
{
gdtp.size = bsp_gdt_size - 1;
gdtp.gdt_vaddr = (ul)phys_2_virt((ul)&GDT_Table);
asm volatile("lgdt (%0) \n\t" ::"r"(&gdtp)
: "memory");
}
void reload_idt()
{
idtp.size = bsp_idt_size - 1;
idtp.idt_vaddr = (ul)phys_2_virt((ul)&IDT_Table);
// kdebug("gdtvaddr=%#018lx", p.gdt_vaddr);
// kdebug("gdt size=%d", p.size);
asm volatile("lidt (%0) \n\t" ::"r"(&idtp)
: "memory");
}
// 初始化系统各模块
void system_initialize()
{
rs_init_before_mem_init();
// 重新加载gdt和idt
ul tss_item_addr = (ul)phys_2_virt(0x7c00);
_stack_start = head_stack_start; // 保存init proc的栈基地址由于之后取消了地址重映射因此必须在这里重新保存
kdebug("_stack_start=%#018lx", _stack_start);
set_current_core_tss(_stack_start, 0);
rs_load_current_core_tss();
cpu_core_info[0].stack_start = _stack_start;
// 初始化中断描述符表
sys_vector_init();
// 初始化内存管理单元
// mm_init();
rs_mm_init();
// 内存管理单元初始化完毕后,需要立即重新初始化显示驱动。
// 原因是系统启动初期framebuffer被映射到48M地址处
// mm初始化完毕后若不重新初始化显示驱动将会导致错误的数据写入内存从而造成其他模块崩溃
// 对显示模块进行低级初始化不启用double buffer
io_mfence();
scm_reinit();
rs_textui_init();
rs_init_intertrait();
// kinfo("vaddr:%#018lx", video_frame_buffer_info.vaddr);
io_mfence();
vfs_init();
rs_driver_init();
acpi_init();
rs_setup_arch();
io_mfence();
irq_init();
rs_process_init();
sched_init();
sti();
io_mfence();
rs_softirq_init();
syscall_init();
io_mfence();
rs_timekeeping_init();
io_mfence();
rs_timer_init();
io_mfence();
rs_jiffies_init();
io_mfence();
rs_kthread_init();
io_mfence();
io_mfence();
rs_clocksource_boot_finish();
io_mfence();
cpu_init();
ps2_keyboard_init();
io_mfence();
rs_pci_init();
// 这里必须加内存屏障,否则会出错
io_mfence();
smp_init();
io_mfence();
rs_futex_init();
cli();
rs_hpet_init();
rs_hpet_enable();
rs_tsc_init();
io_mfence();
kvm_init();
io_mfence();
// 系统初始化到此结束,剩下的初始化功能应当放在初始内核线程中执行
apic_timer_init();
// while(1);
io_mfence();
sti();
while (1)
;
}
// 操作系统内核从这里开始执行
void Start_Kernel(void)
{
// 获取multiboot2的信息
uint64_t mb2_info, mb2_magic;
__asm__ __volatile__("movq %%r15, %0 \n\t"
"movq %%r14, %1 \n\t"
"movq %%r13, %2 \n\t"
"movq %%r12, %3 \n\t"
: "=r"(mb2_info), "=r"(mb2_magic), "=r"(bsp_gdt_size), "=r"(bsp_idt_size)::"memory");
reload_gdt();
reload_idt();
mb2_info &= 0xffffffff;
mb2_magic &= 0xffffffff;
multiboot2_init(mb2_info, mb2_magic);
io_mfence();
system_initialize();
io_mfence();
// idle
while (1)
{
// 如果调用的时候启用了中断则hlt。否则认为是bug
if (get_rflags() & 0x200)
{
// kdebug("hlt");
hlt();
}
else
{
BUG_ON(1);
pause();
}
}
}
#pragma GCC pop_options

View File

@ -7,7 +7,7 @@ use hashbrown::HashMap;
use crate::{
arch::mm::LowAddressRemapping,
include::bindings::bindings::{gfp_t, vm_flags_t, PAGE_U_S},
include::bindings::bindings::{gfp_t, PAGE_U_S},
kerror,
libs::{align::page_align_up, spinlock::SpinLock},
mm::MMArch,
@ -140,7 +140,7 @@ pub unsafe extern "C" fn rs_unmap_at_low_addr() -> usize {
#[no_mangle]
unsafe extern "C" fn rs_mmio_create(
size: u32,
_vm_flags: vm_flags_t,
_vm_flags: u64,
res_vaddr: *mut u64,
res_length: *mut u64,
) -> i32 {

View File

@ -1,6 +0,0 @@
#pragma once
#include <common/glib.h>
#include <common/spinlock.h>
#include <common/atomic.h>
typedef uint64_t vm_flags_t;

View File

@ -1,7 +1,6 @@
#pragma once
#include <common/glib.h>
#include <mm/mm-types.h>
#include <process/process.h>
extern void rs_pseudo_map_phys(uint64_t virt_addr, uint64_t phys_addr, uint64_t size);

View File

@ -1,6 +1,4 @@
#pragma once
#include "proc-types.h"
extern void rs_preempt_disable();
extern void rs_preempt_enable();

View File

@ -1,22 +0,0 @@
#pragma once
#include "ptrace.h"
#include <DragonOS/stdint.h>
// 进程最大可拥有的文件描述符数量
#define PROC_MAX_FD_NUM 16
// 进程的内核栈大小 32K
#define STACK_SIZE 32768
// 进程的运行状态
// 正在运行
#define PROC_RUNNING (1 << 0)
// 可被信号打断
#define PROC_INTERRUPTIBLE (1 << 1)
// 不可被信号打断
#define PROC_UNINTERRUPTIBLE (1 << 2)
// 挂起
#define PROC_ZOMBIE (1 << 3)
// 已停止
#define PROC_STOPPED (1 << 4)

View File

@ -14,11 +14,8 @@
#include <common/errno.h>
#include <common/glib.h>
#include <filesystem/vfs/VFS.h>
#include <mm/mm-types.h>
#include <syscall/syscall.h>
#include "proc-types.h"
/**
* @brief 退
*

View File

@ -2,7 +2,7 @@
#include <arch/arch.h>
#if ARCH(I386) || ARCH(X86_64)
#include <arch/x86_64/x86_64_ipi.h>
#include <arch/x86_64/include/x86_64_ipi.h>
#else
#error "error type of arch!"
#endif