├── .gitignore
├── LICENSE
├── Makefile
├── README.md
├── TODO
├── TayhuangOS.png
├── TayhuangOS.svg
├── configs
└── grub.cfg
├── doc
├── OS抽象层.md
├── OS版本规则.md
├── 代码规范.md
├── 内核加载标准.md
├── 内核模块标准.md
├── 内核细节.md
└── 系统调用.md
├── include
├── elf.h
├── std
│ ├── assert.h
│ ├── ctype.h
│ ├── math.h
│ ├── stdarg.h
│ ├── stdbool.h
│ ├── stddef.h
│ ├── stdint.h
│ └── string.h
├── tayhuang
│ ├── boot_args.h
│ ├── control_registers.h
│ ├── defs.h
│ ├── descs.h
│ ├── int_vectors.h
│ ├── io.h
│ ├── kmod_prototypes.h
│ ├── msgpack.h
│ ├── paging.h
│ ├── partition.h
│ ├── ports.h
│ ├── services.h
│ ├── types.h
│ └── video_info.h
└── tool
│ └── tostring.h
├── kernel
├── Makefile
├── README.md
├── entry.S
├── export
│ └── sys_task
│ │ ├── __sys_task_fn.c
│ │ └── __sys_task_fn.h
├── global.h
├── intterup
│ ├── clock.c
│ ├── clock.h
│ ├── exception.c
│ ├── exception.h
│ ├── init_int.c
│ ├── init_int.h
│ ├── int_handlers.S
│ ├── irq_handler.c
│ └── irq_handler.h
├── kernel.c
├── kernel.ld
├── kmod_loader.c
├── kmod_loader.h
├── libs
│ ├── ctype.c
│ ├── debug.c
│ ├── string.c
│ └── tostring.c
├── logging.c
├── logging.h
├── memory
│ ├── kheap.c
│ ├── kheap.h
│ ├── paging.c
│ ├── paging.h
│ ├── pmm.c
│ ├── pmm.h
│ ├── shared_memory.c
│ └── shared_memory.h
├── printk.c
├── printk.h
├── syscall
│ ├── dosyscall.S
│ ├── rpc.c
│ ├── rpc.h
│ ├── sys_task.c
│ ├── sys_task.h
│ ├── syscall.c
│ ├── syscall.h
│ ├── syscalls.c
│ └── syscalls.h
└── task
│ ├── signal.c
│ ├── signal.h
│ ├── task_manager.c
│ ├── task_manager.h
│ ├── task_scheduler.c
│ ├── task_scheduler.h
│ ├── task_struct.h
│ ├── thread.c
│ └── thread.h
├── libs
├── Makefile
├── libc
│ ├── Makefile
│ ├── README.md
│ ├── ipc
│ │ ├── ipc.h
│ │ └── ipc_func.S
│ └── misc
│ │ ├── timer.c
│ │ └── timer.h
├── libfifo
│ ├── Makefile
│ ├── fifo.c
│ └── fifo.h
└── libkmod
│ ├── Makefile
│ ├── README.md
│ ├── debug
│ ├── debug.c
│ ├── logging.c
│ └── logging.h
│ ├── entry.S
│ ├── export
│ ├── keyboard
│ │ ├── __keyboard_driver_fn.c
│ │ ├── __keyboard_driver_fn.h
│ │ └── key_types.h
│ ├── sys_task
│ │ ├── __sys_task_fn.c
│ │ └── __sys_task_fn.h
│ ├── tty
│ │ ├── __tty_driver_fn.c
│ │ └── __tty_driver_fn.h
│ └── video
│ │ ├── __video_driver_fn.c
│ │ └── __video_driver_fn.h
│ ├── init.c
│ ├── libs
│ ├── ctype.c
│ ├── string.c
│ └── tostring.c
│ ├── memory
│ ├── malloc.c
│ ├── malloc.h
│ └── sharemem.h
│ ├── printf.c
│ ├── printf.h
│ └── syscall
│ ├── ipc.c
│ ├── ipc.h
│ ├── rpc.c
│ ├── rpc.h
│ ├── syscall.S
│ └── syscall.h
├── loader
├── Makefile
└── grub_loader
│ ├── Makefile
│ ├── README.md
│ ├── _int_handlers.asm
│ ├── disk.c
│ ├── disk.h
│ ├── fs
│ ├── common.c
│ ├── common.h
│ ├── fat32.c
│ └── fat32.h
│ ├── info_parser.c
│ ├── info_parser.h
│ ├── init.c
│ ├── init.h
│ ├── int_handlers.c
│ ├── int_handlers.h
│ ├── lheap.c
│ ├── lheap.h
│ ├── libs
│ ├── ctype.c
│ ├── multiboot2.h
│ ├── string.c
│ └── tostring.c
│ ├── lm
│ ├── lm_operators.asm
│ ├── lm_operators.h
│ ├── load_kernel.c
│ ├── load_kernel.h
│ ├── setup_lm.c
│ └── setup_lm.h
│ ├── loader.ld
│ ├── logging.c
│ ├── logging.h
│ ├── main.c
│ ├── printf.c
│ ├── printf.h
│ ├── show_icon.c
│ └── show_icon.h
├── module
├── Makefile
├── driver
│ ├── Makefile
│ ├── keyboard
│ │ ├── Makefile
│ │ ├── driver
│ │ │ ├── Makefile
│ │ │ ├── import
│ │ │ │ └── __ioman.c
│ │ │ ├── keyboard.ld
│ │ │ └── main.c
│ │ ├── include
│ │ │ └── export
│ │ │ │ └── __ioman.h
│ │ └── ioman
│ │ │ ├── Makefile
│ │ │ ├── key_parser.c
│ │ │ ├── key_parser.h
│ │ │ ├── keyboard.ld
│ │ │ ├── keymap.c
│ │ │ ├── keymap.h
│ │ │ └── main.c
│ ├── vdd
│ │ ├── Makefile
│ │ └── tty
│ │ │ ├── Makefile
│ │ │ ├── console.c
│ │ │ ├── console.h
│ │ │ ├── main.c
│ │ │ └── tty.ld
│ └── video
│ │ ├── Makefile
│ │ ├── framebuffers.c
│ │ ├── framebuffers.h
│ │ ├── global.h
│ │ ├── main.c
│ │ ├── text.c
│ │ └── video.ld
├── setup
│ ├── Makefile
│ ├── _libkmod
│ │ ├── README.md
│ │ ├── debug
│ │ │ ├── debug.c
│ │ │ ├── logging.c
│ │ │ └── logging.h
│ │ ├── entry.S
│ │ ├── init.c
│ │ ├── libs
│ │ │ ├── ctype.c
│ │ │ ├── string.c
│ │ │ └── tostring.c
│ │ ├── memory
│ │ │ ├── malloc.c
│ │ │ └── malloc.h
│ │ ├── printf.c
│ │ ├── printf.h
│ │ └── syscall
│ │ │ ├── ipc.h
│ │ │ ├── syscall.S
│ │ │ └── syscall.h
│ ├── disk.c
│ ├── disk.h
│ ├── fs
│ │ ├── common.c
│ │ ├── common.h
│ │ ├── fat32.c
│ │ └── fat32.h
│ ├── main.c
│ └── setup.ld
└── testbench
│ ├── Makefile
│ ├── testbench1
│ ├── Makefile
│ ├── main.c
│ └── testbench1.ld
│ └── testbench2
│ ├── Makefile
│ ├── main.c
│ └── testbench2.ld
├── setup
├── MakefileA
├── MakefileB
├── c_cpp_properties.json
├── config.mk
└── settings.json
├── tools
├── build_counter
│ └── counter.py
├── comments_stat
│ └── stat.py
├── get_loop_devices
│ └── get_loop.sh
└── png_converter
│ └── converter.py
└── version.mk
/.gitignore:
--------------------------------------------------------------------------------
1 | *.log
2 | .vscode/
3 | config.mk
4 | !setup/config.mk
5 | bios/
6 | build/
7 | bochsrc.bxrc
8 | bochsrc
9 | tayhuangOS.img
10 | tayhuangBoot.img
11 | bx_enh_dbg.ini
12 | configs/times.txt
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # English Version
2 |
3 | # 英文版
4 |
5 |
6 |
7 |
8 |
9 |
10 | ### Tayhuang OS
11 |
12 |
13 |
14 | > There was a person called Pangu. He borned in Tayhuang.
15 |
16 | It's a OS made by me
17 |
18 | If you want to download a operating system for use, please look for other repo.It has not be done.
19 |
20 | Everyone is welcome to program OS with me, if you have some experience about it.
21 |
22 | I'm glad to talk with you about the theory of OS.
23 |
24 | There's my QQ number: 2715626245
25 |
26 | Or you can send an email to me, my email address is: song_of_the_fly@163.com
27 |
28 |
29 | ---
30 |
31 | # Chinese Version
32 |
33 | # 中文版
34 |
35 |
36 |
44 |
45 | > 粤自盘古,生于太荒。
46 |
47 | 这是我自制的OS
48 |
49 | 如果你想要下载一个操作系统来用,请去找别的repo,因为它还没做好
50 |
51 | 我欢迎任何人和我一起写OS,只要你有这方面的一点知识
52 |
53 | 我也乐于和你一起交谈操作系统原理
54 |
55 | QQ号: 2715626245
56 |
57 | 或者你可以给我发邮件, 地址: song_of_the_fly@163.com
58 |
59 | ---
60 |
61 | # 设置工作环境的方法
62 |
63 | 在 https://github.com/TayhuangOS-Development-Team/TayHuangOS_tools 中下载开发工具包
64 |
65 | 再在工具包目录下输入source ./setup.sh
66 |
67 | 之后把~/opt/cross/bin加入你的环境变量PATH中
68 |
69 | 再输入x86_64-elf-gcc --version
70 |
71 | 若出现
72 |
73 | ```
74 | 86_64-elf-gcc (GCC) 9.4.0
75 | Copyright (C) 2019 Free Software Foundation, Inc.
76 | This is free software; see the source for copying conditions. There is NO
77 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
78 | ```
79 |
80 | 则成功
81 |
82 | 在项目目录下输入make setup_workspace
83 |
84 | 配置好镜像的分区与启动分区(启动分区(即OS所在的分区)作为第一个分区)后输入make build_and_run
85 |
86 | 如果成功启动就可以了
87 |
88 | ## "我不会配置镜像分区与启动分区的"这么做
89 |
90 | 在出现`命令(输入 m 获取帮助):`后依次输入
91 |
92 | ```
93 | n
94 | p
95 | 1
96 | 2048
97 | 262143
98 | a
99 | w
100 | ```
101 |
102 | 即可
103 |
--------------------------------------------------------------------------------
/TODO:
--------------------------------------------------------------------------------
1 | └─ TayhuangOS
2 | ├─ kernel
3 | │ ├─ syscall
4 | │ │ └─ rpc.c
5 | │ │ └─ line 35: TODO : 使用红黑树
6 | │ └─ kernel.c
7 | │ └─ line 222: TODO : 更改loader以获取framebuffer的bpp
8 | ├─ libs
9 | │ ├─ libc
10 | │ │ ├─ ipc
11 | │ │ │ ├─ ipc.h
12 | │ │ │ │ └─ line 21: FIXME : 重构
13 | │ │ │ └─ ipc_func.S
14 | │ │ │ └─ line 19: FIXME : 重构*/
15 | │ │ └─ misc
16 | │ │ ├─ timer.c
17 | │ │ │ └─ line 24: TODO : 实现此函数
18 | │ │ └─ timer.h
19 | │ │ └─ line 22: TODO : 实现此函数
20 | │ └─ libkmod
21 | │ ├─ memory
22 | │ │ └─ malloc.c
23 | │ │ └─ line 83: TODO : extended the heap!
24 | │ ├─ syscall
25 | │ │ └─ rpc.c
26 | │ │ ├─ line 41: TODO : 使用红黑树
27 | │ │ └─ line 173: TODO : 加到队列中
28 | │ └─ printf.c
29 | │ └─ line 51: TODO : 重写
30 | ├─ module
31 | │ └─ framebuffers.c
32 | │ └─ line 23: TODO : 用红黑树
33 | ├─ Makefile
34 | │ └─ line 163: TODO :添加UEFI支持
35 | └─ TODO
36 | ├─ line 1: TODO : 更改loader以获取framebuffer的bpp
37 | ├─ line 2: TODO : 使用红黑树
38 | ├─ line 3: TODO : 实现此函数
39 | ├─ line 4: TODO : 实现此函数
40 | ├─ line 5: TODO : extended the heap!
41 | ├─ line 6: TODO : 重写
42 | ├─ line 7: TODO : 使用红黑树
43 | ├─ line 8: TODO : 加到队列中
44 | ├─ line 9: TODO :添加UEFI支持
45 | ├─ line 10: FIXME : 重构
46 | └─ line 11: FIXME : 重构*/
--------------------------------------------------------------------------------
/TayhuangOS.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TayhuangOS-Development-Team/TayHuangOS/29dcc247f2a97f1d033e6c5c2ca761e1e95c309c/TayhuangOS.png
--------------------------------------------------------------------------------
/TayhuangOS.svg:
--------------------------------------------------------------------------------
1 |
2 | Drawing
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | Layer 1
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/configs/grub.cfg:
--------------------------------------------------------------------------------
1 | set timeout=15
2 | set default=0 # Set the default menu entry
3 |
4 | menuentry "Tayhuang OS" {
5 | multiboot2 (hd0,msdos1)/grubld.bin # The multiboot2 command replaces the kernel command
6 | boot
7 | }
--------------------------------------------------------------------------------
/doc/OS抽象层.md:
--------------------------------------------------------------------------------
1 | # 抽象层
2 |
3 | *应用程序*--->*抽象设备*(*虚拟设备*、*实际设备*)--->*驱动程序*--->*IO控制器*--->*内核*->*实际设备*
4 |
5 | 对于*应用程序*而言 *驱动程序*、*实际设备*、*IO控制器*与*虚拟设备* 对其透明 其只可访问*抽象设备*
6 |
7 | 对于*驱动程序*而言 *实际设备* 对其透明 其只可访问 *IO控制器*
8 |
9 | 对于*IO控制器*而言 在大部分情况下 对于*实际设备*的操作 依靠*内核*
10 |
11 | *IO控制器*本质上是一类 **用于管理、调度对实际设备的IO请求** 的驱动程序
12 |
13 | *虚拟设备*是**实际上并不存在**的设备 大部分情况下 *虚拟设备*被设计为**对某一类设备/资源的管理器** 其功能由*虚拟设备驱动*(VDD: Virtual Device Driver)实现
14 |
15 | 例如: *tty*与*vfs*即为*虚拟设备* 但对于*应用程序*而言 它们是真实的设备
--------------------------------------------------------------------------------
/doc/OS版本规则.md:
--------------------------------------------------------------------------------
1 | # OS版本含义
2 |
3 | OS版本命名法如下:
4 |
5 | **代号**-**主版本号**.**副版本号**.**修订版本号**:build **构建次数**
6 |
7 | **代号**的更改不影响 **主版本号 **副版本号 **修订版本号**
8 |
9 | **主版本号** **副版本号** **修订版本号** 的更改不影响 **代号** **构建次数**
10 |
11 | ### 代号含义
12 |
13 | * alpha: 内测版
14 | * indev: 小范围公测版
15 | * beta: 公测版
16 | * dev: 开发版
17 | * dev-snapshot: 快照
18 | * dev-rc: 发行候选版
19 | * release: 正式版
20 | * release-lts: 长期支持版
21 |
22 | **代号**更改时 **构建次数**归0
23 |
24 | ### 主版本号 副版本号 修订版本号 规则
25 |
26 | 1. **主版本号**更改时 **副版本号** **修订版本号**归0
27 | 2. **副版本号**更改时 **修订版本号**归0
28 | 3. 当代号为**alpha** **indev**时
29 | * 当出现**向下不兼容**的**重构式更新**时 **主版本号**+1
30 | * 当出现**向下不兼容**的**非重构式更新**或**向下兼容**的**功能性更新**时 **副版本号**+1
31 | * 当出现**向下兼容**的**补丁更新**时 **修订版本号**+1
32 | 4. 当代号为**beta** **dev** **release**时
33 | * 当出现**向下不兼容**的**更新**时 **主版本号**+1
34 | * 当出现**向下兼容**的**功能性更新**时 **副版本号**+1
35 | * 当出现**向下兼容**的**补丁更新**时 **修订版本号**+1
36 |
37 | ### 构建次数
38 |
39 | * 等于该**代号**所对应的OS总共构建的次数
--------------------------------------------------------------------------------
/doc/代码规范.md:
--------------------------------------------------------------------------------
1 | # 代码规范
2 |
3 | 1. 函数/变量 使用小写字母+下划线写法
4 | 2. 常量/宏 使用大写字母+下划线写法
5 | 3. 记得写注释
6 | 4. 不管怎样 if和for后一定要加大括号 偶尔一两次忘记没事 补回来就好
--------------------------------------------------------------------------------
/doc/内核加载标准.md:
--------------------------------------------------------------------------------
1 | # 内核加载标准
2 |
3 | 进入*内核*时 *内核*应已处于**64位模式**
4 |
5 | *内核*的每个段应被加载到**物理地址**为 p_paddr 处
6 |
7 | *线性地址*与*物理地址*应**一一对应**
8 |
9 | *临时页表*与*gdt*应存放在**32MB以上**区域
10 |
11 | 同样 *Loader*地址也应在**32MB以上**区域
12 |
13 | 应将内核模块文件*setup.mod***一同加载**进内核并告知内核其地址
14 |
15 | 在宏*VBE_ENABLE*被定义的情况下 应进入**图形模式**
16 |
17 | #### gdt要求
18 |
19 | - 0 NULL
20 | - 1-6 空闲 可以随意使用
21 | - 在内核中
22 | - 1 TSS
23 | - 2 TSS
24 | - 3 ring3 long mode cs
25 | - 4 ring3 long mode ds
26 | - 5 ring1 long mode cs
27 | - 6 ring1 long mode ds
28 | - 7 ring0 long mode cs
29 | - 8 ring0 long mode ds
30 |
31 | 进入内核时 应在pic中屏蔽**所有中断**并将**IF设0**
32 |
33 | ### 内核参数
34 |
35 | *rax*应为**参数地址**
36 |
37 | 参数的结构体位于*include/tayhuang/boot_args.h*
38 | 如下所示
39 | ```c
40 | struct boot_args {
41 | unsigned int magic; //魔数 用于校验
42 | unsigned char is_graphic_mode; //是否为图形模式
43 | int screen_width; //屏幕宽
44 | int screen_height; //屏幕高
45 | unsigned long long framebuffer; //屏幕显存
46 | unsigned long long memory_size; //内存大小
47 | unsigned long long kernel_start;
48 | unsigned long long kernel_limit;
49 | unsigned long long page_start;
50 | unsigned long long page_limit;
51 | unsigned long long setup_mod_addr;
52 | };
53 |
54 |
55 | #define BOOT_ARGS_MAGIC (0x5A71F213) //MD5 of "BOOTARGS"(前32位)
56 | ```
--------------------------------------------------------------------------------
/doc/内核模块标准.md:
--------------------------------------------------------------------------------
1 | # 内核模块标准
2 |
3 | 进入内核模块后 模块的寄存器值如下表所示
4 |
5 | | 寄存器 | 值 |
6 | | ------ | -------------------------------------- |
7 | | rax | 魔数0x71BA4851(KERNELMOD的MD5的后32位) |
8 | | rbx | KMod堆底地址 |
9 | | rcx | KMod堆顶地址 |
10 | | rdx | KMod PID |
11 | | rsp | KMod栈顶地址 |
12 | | rbp | KMod栈底地址 |
--------------------------------------------------------------------------------
/doc/内核细节.md:
--------------------------------------------------------------------------------
1 | # 内核细节
2 |
3 | * *TayhuangOS*的**kernel**为**微内核**
4 | * 进程间采用**同步IPC**与**异步IPC**并存
5 | * 在大部分情况下 调用*目标进程*使用**同步IPC** 返回值使用**异步IPC**
6 | * 在大部分情况下 调用*目标进程*会让*调用进程*将自身**剩余的时间片**捐献给被*调用进程* 但被调用进程**不是立即运行**
7 | * 在ipc时 长度若为0则给予的地址为*立即值*
8 | * ipc*立即值*大小 <= 8Byte
9 | * allow_pid**特殊值**如下
10 | * 0 NULL 无进程 不允许IPC
11 | * -1 ANY 允许任何进程发送
12 | * -2 DUMMY 此时 允许内核发送 通常与check_ipc配合使用
13 | * PID**从1开始**分配
14 | * 系统的**第一个进程**为*init*
15 | * 系统使用**双级调度队列**
16 | * 0级使用**FCFS**算法
17 | * *FCFS(First Come First Serve 先来先服务)* 先到的进程**一直运行** 直到其**无法继续运行**为止
18 | * 1级使用**TRR**算法
19 | * *TRR(Time Round Robin 时间片轮转)* 略
20 | * *时间片*长度为 20ms
21 | * 一般情况下 进程持有*priority* \* 3个*时间片*
22 | * 一般情况下 进程的*priority*在\[1, 5\]之间
23 | * 当0级有*进程*可以调度时 **立即**调度*0级进程*
--------------------------------------------------------------------------------
/include/std/assert.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong 383494
10 | *
11 | * assert.h
12 | *
13 | * assert与panic
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | void assertion_failure(const char *expression, const char *file, const char *base_file, int line);
22 | void panic_failure(const char *expression, const char *file, const char *base_file, int line);
23 | void panic(const char *format, ...);
24 |
25 | #ifdef NDEBUG
26 | #define assert(expression) ((void)0)
27 | #define panic_assert(expression) ((void)0)
28 | #else
29 | #define assert(expression) if (! (expression)) \
30 | assertion_failure(#expression, __FILE__, __BASE_FILE__, __LINE__)
31 | #define panic_assert(expression) if (! (expression)) \
32 | panic_failure(#expression, __FILE__, __BASE_FILE__, __LINE__)
33 | #endif
34 |
--------------------------------------------------------------------------------
/include/std/ctype.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2021, 2021 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * ctype.h
12 | *
13 | * 标准库 ctype.h 头文件
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | /*
22 | * ctype函数的宏实现
23 | * 因为是利用宏, 所以可能会引发问题 如__isaplha(getchar())
24 | * 不推荐直接使用
25 | * 仅用于辅助函数实现
26 | */
27 | #define __isspace(ch) (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\v' || ch == '\f')
28 | #define __isupper(ch) (ch >= 'A' && ch <= 'Z')
29 | #define __islower(ch) (ch >= 'a' && ch <= 'z')
30 | #define __isalpha(ch) (__isupper(ch) || __islower(ch))
31 | #define __isdigit(ch) (ch >= '0' && ch <= '9')
32 | #define __isalnum(ch) (__isalpha(ch) || __isdigit(ch))
33 | #define __isblank(ch) (ch == ' ' || ch == '\t')
34 | #define __iscntrl(ch) ((ch >= 0x00 && ch <= 0x1f) || ch == 0x7f)
35 | #define __isprint(ch) (!__iscntrl(ch))
36 | #define __isgraph(ch) (__isprint(ch))
37 | #define __ispunct(ch) (__isprint(ch) && (! __isalnum(ch)))
38 | #define __isxdigit(ch) (__isalnum(ch) || ((ch >= 'a' && ch <= 'F') || (ch >= 'A' && ch <= 'F')))
39 | #define __isodigit(ch) (ch >= '0' && ch <= '7')
40 | #define __tolower(ch) (__isupper(ch) ? (ch - 'A' + 'a') : (ch))
41 | #define __toupper(ch) (__islower(ch) ? (ch - 'a' + 'A') : (ch))
42 |
43 | /* ctype函数 */
44 | int isspace(int ch);
45 | int isupper(int ch);
46 | int islower(int ch);
47 | int isalpha(int ch);
48 | int isdigit(int ch);
49 | int isalnum(int ch);
50 | int isblank(int ch);
51 | int iscntrl(int ch);
52 | int isprint(int ch);
53 | int isgraph(int ch);
54 | int ispunct(int ch);
55 | int isxdigit(int ch);
56 | int isodigit(int ch);
57 | int tolower(int ch);
58 | int toupper(int ch);
--------------------------------------------------------------------------------
/include/std/math.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * math.h
12 | *
13 | * 标准库 math.h 头文件
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #define HUGE_VAL (__builtin_huge_val())
22 |
23 | #define CONSTANT_PI (3.14159265358979323846) //圆周率
24 | #define CONSTANT_E (2.71828182845904523536) //e
25 | #define CONSTANT_PHI (0.61803398874989484820) //黄金分割比例
26 | #define CONSTANT_DELTA (4.66920160910299067185) //费根鲍姆常数
27 |
28 | double sin(double x);
29 | double cos(double x);
30 | double atanh(double x);
31 | double ln(double x);
32 | double exp(double x);
33 | double pow(double x, double y);
34 | double sqrt(double x);
35 | double fabs(double x);
--------------------------------------------------------------------------------
/include/std/stdarg.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2021, 2021 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * stdarg.h
12 | *
13 | * 标准库 stdarg.h 头文件
14 | * 参考自linux
15 | *
16 | */
17 |
18 |
19 |
20 | #pragma once
21 |
22 |
23 | #ifdef ARCH_x86_64
24 |
25 | #ifndef __GNUC_VA_LIST
26 | #define __GNUC_VA_LIST
27 | typedef __builtin_va_list __gnuc_va_list;
28 | #endif
29 |
30 | #ifndef __va_list__
31 | typedef __gnuc_va_list va_list;
32 | #endif
33 |
34 | #define va_start(v,l) __builtin_va_start(v,l)
35 | #define va_end(v) __builtin_va_end(v)
36 | #define va_arg(v,l) __builtin_va_arg(v,l)
37 | #define va_copy(d,s) __builtin_va_copy(d,s)
38 |
39 | #else
40 |
41 | //int的size
42 | #define INTSIZE (sizeof(int))
43 |
44 | //定义类型
45 | typedef char *va_list;
46 |
47 | //将size转为INTSIZE的整数倍
48 | #define INTSIZEOF(ty) ((sizeof(ty) + INTSIZE - 1) & (~(INTSIZE - 1)))
49 |
50 | //初始化可变参数列表
51 | #define va_start(lst, start) (lst = (((va_list)&start) + INTSIZEOF(start)))
52 | //获取列表参数
53 | #define va_arg(lst, ty) (*(ty *)((lst += INTSIZEOF(ty)) - INTSIZEOF(ty)))
54 | //结束获取
55 | #define va_end(lst) (lst = ((va_list)0))
56 |
57 | #endif
--------------------------------------------------------------------------------
/include/std/stdbool.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: 383494
10 | *
11 | * stdbool.h
12 | *
13 | * 标准库 stdbool.h 头文件
14 | *
15 | */
16 |
17 | #pragma once
18 |
19 | #ifndef _STDBOOL_H
20 | #define _STDBOOL_H
21 |
22 | #define __bool_true_false_are_defined 1
23 |
24 | #ifndef __cplusplus
25 | #define bool _Bool
26 |
27 | #if (defined __STDC_VERSION__ && __STDC_VERSION__ > 201710L)
28 | // for C23
29 | #define true ((_Bool) + 1u)
30 | #define false ((_Bool) + 0u)
31 | #else
32 | #define true 1
33 | #define false 0
34 | #endif
35 |
36 | #else
37 | #define _Bool bool
38 | #endif
39 |
40 | #endif
41 |
--------------------------------------------------------------------------------
/include/std/stddef.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong 383494
10 | *
11 | * stddef.h
12 | *
13 | * 标准库 stddef.h 头文件
14 | *
15 | */
16 |
17 |
18 | #pragma once
19 |
20 | #define NULL ((void *)0)
21 |
22 | #ifndef __SIZE_TYPE__
23 | #define __SIZE_TYPE__ unsigned int
24 | #endif
25 |
26 | typedef __SIZE_TYPE__ size_t;
27 |
28 | #ifndef __PTRDIFF_TYPE__
29 |
30 | #ifndef LOADER32BIT
31 | #define __PTRDIFF_TYPE__ signed long long
32 | #else
33 | #define __PTRDIFF_TYPE__ signed int
34 | #endif
35 |
36 | #endif
37 |
38 | typedef __PTRDIFF_TYPE__ ptrdiff_t;
39 |
40 | #ifndef offsetof
41 | #define offsetof(type, member) ((size_t)&(((type *)0)->member))
42 | #endif
43 |
44 | typedef int wchar_t;
45 |
--------------------------------------------------------------------------------
/include/std/stdint.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2021, 2021 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * stdint.h
12 | *
13 | * 标准库stdint.h的头文件
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #ifdef __INT8_TYPE__
22 | typedef __INT8_TYPE__ int8_t;
23 | #endif
24 | #ifdef __INT16_TYPE__
25 | typedef __INT16_TYPE__ int16_t;
26 | #endif
27 | #ifdef __INT32_TYPE__
28 | typedef __INT32_TYPE__ int32_t;
29 | #endif
30 | #ifdef __INT64_TYPE__
31 | typedef __INT64_TYPE__ int64_t;
32 | #endif
33 | #ifdef __UINT8_TYPE__
34 | typedef __UINT8_TYPE__ uint8_t;
35 | #endif
36 | #ifdef __UINT16_TYPE__
37 | typedef __UINT16_TYPE__ uint16_t;
38 | #endif
39 | #ifdef __UINT32_TYPE__
40 | typedef __UINT32_TYPE__ uint32_t;
41 | #endif
42 | #ifdef __UINT64_TYPE__
43 | typedef __UINT64_TYPE__ uint64_t;
44 | #endif
--------------------------------------------------------------------------------
/include/tayhuang/boot_args.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * boot_args.h
12 | *
13 | * boot_args的结构
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | struct boot_args {
22 | unsigned int magic; //魔数 用于校验
23 | unsigned char is_graphic_mode; //是否为图形模式
24 | int screen_width; //屏幕宽
25 | int screen_height; //屏幕高
26 | unsigned long long framebuffer; //屏幕显存
27 | unsigned long long memory_size; //内存大小
28 | unsigned long long kernel_start;
29 | unsigned long long kernel_limit;
30 | unsigned long long page_start;
31 | unsigned long long page_limit;
32 | unsigned long long setup_mod_addr;
33 | };
34 |
35 | #define BOOT_ARGS_MAGIC (0x5A71F213) //MD5 of "BOOTARGS"(后32位)
--------------------------------------------------------------------------------
/include/tayhuang/defs.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * defs.h
12 | *
13 | * 基础的定义
14 | *
15 | */
16 |
17 | #pragma once
18 |
19 | #include
20 |
21 | #define PUBLIC //公共
22 | #define PRIVATE static //私有
23 | #define EXTERN extern //extern
24 | #define VOLATILE volatile //volatile
25 | #define HIGHBYTE(x) (((x) >> 8) & 0xFF) //高字节
26 | #define LOWBYTE(x) (x & 0xFF) //低字节
27 | #define HIGHHEX(x) (((x) >> 4) & 0xF) //高16进制位
28 | #define LOWHEX(x) ((x)&0xF) //低16进制位
29 | #define HIGHWORD(x) ((word)((x) >> 16) & 0xFFFF) //高字
30 | #define LOWWORD(x) ((word)(x)&0xFFFF) //低字
31 | #define MKBYTE(h, l) ((h) << 4 + (l)) //字节
32 | #define MKWORD(h, l) ((((word)h) << 8) + (l)) //字
33 | #define MKDWORD(h, l) ((((dword)h) << 16) + (l)) //双字
34 | #define max(a, b) (((a) > (b)) ? (a) : (b)) //取大
35 | #define min(a, b) (((a) < (b)) ? (a) : (b)) //取小
36 | #define LOWBIT(x) ((x) & (-(x))) //取最低bit
37 | #define TO2POW(num, x) (((num) + (x)-1) & (~((x)-1))) //向上取为2的n次幂
38 | #define abs(x) ((x) < 0 ? (-(x)) : (x)) //绝对值
39 | #define en_int() asmv("sti")
40 | #define dis_int() asmv("cli")
41 |
42 | //32位数x的前导0个数
43 | static inline dword leading_zeros(dword x) {
44 | if (x == 0) {
45 | return 32;
46 | }
47 | int n = 1;
48 | if (x >> 16 == 0) {
49 | n += 16;
50 | x <<= 16;
51 | }
52 |
53 | if (x >> 24 == 0) {
54 | n += 8;
55 | x <<= 8;
56 | }
57 |
58 | if (x >> 28 == 0) {
59 | n += 4;
60 | x <<= 4;
61 | }
62 |
63 | if (x >> 30 == 0) {
64 | n += 2;
65 | x <<= 2;
66 | }
67 |
68 | n -= x >> 31;
69 | return n;
70 | }
71 |
72 | //取log2 x近似值
73 | static inline int simple_log2(qword x) {
74 | int l = 0, r = 64;
75 | int mid = (l + r) >> 1;
76 |
77 | while (l <= r) {
78 | if ((1 << mid) > x) {
79 | r = mid;
80 | }
81 | else if ((1 << mid) < x) {
82 | l = mid;
83 | }
84 | else {
85 | break;
86 | }
87 | mid = (l + r) >> 1;
88 | }
89 |
90 | return mid;
91 | }
92 |
--------------------------------------------------------------------------------
/include/tayhuang/int_vectors.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * ports.h
12 | *
13 | * 基础中断向量
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #define CALC_IRQ(x) (0x20 + (x)) //计算IRQ的中断向量号
22 | //IRQ 0~10
23 | #define INT_VECTOR_IRQ0 CALC_IRQ(0)
24 | #define INT_VECTOR_IRQ1 CALC_IRQ(1)
25 | #define INT_VECTOR_IRQ2 CALC_IRQ(2)
26 | #define INT_VECTOR_IRQ3 CALC_IRQ(3)
27 | #define INT_VECTOR_IRQ4 CALC_IRQ(4)
28 | #define INT_VECTOR_IRQ5 CALC_IRQ(5)
29 | #define INT_VECTOR_IRQ6 CALC_IRQ(6)
30 | #define INT_VECTOR_IRQ7 CALC_IRQ(7)
31 | #define INT_VECTOR_IRQ8 CALC_IRQ(8)
32 | #define INT_VECTOR_IRQ9 CALC_IRQ(9)
33 | #define INT_VECTOR_IRQ10 CALC_IRQ(10)
--------------------------------------------------------------------------------
/include/tayhuang/io.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * io.h
12 | *
13 | * In&Out
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | static inline byte inb(word port) { //in字节
24 | byte data;
25 | asmv ("inb %1, %0" : "=a"(data) : "dN"(port));
26 | return data;
27 | }
28 |
29 | static inline void outb(word port, byte data) { //out字节
30 | asmv ("outb %0, %1" : : "a"(data), "dN"(port));
31 | }
32 |
33 | static inline word inw(word port) { //in字
34 | word data;
35 | asmv ("inw %1, %0" : "=a"(data) : "dN"(port));
36 | return data;
37 | }
38 |
39 | static inline void outw(word port, word data) { //out字
40 | asmv ("outw %0, %1" : : "a"(data), "dN"(port));
41 | }
42 |
43 | static inline dword ind(word port) { //in双字
44 | dword data;
45 | asmv ("inl %1, %0" : "=a"(data) : "dN"(port));
46 | return data;
47 | }
48 |
49 | static inline void outd(word port, dword data) { //out双字
50 | asmv ("outl %0, %1" : : "a"(data), "dN"(port));
51 | }
52 |
53 | static inline sreg_t rdcs(void) { //读取cs值
54 | sreg_t reg;
55 | asmv ("movw %%cs, %0" : "=rm"(reg));
56 | return reg;
57 | }
58 |
59 | static inline b16 rdds(void) { //读取ds值
60 | sreg_t reg;
61 | asmv("movw %%ds, %0" : "=rm"(reg));
62 | return reg;
63 | }
64 |
65 | static inline void stds(sreg_t reg) {
66 | asmv("movw %0, %%ds" : : "rm"(reg));
67 | }
68 |
69 | static inline sreg_t rdes(void) { //读取es值
70 | sreg_t reg;
71 | asmv("movw %%es, %0" : "=rm"(reg));
72 | return reg;
73 | }
74 |
75 | static inline void stes(sreg_t reg) {
76 | asmv("movw %0, %%es" : : "rm"(reg));
77 | }
78 |
79 | static inline sreg_t rdfs(void) { //读取fs值
80 | sreg_t reg;
81 | asmv("movw %%fs, %0" : "=rm"(reg));
82 | return reg;
83 | }
84 |
85 | static inline void stfs(sreg_t reg) {
86 | asmv("movw %0, %%fs" : : "rm"(reg));
87 | }
88 |
89 | static inline sreg_t rdgs(void) { //读取gs值
90 | sreg_t reg;
91 | asmv("movw %%gs, %0" : "=rm"(reg));
92 | return reg;
93 | }
94 |
95 | static inline void stgs(sreg_t reg) {
96 | asmv("movw %0, %%gs" : : "rm"(reg));
97 | }
98 |
99 | static inline sreg_t rdss(void) { //读取ss值
100 | sreg_t reg;
101 | asmv("movw %%ss, %0" : "=rm"(reg));
102 | return reg;
103 | }
104 |
105 | static inline void stss(sreg_t reg) {
106 | asmv("movw %0, %%ss" : : "rm"(reg));
107 | }
108 |
--------------------------------------------------------------------------------
/include/tayhuang/kmod_prototypes.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * kmod_prototypes.h
12 | *
13 | * 内核模块原型
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | PUBLIC void kmod_init(void);
24 | PUBLIC void kmod_main(void);
25 |
26 | #define KMOD_MAGIC (0x71BA4851)
--------------------------------------------------------------------------------
/include/tayhuang/msgpack.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * msgpack.h
12 | *
13 | * 消息包结构
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | struct __msgpack_struct {
24 | byte reserved : 8;
25 | byte message_no : 8;
26 |
27 | word msg_id : 16;
28 | word length : 16;
29 | word source : 16;
30 | } __attribute__((packed));
31 |
32 | struct __msgno_id {
33 | byte reserved : 8;
34 | byte message_no : 8;
35 | word msg_id : 16;
36 |
37 | } __attribute__((packed));
38 |
39 | typedef struct __msgno_id msgno_id;
40 | typedef struct __msgpack_struct msgpack_struct;
41 |
42 | #define MSG_NORMAL_IPC (0x00)
43 | #define MSG_RPC_CALL (0x01)
44 | #define MSG_RPC_RESULT (0x02)
45 | #define MSG_IRQ (0x03)
46 | #define MSG_WAKEUP (0x04)
--------------------------------------------------------------------------------
/include/tayhuang/paging.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * paging.h
12 | *
13 | * 分页相关结构/宏
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | #define MEMUNIT_SZ (4096) //内存单元大小
24 |
25 | #define CALC_M(x) min(54, max((sizeof(dword) * 8 - leading_zeros(x - 1)), 32)) //根据内存大小计算M的值
26 |
27 | //页项结构
28 | typedef struct {
29 | bool p : 1;
30 | bool rw : 1;
31 | bool us : 1;
32 | bool pwt : 1;
33 | bool pcd : 1;
34 | bool a : 1;
35 | byte avl3 : 1;
36 | byte reserved2 : 1;
37 | byte avl2 : 4;
38 | qword address : 39;
39 | byte reserved : 1;
40 | word avl : 11;
41 | bool xd : 1;
42 | } PML4E;
43 |
44 | typedef struct {
45 | bool p : 1;
46 | bool rw : 1;
47 | bool us : 1;
48 | bool pwt : 1;
49 | bool pcd : 1;
50 | bool a : 1;
51 | byte avl3 : 1;
52 | byte reserved2 : 1;
53 | byte avl2 : 4;
54 | qword address : 39;
55 | byte reserved : 1;
56 | word avl : 11;
57 | bool xd : 1;
58 | } PDPTE;
59 |
60 | typedef struct {
61 | bool p : 1;
62 | bool rw : 1;
63 | bool us : 1;
64 | bool pwt : 1;
65 | bool pcd : 1;
66 | bool a : 1;
67 | byte avl3 : 1;
68 | byte reserved2 : 1;
69 | byte avl2 : 4;
70 | qword address : 39;
71 | byte reserved : 1;
72 | word avl : 11;
73 | bool xd : 1;
74 | } PDE;
75 |
76 | typedef struct {
77 | bool p : 1;
78 | bool rw : 1;
79 | bool us : 1;
80 | bool pwt : 1;
81 | bool pcd : 1;
82 | bool a : 1;
83 | byte d : 1;
84 | byte pat : 1;
85 | byte g : 1;
86 | byte avl2 : 3;
87 | qword address : 39;
88 | byte reserved : 1;
89 | word avl : 11;
90 | bool xd : 1;
91 | } PTE;
--------------------------------------------------------------------------------
/include/tayhuang/partition.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * partition.h
12 | *
13 | * 分区相关
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | #define PARTITION_TABLE_OFFSET (0x01BE)
24 | #define PARTITION_NUMBER (4)
25 |
26 | //分区状态
27 | enum {
28 | PS_BOOTABLE = 0x80,
29 | PS_UNBOOTABLE = 0x00
30 | };
31 |
32 | //分区成员
33 | struct __partition_info {
34 | byte state;
35 | byte start_head;
36 | byte start_sector;
37 | byte start_cylinder;
38 | byte system_id;
39 | byte end_head;
40 | byte end_cylinder;
41 | byte end_sector;
42 | dword start_lba;
43 | dword sector_number;
44 | } __attribute__((packed));
45 |
46 | typedef struct __partition_info partition_info;
--------------------------------------------------------------------------------
/include/tayhuang/services.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * services.h
12 | *
13 | * 内核模块服务号
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #define API_PID(x) (0x8000 + (x))
22 |
23 | //init()
24 | #define INIT_SERVICE (1)
25 | //setup.mod 辅助内核初始化
26 | #define SETUP_SERVICE API_PID(0)
27 | //void sys_task() 系统调用模块
28 | #define SYSTASK_SERVICE API_PID(1)
29 | //保留
30 | #define RESERVED0_SERVICE API_PID(2)
31 | //video.mod 视频驱动
32 | #define VIDEO_DRIVER_SERVICE API_PID(3)
33 | //tty.mod tty虚拟设备驱动
34 | #define TTY_DRIVER_SERVICE API_PID(4)
35 | //keyboard.mod 键盘驱动
36 | #define KEYBOARD_DRIVER_SERVICE API_PID(5)
37 | #define KEYBOARD_IOMAN_SERVICE API_PID(6)
38 | //disk.mod 硬盘驱动
39 | #define DISK_DRIVER_SERVICE API_PID(7)
40 | #define DISK_IOMAN_SERVICE API_PID(8)
41 | //vfs.mod 文件系统虚拟设备驱动
42 | #define VFS_DRIVER_SERVICE API_PID(9)
43 |
44 | //空进程
45 | #define NULL_TASK (0)
46 | //任意进程
47 | #define ANY_TASK (-1)
48 | //假进程
49 | #define DUMMY_TASK (-2)
--------------------------------------------------------------------------------
/include/tayhuang/types.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * types.h
12 | *
13 | * 基础的类型
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | typedef unsigned char byte; //字节
25 | typedef unsigned short word; //字
26 | typedef unsigned int dword; //双字
27 | typedef unsigned long long qword; //四字
28 | typedef byte b8; //8位
29 | typedef word b16; //16位
30 | typedef dword b32; //32位
31 | typedef qword b64; //64位
32 | typedef b16 sreg_t; //段寄存器
33 | typedef b8 reg8_t; //8位寄存器
34 | typedef b16 reg16_t; //16位寄存器
35 | typedef b32 reg32_t; //32位寄存器
36 | typedef b64 reg64_t; //64位寄存器
37 | typedef void *handle_t; //处理器
38 |
39 | #define asmv asm volatile //简写
40 | typedef void *addr_t; //地址
41 | typedef dword id_t; //编号
42 | typedef int service_t; //服务号
43 | typedef qword offset_t; //偏移
44 |
--------------------------------------------------------------------------------
/include/tayhuang/video_info.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * video_info_struct.h
12 | *
13 | * 视频信息
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | typedef struct {
24 | void *framebuffer;
25 | int width;
26 | int height;
27 | bool is_graphic_mode;
28 | } video_info_struct;
--------------------------------------------------------------------------------
/include/tool/tostring.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * tostring.h
12 | *
13 | * xx转字符串
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | char *itoa(int val, char *buffer, int base);
22 | char *uitoa(unsigned int val, char *buffer, int base);
23 | char *lltoa(long long val, char *buffer, int base);
24 | char *ulltoa(unsigned long long val, char *buffer, int base);
25 | char *ftoa(float val, char *buffer, int round);
26 | char *dtoa(double val, char *buffer, int round);
27 | char *ftoea(float val, char *buffer, unsigned char upper_e);
28 | char *dtoea(double val, char *buffer, unsigned char upper_e);
--------------------------------------------------------------------------------
/kernel/README.md:
--------------------------------------------------------------------------------
1 | # Kernel
2 |
3 | Kernel source here
--------------------------------------------------------------------------------
/kernel/entry.S:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * entry.S
12 | *
13 | * 内核入口
14 | *
15 | */
16 |
17 |
18 |
19 | .code64
20 | .text
21 | .extern entry
22 |
23 | .global _start
24 | _start: /*入口*/
25 | jmp entry
26 |
27 | .global __set_cr0
28 | .type __set_cr0, @function
29 | __set_cr0: /*设置cr0*/
30 | movq %rdi, %cr0
31 | ret
32 |
33 | .global __get_cr0
34 | .type __get_cr0, @function
35 | __get_cr0: /*获取cr0*/
36 | movq %cr0, %rax
37 | ret
38 |
39 | .global __set_cr2
40 | .type __set_cr2, @function
41 | __set_cr2: /*设置cr2*/
42 | movq %rdi, %cr2
43 | ret
44 |
45 | .global __get_cr2
46 | .type __get_cr2, @function
47 | __get_cr2: /*获取cr2*/
48 | movq %cr2, %rax
49 | ret
50 |
51 | .global __set_cr3
52 | .type __set_cr3, @function
53 | __set_cr3: /*设置cr3*/
54 | movq %rdi, %cr3
55 | ret
56 |
57 | .global __set_cr32
58 | .type __set_cr32, @function
59 | __set_cr32: /*设置cr3*/
60 | ud2
61 | movq %rdi, %cr3
62 | ret
63 |
64 | .global __get_cr3
65 | .type __get_cr3, @function
66 | __get_cr3: /*获取cr3*/
67 | movq %cr3, %rax
68 | ret
69 |
70 | .global __set_cr4
71 | .type __set_cr4, @function
72 | __set_cr4: /*设置cr4*/
73 | movq %rdi, %cr4
74 | ret
75 |
76 | .global __get_cr4
77 | .type __get_cr4, @function
78 | __get_cr4: /*获取cr4*/
79 | movq %cr4, %rax
80 | ret
81 |
82 | .global __set_efer
83 | .type __set_efer, @function
84 | __get_efer: /*设置efer*/
85 | pushq %rdx
86 | pushq %rcx
87 | xorq %rdx, %rdx
88 | movq $0xC0000080, %rcx /*EFER*/
89 | rdmsr
90 | popq %rcx
91 | popq %rdx
92 | ret
93 |
94 | .global __get_efer
95 | .type __get_efer, @function
96 | __set_efer: /*获取efer*/
97 | pushq %rax
98 | pushq %rdx
99 | pushq %rcx
100 | xorq %rdx, %rdx
101 | movq %rdi, %rax
102 | movq $0xC0000080, %rcx /*EFER*/
103 | wrmsr
104 | popq %rcx
105 | popq %rdx
106 | popq %rax
107 | ret
108 |
--------------------------------------------------------------------------------
/kernel/export/sys_task/__sys_task_fn.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * __sys_task_fn.c
12 | *
13 | * sys_task 函数功能
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 |
23 | #include
24 |
25 | //共享内存
26 | PUBLIC void *share_memory(void *addr, int pages, int target) {
27 | char buffer[SHARE_MEMORY_ARGS_SIZE];
28 | void *buf = buffer;
29 |
30 | //写入参数
31 | ARG_WRITE(buf, void *, addr);
32 | ARG_WRITE(buf, int, pages);
33 | ARG_WRITE(buf, int, target);
34 |
35 | //调用
36 | void *new_addr = remote_call(SHARE_MEMORY_RETURN_TYPE, SYSTASK_SERVICE, SHARE_MEMORY_FN, MAKE_ARGS(buffer, SHARE_MEMORY_ARGS_SIZE));
37 |
38 | return new_addr;
39 | }
40 |
41 | //创建共享内存
42 | PUBLIC void *create_share_memory(int pages) {
43 | //调用
44 | void *addr = remote_call(CREATE_SHARE_MEMORY_RETURN_TYPE, SYSTASK_SERVICE, CREATE_SHARE_MEMORY_FN, MAKE_ARGS(&pages, CREATE_SHARE_MEMORY_ARGS_SIZE));
45 | return addr;
46 | }
--------------------------------------------------------------------------------
/kernel/export/sys_task/__sys_task_fn.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * __sys_task_fn.h
12 | *
13 | * sys_task 函数功能
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //共享内存
24 | #define SHARE_MEMORY_FN (0)
25 | #define SHARE_MEMORY_ARGS_SIZE (sizeof(void *) + sizeof(int) * 2)
26 | #define SHARE_MEMORY_RETURN_TYPE void *
27 |
28 | //创建共享内存
29 | #define CREATE_SHARE_MEMORY_FN (1)
30 | #define CREATE_SHARE_MEMORY_ARGS_SIZE (sizeof(int))
31 | #define CREATE_SHARE_MEMORY_RETURN_TYPE void *
32 |
33 | //共享内存
34 | PUBLIC void *share_memory(void *addr, int pages, int target);
35 | //创建共享内存
36 | PUBLIC void *create_share_memory(int pages);
--------------------------------------------------------------------------------
/kernel/global.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * global.h
12 | *
13 | * 全局的信息/变量
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //杂项
24 | EXTERN PUBLIC void *kernel_pml4;
25 | EXTERN PUBLIC struct boot_args args;
26 | EXTERN PUBLIC int cur_pid;
27 | EXTERN PUBLIC int cur_tid;
28 | EXTERN PUBLIC bool entered_handler;
29 | EXTERN PUBLIC word msgid_counter;
30 |
31 | static inline word get_msgid(void) {
32 | if (msgid_counter == 65535) {
33 | msgid_counter = 0;
34 | return 65535;
35 | }
36 | return msgid_counter ++;
37 | }
38 |
39 | //GDT表项编号
40 | #define tr_idx (1)
41 | #define cs3_idx (3)
42 | #define ds3_idx (4)
43 | #define cs1_idx (5)
44 | #define ds1_idx (6)
45 | #define cs0_idx (7)
46 | #define ds0_idx (8)
47 |
48 | //不同特权级的描述符选择子/标志位
49 | #define CS_USER (cs3_idx << 3 | 3)
50 | #define DS_USER (ds3_idx << 3 | 3)
51 | #define RFLAGS_USER ((1 << 9) | (3 << 12))
52 | #define CS_SERVICE (cs1_idx << 3 | 1)
53 | #define DS_SERVICE (ds1_idx << 3 | 1)
54 | #define RFLAGS_SERVICE ((1 << 9) | (1 << 12))
55 | #define CS_KERNEL (cs0_idx << 3)
56 | #define DS_KERNEL (ds0_idx << 3)
57 | #define RFLAGS_KERNEL (1 << 9)
58 |
59 | //堆栈分配
60 | #define RING0_STACKTOP (0x1400000)
61 | #define RING0_T1STACKBOTTOM (0x13C0000)
62 | #define RING0_T2STACKBOTTOM (0x1380000)
63 | #define RING0_STACKBOTTOM (0x1300000)
64 |
65 | #define RING0_STACKTOP2 (0x1300000)
66 | #define RING0_STACKBOTTOM2 (0x1200000)
67 |
68 | #define RING0_STACKTOP3 (0x1200000)
69 | #define RING0_STACKBOTTOM3 (0x1100000)
70 |
71 | #define IST0_STACKTOP (0x1100000)
72 | #define IST0_STACKBOTTOM (0x1000000)
73 |
74 | //内核模块堆栈顶(1GB)
75 | #define KMOD_STACK_TOP (0x40000000)
76 |
77 | //默认共享内存地址(4GB~8GB)
78 | #define DEFAULT_SHM_START (0x100000000)
79 | #define DEFAULT_SHM_END (0x200000000)
80 |
81 | #define KERNEL_TASK_STACK_SIZE (0x8000)
82 |
83 | //映射内核
84 | PUBLIC void mapping_kernel(void *pgd);
--------------------------------------------------------------------------------
/kernel/intterup/clock.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * clock.c
12 | *
13 | * 时钟中断
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 | #include
21 |
22 | #include
23 |
24 | #include
25 |
26 | #include
27 |
28 | #include
29 |
30 | #define PIT_FREQUENCY (1193181.6666f)
31 |
32 | PUBLIC VOLATILE int ticks = 0;
33 |
34 | //初始化PIT
35 | PUBLIC bool init_pit(float frequency) {
36 | //频率过高
37 | if (frequency > PIT_FREQUENCY) {
38 | return false;
39 | }
40 |
41 | int count = (int)(PIT_FREQUENCY / frequency);
42 | if ((PIT_FREQUENCY - count * frequency) > (frequency / 2))
43 | count ++;
44 |
45 | //频率过低
46 | if (count >= 65535) {
47 | return false;
48 | }
49 |
50 | //设置频率
51 | outb(PIT_CHANNEL0, (byte)count);
52 | outb(PIT_CHANNEL0, (byte)(count >> 8));
53 |
54 | return true;
55 | }
56 |
57 | EXTERN PUBLIC bool do_schedule;
58 |
59 | //时钟中断
60 | PUBLIC void clock_int_handler(int irq, struct intterup_args *regs, bool entered_handler) {
61 | //增加滴答计数
62 | ticks ++;
63 |
64 | if (do_schedule) {
65 | //在系统调用(中断)结束时调用
66 | after_syscall(regs);
67 |
68 | //是level1则减少时间片计数
69 | if (current_thread->task->level == 1) {
70 | current_thread->count --;
71 | }
72 | }
73 | }
--------------------------------------------------------------------------------
/kernel/intterup/clock.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * clock.h
12 | *
13 | * 时钟中断
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //ticks
24 | EXTERN PUBLIC VOLATILE int ticks;
25 | //初始化pit
26 | PUBLIC bool init_pit(float frequency);
27 | PUBLIC void clock_int_handler(int irq, struct intterup_args *regs, bool entered_handler);
--------------------------------------------------------------------------------
/kernel/intterup/exception.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * exception.h
12 | *
13 | * 异常处理
14 | *
15 | */
16 |
17 |
18 |
19 |
20 | #pragma once
21 |
22 | #include
23 |
24 | #include
25 |
26 | //通用异常处理器
27 | PUBLIC void general_exception_handler(int vector, struct intterup_args *regs);
28 |
29 | //各类异常
30 | PUBLIC void divide_by_zero_error(void); //除以0
31 | PUBLIC void single_step_debug(void); //单步调试
32 | PUBLIC void non_maskable_interrup(void); //NMI
33 | PUBLIC void breakpoint(void); //断点
34 | PUBLIC void overflow(void); //溢出
35 | PUBLIC void bound_range_exceeded(void); //出界
36 | PUBLIC void invalid_opcode(void); //非法指令码
37 | PUBLIC void device_not_available(void); //设备不可用
38 | PUBLIC void double_fault(void); //双重错误
39 | PUBLIC void coprocessor_segment_overrun(void); //协处理器错误
40 | PUBLIC void invalid_tss(void); //无效TSS
41 | PUBLIC void segment_not_present(void); //段不存在
42 | PUBLIC void stack_segment_fault(void); //栈段错误
43 | PUBLIC void general_protection_fault(void); //通用保护错误
44 | PUBLIC void page_fault(void); //缺页中断
45 | PUBLIC void reserved1_excepetion(void); //保留
46 | PUBLIC void x87_floating_point_exception(void); //x87数学协处理器浮点运算错误
47 | PUBLIC void alignment_check(void); //对齐检测
48 | PUBLIC void machine_check(void); //机器检测
49 | PUBLIC void simd_floating_point_exception(void); //SIMD浮点运算错误
50 | PUBLIC void virtualization_exception(void); //虚拟化异常
51 | PUBLIC void control_protection_exception(void); //控制保护错误
52 | PUBLIC void reserved2_excepetion(void); //保留
53 | PUBLIC void reserved3_excepetion(void); //保留
54 | PUBLIC void reserved4_excepetion(void); //保留
55 | PUBLIC void reserved5_excepetion(void); //保留
56 | PUBLIC void reserved6_excepetion(void); //保留
57 | PUBLIC void reserved7_excepetion(void); //保留
58 | PUBLIC void hypervisor_injection_exception(void); //VMM注入错误
59 | PUBLIC void vmm_communication_exception(void); //VMM交流错误
60 | PUBLIC void security_exception(void); //安全性错误
61 | PUBLIC void reserved8_excepetion(void); //保留
--------------------------------------------------------------------------------
/kernel/intterup/init_int.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * init_int.h
12 | *
13 | * 初始化中断
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //中断参数
24 | struct intterup_args {
25 | b64 r15,
26 | r14,
27 | r13,
28 | r12,
29 | r11,
30 | r10,
31 | r9,
32 | r8,
33 | rdi,
34 | rsi,
35 | rdx,
36 | rcx,
37 | rbx,
38 | rax,
39 | gs,
40 | fs,
41 | es,
42 | ds,
43 | pgd,
44 | rbp;
45 | b64 ist;
46 | b64 rip,
47 | cs,
48 | rflags,
49 | rsp,
50 | ss;
51 | } __attribute__((packed));
52 |
53 | //初始化PIC
54 | PUBLIC void init_pic(void);
55 | //禁用/启用 IRQ
56 | PUBLIC void disable_irq(int irq);
57 | PUBLIC void enable_irq(int irq);
58 | //发送EOI
59 | PUBLIC void send_eoi(int irq);
60 | //初始化IDT
61 | PUBLIC void init_idt(void);
62 | typedef void(* int_handler)(void);
--------------------------------------------------------------------------------
/kernel/intterup/irq_handler.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * irq_handler.c
12 | *
13 | * IRQ处理
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 | #include
21 |
22 | #include
23 | #include
24 |
25 | PRIVATE irq_handler IRQ_HANDLERS[16] = {};
26 |
27 | //注册IRQ处理器
28 | PUBLIC void register_irq_handler(int irq, irq_handler handler) {
29 | IRQ_HANDLERS[irq] = handler;
30 | }
31 |
32 | PUBLIC bool entered_handler = false;
33 |
34 | //通用IRQ处理器
35 | PUBLIC void general_irq_handler(int irq, struct intterup_args *args) {
36 | __set_cr3(kernel_pml4);
37 |
38 | bool flag = entered_handler;
39 | if (! flag) {
40 | entered_handler = true;
41 | }
42 |
43 | //禁用同类中断接收
44 | disable_irq(irq);
45 |
46 | //发送EOI
47 | send_eoi(irq);
48 |
49 | if (IRQ_HANDLERS[irq] != NULL) {
50 | IRQ_HANDLERS[irq](irq, args, flag);
51 | }
52 |
53 | //启用同类中断接收
54 | enable_irq(irq);
55 |
56 | if (! flag) {
57 | entered_handler = false;
58 | }
59 | }
--------------------------------------------------------------------------------
/kernel/intterup/irq_handler.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * irq_handler.h
12 | *
13 | * IRQ处理
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | #include
24 |
25 | typedef void(* irq_handler)(int, struct intterup_args*, bool);
26 | //注册IRQ处理器
27 | PUBLIC void register_irq_handler(int irq, irq_handler handler);
28 | //通用IRQ处理器
29 | PUBLIC void general_irq_handler(int irq, struct intterup_args *args);
30 | //系统调用处理器
31 | PUBLIC void syscall_int_handler(struct intterup_args *regs);
32 |
33 | //IRQ处理器
34 | void irq0_handler(void);
35 | void irq1_handler(void);
36 | void irq2_handler(void);
37 | void irq3_handler(void);
38 | void irq4_handler(void);
39 | void irq5_handler(void);
40 | void irq6_handler(void);
41 | void irq7_handler(void);
42 | void irq8_handler(void);
43 | void irq9_handler(void);
44 | void irq10_handler(void);
45 | void irq11_handler(void);
46 | void irq12_handler(void);
47 | void irq13_handler(void);
48 | void irq14_handler(void);
49 | void irq15_handler(void);
50 |
51 | //系统调用处理器
52 | void syscall_handler(void);
--------------------------------------------------------------------------------
/kernel/kernel.ld:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * kernel.ld
12 | *
13 | * 内核LD脚本
14 | *
15 | */
16 |
17 |
18 |
19 | ENTRY(_start)
20 | OUTPUT_FORMAT("elf64-x86-64")
21 | OUTPUT_ARCH("i386:x86-64")
22 |
23 | SECTIONS
24 | {
25 | . = 0x1500000;
26 | .text : { *(.text) }
27 | .data : { *(.data) }
28 | .bss : { *(.bss) }
29 | }
--------------------------------------------------------------------------------
/kernel/kmod_loader.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * kmod_loader.h
12 | *
13 | * 内核模块加载器
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | //程序信息
25 | typedef struct {
26 | void *start;
27 | void *end;
28 | void *entry;
29 | void *pgd;
30 | void *stack_top;
31 | void *stack_bottom;
32 | void *heap_bottom;
33 | void *heap_top;
34 | } program_info;
35 |
36 | //从内存中加载kmod
37 | PUBLIC program_info load_kmod_from_memory(void *addr);
38 | //初始化kmod进程
39 | PUBLIC task_struct *initialize_kmod_task(task_struct *kmod_task);
--------------------------------------------------------------------------------
/kernel/libs/ctype.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2021, 2021 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * ctype.h
12 | *
13 | * 标准库ctype实现
14 | *
15 | */
16 |
17 | #include
18 |
19 | //利用提供好的宏实现
20 | //目前只支持ascii字符
21 |
22 | //空字符
23 | int isspace(int ch) {
24 | return __isspace(ch);
25 | }
26 |
27 | //大写字母
28 | int isupper(int ch) {
29 | return __isupper(ch);
30 | }
31 |
32 | //小写字母
33 | int islower(int ch) {
34 | return __islower(ch);
35 | }
36 |
37 | //字母
38 | int isalpha(int ch) {
39 | return __isalpha(ch);
40 | }
41 |
42 | //数字
43 | int isdigit(int ch) {
44 | return __isdigit(ch);
45 | }
46 |
47 | //字母/数字
48 | int isalnum(int ch) {
49 | return __isalnum(ch);
50 | }
51 |
52 | //空格
53 | int isblank(int ch) {
54 | return __isblank(ch);
55 | }
56 |
57 | //控制符
58 | int iscntrl(int ch) {
59 | return __iscntrl(ch);
60 | }
61 |
62 | //可输出字符
63 | int isprint(int ch) {
64 | return __isprint(ch);
65 | }
66 |
67 | //可见字符
68 | int isgraph(int ch) {
69 | return __isgraph(ch);
70 | }
71 |
72 | //符号
73 | int ispunct(int ch) {
74 | return __ispunct(ch);
75 | }
76 |
77 | //16进制位
78 | int isxdigit(int ch) {
79 | return __isxdigit(ch);
80 | }
81 |
82 | //8进制位
83 | int isodigit(int ch) {
84 | return __isodigit(ch);
85 | }
86 |
87 | //大写字母->小写字母
88 | int tolower(int ch) {
89 | return __tolower(ch);
90 | }
91 |
92 | //小写字母->大写字母
93 | int toupper(int ch) {
94 | return __toupper(ch);
95 | }
--------------------------------------------------------------------------------
/kernel/libs/debug.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * debug.c
12 | *
13 | * assert与panic
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 |
23 | #include
24 |
25 | #include
26 | #include
27 |
28 | void assertion_failure(const char *expression, const char *file, const char *base_file, int line) {
29 | lerror("assert", "assert(%s) failed; file:%s ; base file: %s ; line: %d\n", expression, file, base_file, line);
30 |
31 | while (true);
32 | //不应该被执行
33 | asmv ("ud2");
34 | }
35 |
36 | void panic_failure(const char *expression, const char *file, const char *base_file, int line) {
37 | //关中断(阻止进程切换)
38 | dis_int();
39 |
40 | lerror("panic", "panic_assert(%s) failed; file:%s ; base file: %s ; line: %d\n", expression, file, base_file, line);
41 |
42 | while (true);
43 | //不应该被执行
44 | asmv ("ud2");
45 | }
46 |
47 | void panic(const char *format, ...) {
48 | //关中断(阻止进程切换)
49 | dis_int();
50 |
51 | va_list args;
52 | va_start(args, format);
53 |
54 | static char buffer[256];
55 |
56 | vsprintk(buffer, format, args);
57 |
58 | lerror("panic", "%s", buffer);
59 |
60 | va_end(args);
61 |
62 | while (true);
63 | //不应该被执行
64 | asmv ("ud2");
65 | }
--------------------------------------------------------------------------------
/kernel/libs/string.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2021, 2021 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * string.c
12 | *
13 | * 标准库string实现
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 | #include
21 |
22 | int strcmp(const char *str1, const char *str2) {
23 | int len1 = strlen(str1);
24 | int len2 = strlen(str2);
25 | //长度不等
26 | if (len1 != len2) {
27 | if (len1 < len2) {
28 | return -1;
29 | }
30 | else {
31 | return 1;
32 | }
33 | }
34 | int res = 0;
35 | while (*str1 != '\0') {
36 | res = (*str1) - (*str2);
37 | //字符不等
38 | if (res != 0) {
39 | break;
40 | }
41 | str1 ++;
42 | str2 ++;
43 | }
44 |
45 | //前者小于后者
46 | if (res < 0) {
47 | return -1;
48 | }
49 | else if (res > 0) {
50 | return 1;
51 | }
52 | //二者相等
53 | return 0;
54 | }
55 |
56 | int strlen(const char *str) {
57 | int cnt = 0x7FFFFFFF;
58 | //使用字符串扫描指令
59 | __asm__ __volatile__ (
60 | "cld\n"
61 | "repnz\n"
62 | "scasb" :
63 | "+c" (cnt) : "D" (str), "a" (0));
64 | return 0x7FFFFFFF - cnt - 1;
65 | }
66 |
67 | char *strcpy(void *dst, const char *src) {
68 | char *r = dst;
69 | do {
70 | *(r ++) = *(src ++);
71 | } while (*src != '\0'); //遇到\0结束(复制后)
72 | return dst;
73 | }
--------------------------------------------------------------------------------
/kernel/logging.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * logging.h
12 | *
13 | * 日志
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | //初始化串口
25 | PUBLIC void init_serial(void);
26 |
27 | //输出字符(串)
28 | PUBLIC void write_serial_char(char ch);
29 | PUBLIC void write_serial_str(const char *str);
30 |
31 | //日志类型
32 | enum {
33 | INFO = 0,
34 | WARN,
35 | ERROR,
36 | FATAL,
37 | TIPS,
38 | ATTENTION
39 | };
40 |
41 | //打印日志
42 | PUBLIC void __log(const char *name, const char *type, const char *msg);
43 | PUBLIC void _log(const char *name, const int type, const char *fmt, va_list args);
44 | PUBLIC void log(const char *name, const char *type, const char *fmt, ...);
45 | PUBLIC void linfo(const char *name, const char *fmt, ...);
46 | PUBLIC void lwarn(const char *name, const char *fmt, ...);
47 | PUBLIC void lerror(const char *name, const char *fmt, ...);
48 | PUBLIC void lfatal(const char *name, const char *fmt, ...);
49 | PUBLIC void ltips(const char *name, const char *fmt, ...);
50 | PUBLIC void lattention(const char *name, const char *fmt, ...);
--------------------------------------------------------------------------------
/kernel/memory/kheap.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * kheap.h
12 | *
13 | * 内核堆
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | PUBLIC void init_kheap(void *prepare_base, size_t prepare_size);
24 | PUBLIC void *kmalloc(size_t size); //分配内存
25 | static inline void *kcalloc(int num, size_t size) { //分配num个size的内存
26 | return kmalloc(num * size);
27 | }
28 | PUBLIC void kfree(void *ptr); //释放内存
--------------------------------------------------------------------------------
/kernel/memory/paging.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * paging.h
12 | *
13 | * 页表
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //创建页表
24 | PUBLIC void *create_pgd(void);
25 | //设置映射
26 | PUBLIC bool set_mapping(void *pgd, void *vaddr, void *paddr, int pages, bool rw, bool us);
27 | PUBLIC bool set_mappingvv(void *src_pgd, void *src_addr, void *dst_pgd, void *dst_addr, int pages, bool rw, bool us);
28 | //获取物理地址
29 | PUBLIC void *get_physical_address(void *__pgd, void *vaddr);
30 |
31 | #define __pa(pgd, vaddr) (get_physical_address(pgd, vaddr))
--------------------------------------------------------------------------------
/kernel/memory/pmm.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * pmm.h
12 | *
13 | * 物理内存管理
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | //0~reserved_limit = used
25 | //初始化pmm
26 | PUBLIC void init_pmm(size_t memsize, void *reserved_limit);
27 | //分配空闲内存
28 | PUBLIC void *__alloc_free_pages(int order, int *order_give);
29 | PUBLIC void *alloc_pages(int order);
30 | //归还内存
31 | PUBLIC void __return_pages(void *addr, int order);
32 | PUBLIC void return_pages(void *addr, int pages);
33 | //给给定页表分配逻辑上连续的内存
34 | PUBLIC void alloc_vpages(void *pgd, void *addr, int pages);
35 | //虚拟地址memset
36 | PUBLIC void vmemset(void *pgd, void *addr, int val, size_t size);
37 | //虚拟地址拷贝到物理地址
38 | PUBLIC void vpmemcpy(void *dst, void *src_pgd, void *src, size_t size);
39 | //物理地址拷贝到虚拟地址
40 | PUBLIC void pvmemcpy(void *dst_pgd, void *dst, void *src, size_t size);
41 | //虚拟地址拷贝到虚拟地址
42 | PUBLIC void vvmemcpy(void *dst_pgd, void *dst, void *src_pgd, void *src, size_t size);
--------------------------------------------------------------------------------
/kernel/memory/shared_memory.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: Flysong
10 | *
11 | * shared_memory.c
12 | *
13 | * 共享内存
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 | #include
21 | #include
22 | #include
23 |
24 | //进行映射
25 | PUBLIC bool shm_mapping(void *mem, int pages, int src_pid, int target_pid) {
26 | //获取原页表与新页表
27 | void *target_pgd = get_task_by_pid(target_pid)->mm_info.pgd;
28 | void *source_pgd = get_task_by_pid(src_pid)->mm_info.pgd;
29 |
30 | //映射
31 | bool ret =set_mappingvv(source_pgd, mem, target_pgd, mem, pages, true, true);
32 |
33 | return ret;
34 | }
--------------------------------------------------------------------------------
/kernel/memory/shared_memory.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: Flysong
10 | *
11 | * shared_memory.h
12 | *
13 | * 共享内存
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //映射内存
24 | //将src进程从mem开始的pages个页映射到target进程
25 | PUBLIC bool shm_mapping(void *mem, int pages, int src_pid, int target_pid);
--------------------------------------------------------------------------------
/kernel/printk.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * printk.h
12 | *
13 | * printk
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | #include
24 |
25 | PUBLIC void init_video(void *video_addr, int width, int height);
26 |
27 | //写生字符(串)
28 | PUBLIC void write_char(char ch, int color, int posx, int posy);
29 | PUBLIC void write_str(const char *str, int color, int posx, int posy);
30 |
31 | //输出字符(串)
32 | PUBLIC void putchar(char ch);
33 | PUBLIC void puts(const char *str);
34 |
35 | //杂项
36 | PUBLIC void clrscr(void);
37 | PUBLIC int get_print_color(void);
38 | PUBLIC void set_print_color(int color);
39 | PUBLIC void change_pos(int x, int y);
40 | PUBLIC int get_pos_x(void);
41 | PUBLIC int get_pos_y(void);
42 |
43 | //printk变种
44 | PUBLIC int vsprintk(char *buffer, const char *format, va_list args);
45 | PUBLIC int sprintk(char *buffer, const char *format, ...);
46 | PUBLIC int printk(const char *format, ...);
--------------------------------------------------------------------------------
/kernel/syscall/dosyscall.S:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * dosyscall.S
12 | *
13 | * 系统调用
14 | *
15 | */
16 |
17 |
18 |
19 | .global dosyscall
20 | dosyscall: //dosyscall(int sysno, qword mode, qword counter, qword data, void *src, void *dst, qword arg1, qword arg2, qword arg3, qword arg4, qword arg5, qword arg6, qword arg7, qword arg8)
21 | pushq %rbx
22 | pushq %r12
23 | pushq %r13
24 | pushq %r14
25 | pushq %r15
26 |
27 | movq %rdi, %rax
28 | movq %rsi, %rbx
29 | xchg %rdx, %rcx
30 | movq %r8, %rsi
31 | movq %r9, %rdi
32 | movq 48(%rsp), %r8
33 | movq 56(%rsp), %r9
34 | movq 64(%rsp), %r10
35 | movq 72(%rsp), %r11
36 | movq 80(%rsp), %r12
37 | movq 88(%rsp), %r13
38 | movq 96(%rsp), %r14
39 | movq 104(%rsp), %r15
40 | int $0x40
41 |
42 | popq %r15
43 | popq %r14
44 | popq %r13
45 | popq %r12
46 | popq %rbx
47 |
48 | ret
49 |
--------------------------------------------------------------------------------
/kernel/syscall/rpc.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * rpc.h
12 | *
13 | * 远程过程调用
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | //类型定义
25 | typedef qword rpc_func;
26 | typedef dword rpc_size;
27 |
28 | typedef struct {
29 | qword data : 44;
30 | int size : 20;
31 | } rpc_args_struct;
32 |
33 | typedef rpc_args_struct(* rpc_proccess_wrapper)(int, rpc_func, rpc_args_struct);
34 |
35 | #define ARG_READ(args, type) *(type *)(((args) = (((void *)(args)) + sizeof(type))) - sizeof(type))
36 | #define ARG_WRITE(args, type, value) *(type *)(((args) = (((void *)(args)) + sizeof(type))) - sizeof(type)) = value
37 |
38 | //处理RPC请求
39 | PUBLIC void deal_rpc_request(msgpack_struct pack, void *msg);
40 | PUBLIC void rpc_register(rpc_func func, rpc_proccess_wrapper process, rpc_size return_size, rpc_size args_size); //当args_size = -1时, 不对参数大小进行检验
41 | PUBLIC rpc_args_struct rpc_call(int service, rpc_func func, rpc_args_struct args, rpc_size return_size, void *result);
42 | //强烈不推荐使用此函数!!! 只在测试情况下使用!!! 使用前确保程序的消息缓冲区为空
43 | PUBLIC rpc_args_struct rpc_direct_call(int service, rpc_func func, rpc_args_struct args, rpc_size return_size, void *result);
--------------------------------------------------------------------------------
/kernel/syscall/sys_task.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * sys_task.h
12 | *
13 | * 系统调用进程
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //系统调用进程
24 | //使用rpc系统实现
25 | PUBLIC void sys_task(void);
--------------------------------------------------------------------------------
/kernel/syscall/syscall.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * syscall.h
12 | *
13 | * 系统调用
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | #include
24 |
25 | // rax rbx rcx rdx rsi rdi
26 | PUBLIC qword syscall(int sysno, qword mode, qword counter, qword data, void *src, void *dst,
27 | // r8 r9 r10 r11 r12 r13 r14 r15
28 | qword arg1, qword arg2, qword arg3, qword arg4, qword arg5, qword arg6, qword arg7, qword arg8);
29 |
30 | PUBLIC qword dosyscall(int sysno, qword mode, qword counter, qword data, void *src, void *dst,
31 | qword arg1, qword arg2, qword arg3, qword arg4, qword arg5, qword arg6, qword arg7, qword arg8);
--------------------------------------------------------------------------------
/kernel/syscall/syscalls.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * syscalls.h
12 | *
13 | * 系统调用的实现
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | #include
25 |
26 | //超级牛力
27 | #define MOO_SN (0x00)
28 | PUBLIC void __moo(void);
29 | PUBLIC void moo(void);
30 |
31 | //IPC相关
32 | #define SEND_MSG_SN (0x01)
33 | PUBLIC bool __send_msg(msgno_id msgno, void *src, qword size, int dst);
34 | PUBLIC bool send_msg(msgno_id msgno, void *src, qword size, int dst);
35 |
36 | #define CHECK_IPC_SN (0x02)
37 | PUBLIC void __check_ipc(void);
38 | PUBLIC void check_ipc(void);
39 |
40 | #define SET_ALLOW_SN (0x03)
41 | PUBLIC void __set_allow(int pid);
42 | PUBLIC void set_allow(int pid);
43 |
44 | #define RECV_MSG_SN (0x04)
45 | PUBLIC msgpack_struct __recv_msg(void *dst);
46 | PUBLIC msgpack_struct recv_msg(void *dst);
47 |
48 | #define CLEAR_MAIL_SN (0x05)
49 | PUBLIC void __clear_mail(void);
50 | PUBLIC void clear_mail(void);
51 |
52 | //锁
53 | #define TEST_AND_LOCK_SN (0x06)
54 | PUBLIC bool __test_and_lock(bool *val);
55 | PUBLIC bool test_and_lock(bool *val);
56 |
57 | #define SET_MAILBUFFER_SN (0x07)
58 | PUBLIC void __set_mailbuffer(void *buffer, qword size);
59 | PUBLIC void set_mailbuffer(void *buffer, qword size);
60 |
61 | #define CREATE_SIGNAL_SN (0x08)
62 | PUBLIC id_t create_signal(int max_signals, int value, bool soft);
63 |
64 | #define GET_SIGNALS_SN (0x09)
65 | PUBLIC int get_signals(id_t id);
66 |
67 | #define UP_SIGNAL_SN (0x0A)
68 | PUBLIC void up_signal(id_t id);
69 |
70 | #define DOWN_SIGNAL_SN (0x0B)
71 | PUBLIC void down_signal(id_t id);
72 |
73 | #define IS_SOFT_SIGNAL_SN (0x0C)
74 | PUBLIC bool is_soft_signal(id_t id);
75 |
76 | //其他
77 | #define REG_IRQ_SN (0x10)
78 | PUBLIC void __reg_irq(int irq);
79 | PUBLIC void reg_irq(int irq);
80 |
81 | typedef void(*thread_function)(void *);
82 |
83 | //线程
84 | #define CREATE_THREAD_SN (0x20)
85 | PUBLIC int __create_thread(thread_function entry, void *args);
86 | PUBLIC int create_thread(thread_function entry, void *args);
87 |
88 | #define EXIT_THREAD_SN (0x21)
89 | PUBLIC void __exit_thread(void *retval);
90 | PUBLIC void exit_thread(void *retval);
91 |
92 | //IRQ处理器
93 | PUBLIC void normal_irq_handler(int irq, struct intterup_args *args, bool flags);
94 |
95 | //dummy进程发送消息
96 | PUBLIC bool dummy_send_msg(msgno_id msgno, void *src, qword size, int dst);
--------------------------------------------------------------------------------
/kernel/task/signal.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * signal.h
12 | *
13 | * 信号量
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //创建信号量
24 | PUBLIC id_t __create_signal(int max_signals, int value, bool soft);
25 | //up/down原语
26 | PUBLIC void __up_signal(id_t id);
27 | PUBLIC void __down_signal(id_t id);
28 | //获得信号量
29 | PUBLIC int __get_signals(id_t id);
30 | //是否为soft信号量
31 | PUBLIC bool __is_soft_signal(id_t id);
--------------------------------------------------------------------------------
/kernel/task/task_manager.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * task_manager.h
12 | *
13 | * 进程管理器
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | #define EMPTY_TASK_PID (-1)
25 |
26 | //获取空进程
27 | PUBLIC task_struct *get_empty_task(void);
28 | //设置空进程
29 | PUBLIC void create_empty_task(void);
30 |
31 | //是否为空进程
32 | static inline bool is_empty(task_struct *task) {
33 | return task->pid == EMPTY_TASK_PID;
34 | }
35 |
36 | //分配pid
37 | static inline int alloc_pid(void) {
38 | return cur_pid ++;
39 | }
40 |
41 | //获取进程表
42 | PUBLIC task_struct *get_task_list(void);
43 | //获取进程
44 | PUBLIC task_struct *get_task_by_pid(int pid);
45 | //增加进程
46 | PUBLIC void add_task(task_struct *task);
47 | //移除进程
48 | PUBLIC void remove_task(task_struct *task);
49 |
50 | //获取空闲进程表
51 | PUBLIC thread_info_struct *get_level0_list(void);
52 | PUBLIC thread_info_struct *get_level1_list(void);
53 |
54 | //获取空闲进程
55 | PUBLIC thread_info_struct *dequeue_level0_thread(void);
56 | PUBLIC thread_info_struct *dequeue_level1_thread(void);
57 |
58 | //level0是否有thread
59 | PUBLIC bool has_level0_thread(void);
60 | //level1是否有thread
61 | PUBLIC bool has_level1_thread(void);
62 |
63 | //加入空闲进程
64 | PUBLIC void enqueue_level0_thread(thread_info_struct *thread);
65 | PUBLIC void enqueue_level1_thread(thread_info_struct *thread);
66 | PUBLIC void enqueue_thread(thread_info_struct *thread);
67 |
68 | //创建进程(low level)
69 | PUBLIC task_struct *__create_task(
70 | word ds, void *stack_top, void *stack_bottom, void *entry, word cs, qword rflags,
71 | void *pgd, qword code_start, qword code_end, qword data_start, qword data_end, qword heap_start, qword heap_end, qword rodata_start, qword rodata_end,
72 | qword shm_start, qword shm_end,
73 | int pid, int priority, int level, task_struct *parent, bool kernel_task
74 | );
75 |
76 | //创建进程
77 | PUBLIC task_struct *create_task(
78 | word ds, void *stack_top, void *stack_bottom, void *entry, word cs, qword rflags,
79 | void *pgd, qword code_start, qword code_end, qword data_start, qword data_end, qword heap_start, qword heap_end, qword rodata_start, qword rodata_end,
80 | qword shm_start, qword shm_end,
81 | int pid, int priority, int level, task_struct *parent, bool kernel_task
82 | );
--------------------------------------------------------------------------------
/kernel/task/task_scheduler.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * task_scheduler.h
12 | *
13 | * 进程调度器
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | //当前进程
25 | EXTERN PUBLIC thread_info_struct *current_thread;
26 | //开始调度
27 | PUBLIC void start_schedule(void);
28 | //获得下一个运行的线程
29 | PUBLIC thread_info_struct *get_next_run_thread(void);
30 | //是否需要调度
31 | PUBLIC bool should_switch(void);
32 | //进行调度
33 | PUBLIC void do_switch(struct intterup_args *regs);
34 | //系统调用之后
35 | PUBLIC void after_syscall(struct intterup_args *regs);
--------------------------------------------------------------------------------
/kernel/task/task_struct.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * task_struct.h
12 | *
13 | * PCB
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 | #include
24 |
25 | //内存管理信息
26 | typedef struct {
27 | void *pgd;
28 | qword code_start, code_end; //代码段
29 | qword data_start, data_end; //数据段
30 | qword stack_top, stack_bottom; //栈段
31 | qword heap_start, heap_end; //堆段
32 | qword rodata_start, rodata_end; //只读数据段
33 | qword shm_start, shm_end; //共享内存区
34 | qword shm_ptr; //共享内存指针
35 | } mm_info_struct;
36 |
37 | //特殊PID
38 | #define NULL_TASK (0)
39 | #define ANY_TASK (-1)
40 | #define DUMMY_TASK (-2)
41 |
42 | //IPC信息
43 | typedef struct {
44 | void *mail; //邮箱
45 | msgpack_struct *lastest_msg; //最新消息
46 | void *write_ptr; //写入指针
47 | void *read_ptr; //读入指针
48 | qword mail_size; //邮箱大小
49 | qword used_size; //已使用大小
50 | int allow_pid; //允许的PID
51 | thread_info_struct *msg_handler_thread; //消息处理线程
52 | } ipc_info_struct;
53 |
54 | typedef struct __task_struct {
55 | //进程信息
56 | thread_info_struct *threads;
57 | mm_info_struct mm_info;
58 | ipc_info_struct ipc_info;
59 |
60 | //进程链表
61 | struct __task_struct *last;
62 | struct __task_struct *next;
63 |
64 | //进程树
65 | struct __task_struct *parent;
66 | struct __task_struct *children;
67 | struct __task_struct *bro;
68 |
69 | int pid; //PID
70 | int level; //调度级别
71 | int cur_tid;
72 |
73 | word cs;
74 | word ds;
75 | qword rflags;
76 | int priority;
77 | bool kernel_task;
78 | } task_struct;
79 |
80 | #define TASK_INIT_HEAP_SIZE (0x10000) //初始化堆大小
--------------------------------------------------------------------------------
/kernel/task/thread.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * thread.h
12 | *
13 | * 线程
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //进程的寄存器信息
24 | typedef struct {
25 | //基本寄存器
26 | struct {
27 | b64 r15,
28 | r14,
29 | r13,
30 | r12,
31 | r11,
32 | r10,
33 | r9,
34 | r8,
35 | rdi,
36 | rsi,
37 | rdx,
38 | rcx,
39 | rbx,
40 | rax,
41 | gs,
42 | fs,
43 | es,
44 | ds;
45 | } basic;
46 | //栈寄存器
47 | struct {
48 | b64 rbp;
49 | b64 rsp,
50 | ss;
51 | } stack;
52 | //程序寄存器
53 | struct {
54 | b64 rip,
55 | cs,
56 | rflags;
57 | } program;
58 | } register_info_struct;
59 |
60 | typedef struct __task_struct task_struct;
61 |
62 | typedef struct __thread_info_struct {
63 | register_info_struct registers;
64 |
65 | //线程状态
66 | enum {
67 | READY = 0,
68 | RUNNING,
69 | SUBBMITED,
70 | WAITING,
71 | WAITING_IPC,
72 | TERMINATED,
73 | EXCEPTION,
74 | SUSPEND,
75 | WAITING_SUSPEND,
76 | WAITING_IPC_SUSPEND
77 | } state;
78 |
79 | struct __thread_info_struct *next;
80 | struct __thread_info_struct *last;
81 | //空闲链表
82 | struct __thread_info_struct *free_next;
83 |
84 | task_struct *task;
85 |
86 | int count; //时间片
87 | int priority; //优先级
88 |
89 | int tid;
90 | }thread_info_struct;
91 |
92 | PUBLIC thread_info_struct *create_thread_info(
93 | word ds, void *stack_top, void *stack_bottom, void *entry, word cs, qword rflags, int priority, task_struct *task
94 | );
95 |
96 | //创建线程
97 | PUBLIC thread_info_struct *__create_thread__(void *entry, task_struct *task);
98 |
99 | //移除线程
100 | PUBLIC void remove_thread(thread_info_struct *thread);
101 |
102 | //根据tid获得线程
103 | PUBLIC thread_info_struct *get_thread_by_tid(int tid, task_struct *task);
104 |
105 | #define THREAD_STACK_SIZE (0x800000) //线程栈大小
106 | #define THREAD_INIT_STACK_SIZE (0x10000) //初始化栈大小
--------------------------------------------------------------------------------
/libs/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/libs/
20 | export OBJECTSDIR
21 |
22 | #编译
23 | .PHONY: build
24 | build:
25 | $(CD) libc && $(MAKE) build
26 | $(CD) libkmod && $(MAKE) build
27 | $(CD) libfifo && $(MAKE) build
28 |
29 | #清理
30 | .PHONY: clean
31 | clean:
32 | $(CD) libc && $(MAKE) clean
33 | $(CD) libkmod && $(MAKE) clean
34 | $(CD) libfifo && $(MAKE) clean
--------------------------------------------------------------------------------
/libs/libc/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/libc/
20 |
21 | LIBC_OUTPUT := $(BINDIR)/libc.a
22 |
23 | CFLAGS := -m64 -Wall -Wno-int-conversion -Wstrict-prototypes -mfpmath=387 -mcmodel=large \
24 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
25 | -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone \
26 | -mpreferred-stack-boundary=3 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
27 |
28 | CPPFLAGS := -m64 -Wall -Wno-int-conversion -Wstrict-prototypes -mfpmath=387 -mcmodel=large \
29 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
30 | -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone \
31 | -mpreferred-stack-boundary=3 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
32 |
33 | MISC_C_SRC := timer
34 | C_SRC := $(foreach src, $(MISC_C_SRC), misc/$(src))
35 |
36 | CPP_SRC :=
37 |
38 | ASMFLAGS :=
39 | ASM_SRC := ipc/ipc_func
40 |
41 | C_DEFS := $(ARCHDEF_C)
42 | C_INCLUDE := -I"$(ROOTDIR)/libs/libc"
43 |
44 | CPP_DEFS := $(ARCHDEF_C)
45 | CPP_INCLUDE := -I"$(ROOTDIR)/libs/libc"
46 |
47 | OBJECTS := $(foreach obj, $(ASM_SRC), $(OBJECTSDIR)/$(obj).o) \
48 | $(foreach obj, $(C_SRC), $(OBJECTSDIR)/$(obj).o) \
49 | $(foreach obj, $(CPP_SRC), $(OBJECTSDIR)/$(obj).o)
50 |
51 | $(OBJECTSDIR)/%.o : %.cpp
52 | $(MKDIR) $(dir $@)
53 | $(GPP) -c $(CPPFLAGS) $(CPP_INCLUDE) $(CPP_DEFS) -o $@ $^
54 |
55 | $(OBJECTSDIR)/%.o : %.c
56 | $(MKDIR) $(dir $@)
57 | $(GCC) -c $(CFLAGS) $(C_INCLUDE) $(C_DEFS) -o $@ $^
58 |
59 | $(OBJECTSDIR)/%.o : %.S
60 | $(MKDIR) $(dir $@)
61 | $(GAS) -o $@ $^
62 |
63 | .PHONY: build
64 | build: $(OBJECTS)
65 | $(MKDIR) $(dir $(LIBC_OUTPUT))
66 | $(AR) rvs $(LIBC_OUTPUT) $^
67 |
68 | .PHONY: clean
69 | clean:
70 | $(RM) $(LIBC_OUTPUT) $(OBJECTS)
71 |
--------------------------------------------------------------------------------
/libs/libc/README.md:
--------------------------------------------------------------------------------
1 | # Libc
2 |
3 | C Standard Library
--------------------------------------------------------------------------------
/libs/libc/ipc/ipc.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * ipc.h
12 | *
13 | * IPC
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | //FIXME: 重构
22 |
23 | //IPC相关函数
24 | int send_msg(void *msg, int dest, int len, int tickout);
25 | int recv_msg(void *msg, int source);
26 | int recv_any_msg(void *msg);
27 | int recv_any_msg_and_wait(void *msg);
--------------------------------------------------------------------------------
/libs/libc/ipc/ipc_func.S:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * int_func.S
12 | *
13 | * IPC相关函数
14 | *
15 | */
16 |
17 |
18 |
19 | /*FIXME: 重构*/
20 |
21 | .global send_msg
22 | .type send_msg, @function
23 | .global recv_msg
24 | .type recv_msg, @function
25 | .global recv_any_msg
26 | .type recv_any_msg, @function
27 | .global recv_any_msg_and_wait
28 | .type recv_any_msg_and_wait, @function
29 |
30 | send_msg: /*int send_msg(void *msg, int dest, int len, int tickout)*/
31 | movq %rsi, %r8
32 | movq %rdi, %rsi
33 |
34 | movq $0, %rax /*sysno = 0(send_msg)*/
35 |
36 | int $0x40
37 | ret
38 |
39 | recv_msg: /*int recv_msg(void *msg, int source)*/
40 | movq %rsi, %r8
41 |
42 | movq $1, %rax /*sysno = 1(recv_msg)*/
43 |
44 | int $0x40
45 | ret
46 |
47 | recv_any_msg: /*int recv_any_msg(void *msg)*/
48 | movq $2, %rax /*sysno = 2(recv_any_msg)*/
49 |
50 | int $0x40
51 | ret
52 |
53 | recv_any_msg_and_wait: /*int recv_any_msg_and_wait(void *msg)*/
54 | movq $11, %rax /*sysno = 11(recv_any_msg_and_wait)*/
55 |
56 | int $0x40
57 | ret
58 |
--------------------------------------------------------------------------------
/libs/libc/misc/timer.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * timer.c
12 | *
13 | * 时间相关
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 | #include
21 |
22 | //获取滴答计数
23 | int get_ticks(void) {
24 | //TODO: 实现此函数
25 | return 0;
26 | }
--------------------------------------------------------------------------------
/libs/libc/misc/timer.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * timer.h
12 | *
13 | * 时间相关
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | //获取滴答计数
22 | //TODO: 实现此函数
23 | int get_ticks(void);
--------------------------------------------------------------------------------
/libs/libfifo/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/libfifo/
20 |
21 | LIBFIFO_OUTPUT := $(BINDIR)/libfifo.a
22 |
23 | CFLAGS := -m64 -Wall -Wno-int-conversion -Wstrict-prototypes -mfpmath=387 -mcmodel=large \
24 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
25 | -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone \
26 | -mpreferred-stack-boundary=3 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
27 |
28 | CPPFLAGS := -m64 -Wall -Wno-int-conversion -Wstrict-prototypes -mfpmath=387 -mcmodel=large \
29 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
30 | -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone \
31 | -mpreferred-stack-boundary=3 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
32 |
33 | C_SRC := fifo
34 | CPP_SRC :=
35 |
36 | ASMFLAGS :=
37 | ASM_SRC :=
38 |
39 | C_DEFS := $(ARCHDEF_C)
40 | C_INCLUDE := -I"$(ROOTDIR)/libs/libfifo" -I"$(ROOTDIR)/libs/libkmod" -I"$(ROOTDIR)/include" -I"$(ROOTDIR)/include/std"
41 |
42 | CPP_DEFS := $(ARCHDEF_C)
43 | CPP_INCLUDE := -I"$(ROOTDIR)/libs/libfifo" -I"$(ROOTDIR)/libs/libkmod" -I"$(ROOTDIR)/include" -I"$(ROOTDIR)/include/std"
44 |
45 | ifeq ($(MODE), debug)
46 | C_DEFS += -D_DEBUG
47 | else
48 | CPP_DEFS += -D_DEBUG
49 | endif
50 |
51 | OBJECTS := $(foreach obj, $(ASM_SRC), $(OBJECTSDIR)/$(obj).o) \
52 | $(foreach obj, $(C_SRC), $(OBJECTSDIR)/$(obj).o) \
53 | $(foreach obj, $(CPP_SRC), $(OBJECTSDIR)/$(obj).o)
54 |
55 | $(OBJECTSDIR)/%.o : %.cpp
56 | $(MKDIR) $(dir $@)
57 | $(GPP) -c $(CPPFLAGS) $(CPP_INCLUDE) $(CPP_DEFS) -o $@ $^
58 |
59 | $(OBJECTSDIR)/%.o : %.c
60 | $(MKDIR) $(dir $@)
61 | $(GCC) -c $(CFLAGS) $(C_INCLUDE) $(C_DEFS) -o $@ $^
62 |
63 | $(OBJECTSDIR)/%.o : %.S
64 | $(MKDIR) $(dir $@)
65 | $(GAS) -o $@ $^
66 |
67 | .PHONY: build
68 | build: $(OBJECTS)
69 | $(MKDIR) $(dir $(LIBFIFO_OUTPUT))
70 | $(AR) rvs $(LIBFIFO_OUTPUT) $^
71 |
72 | .PHONY: clean
73 | clean:
74 | $(RM) $(LIBFIFO_OUTPUT) $(OBJECTS)
75 |
--------------------------------------------------------------------------------
/libs/libfifo/fifo.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * fifo.h
12 | *
13 | * First In First Out Buffer
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | // 创建出的缓冲区大小一般不等于此值
24 | // 创建出的缓冲区大小为离fifo_size最近的MEMUNIT_SZ的倍数减去缓冲区信息的大小
25 | PUBLIC void *create_fifo(size_t fifo_size);
26 | //共享FIFO
27 | PUBLIC void *share_fifo(void *fifo, int target);
28 | //向FIFO写
29 | PUBLIC void fifo_write_bytes(void *fifo, byte *data, size_t size);
30 | //从FIFO读
31 | PUBLIC void fifo_read_bytes(void *fifo, byte *data, size_t size);
32 | PUBLIC size_t fifo_read_all(void *fifo, byte *data);
33 | //FIFO信息
34 | PUBLIC size_t fifo_get_size(void *fifo);
35 | PUBLIC size_t fifo_get_used_size(void *fifo);
36 | PUBLIC size_t fifo_get_free_size(void *fifo);
--------------------------------------------------------------------------------
/libs/libkmod/README.md:
--------------------------------------------------------------------------------
1 | # Libkmod
2 |
3 | Kernel Module Library
--------------------------------------------------------------------------------
/libs/libkmod/debug/debug.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * debug.c
12 | *
13 | * assert与panic
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 |
23 | #include
24 |
25 | #include
26 |
27 | #include
28 |
29 | void assertion_failure(const char *expression, const char *file, const char *base_file, int line) {
30 | lerror ("assert(%s) failed; file:%s ; base file: %s ; line: %d\n", expression, file, base_file, line);
31 |
32 | while (true);
33 | asmv ("ud2"); //不应该被执行
34 | }
35 |
36 | void panic_failure(const char *expression, const char *file, const char *base_file, int line) {
37 | dis_int(); //关中断(阻止进程切换)
38 |
39 | lerror("panic_assert(%s) failed; file:%s ; base file: %s ; line: %d\n", expression, file, base_file, line);
40 |
41 | while (true);
42 | asmv ("ud2"); //不应该被执行
43 | }
44 |
45 | void panic(const char *format, ...) {
46 | dis_int(); //关中断(阻止进程切换)
47 |
48 | va_list args;
49 | va_start(args, format);
50 |
51 | static char buffer[256];
52 |
53 | vsprintf (buffer, format, args);
54 |
55 | lerror ("%s", buffer);
56 |
57 | va_end(args);
58 |
59 | while (true);
60 | asmv ("ud2"); //不应该被执行
61 | }
--------------------------------------------------------------------------------
/libs/libkmod/debug/logging.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * logging.h
12 | *
13 | * 日志
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | //输出字符(串)
25 | PUBLIC void write_serial_char(char ch);
26 | PUBLIC void write_serial_str(const char *str);
27 |
28 | //日志类型
29 | enum {
30 | INFO = 0,
31 | WARN,
32 | ERROR,
33 | FATAL,
34 | TIPS,
35 | ATTENTION
36 | };
37 |
38 | //设置日志名称
39 | PUBLIC void set_logging_name(const char *name);
40 |
41 | //打印日志
42 | PUBLIC void __log(const char *type, const char *msg);
43 | PUBLIC void _log(const int type, const char *fmt, va_list args);
44 | PUBLIC void log(const char *type, const char *fmt, ...);
45 | PUBLIC void linfo(const char *fmt, ...);
46 | PUBLIC void lwarn(const char *fmt, ...);
47 | PUBLIC void lerror(const char *fmt, ...);
48 | PUBLIC void lfatal(const char *fmt, ...);
49 | PUBLIC void ltips(const char *fmt, ...);
50 | PUBLIC void lattention(const char *fmt, ...);
--------------------------------------------------------------------------------
/libs/libkmod/entry.S:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * entry.S
12 | *
13 | * 内核模块入口
14 | *
15 | */
16 |
17 |
18 |
19 | .code64
20 | .text
21 |
22 | .extern __kmod_init__
23 | .global _start
24 | _start:
25 | jmp __kmod_init__
26 |
--------------------------------------------------------------------------------
/libs/libkmod/export/keyboard/__keyboard_driver_fn.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * __keyboard_driver_fn.c
12 | *
13 | * keyboard_driver 函数功能
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 |
23 | #include
24 | #include
25 |
26 | #include
27 |
28 | PUBLIC key_t getkey(void) {
29 | char buffer[GETKEY_ARGS_SIZE];
30 | void *buf = buffer;
31 |
32 | ARG_WRITE(buf, bool, false);
33 |
34 | key_t ret = remote_call(GETKEY_RETURN_TYPE, KEYBOARD_DRIVER_SERVICE, GETKEY_FN, MAKE_ARGS(buffer, GETKEY_ARGS_SIZE));
35 |
36 | return ret;
37 | }
--------------------------------------------------------------------------------
/libs/libkmod/export/keyboard/__keyboard_driver_fn.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * __keyboard_driver_fn.h
12 | *
13 | * keyboard_driver 函数功能
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | #define GETKEY_FN (0)
25 | #define GETKEY_ARGS_SIZE (sizeof(bool))
26 | #define GETKEY_RETURN_TYPE key_t
27 |
28 | PUBLIC key_t getkey(void);
--------------------------------------------------------------------------------
/libs/libkmod/export/keyboard/key_types.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * key_types.h
12 | *
13 | * 按键类型
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | typedef short key_t;
22 |
23 | enum {
24 | ESC = 256,
25 | BACKSPACE,
26 | TAB,
27 | ENTER,
28 | PAD_ENTER,
29 | LCTRL,
30 | RCTRL,
31 | LSHIFT,
32 | RSHIFT,
33 | LALT,
34 | RALT,
35 | PAD_SLASH,
36 | CAPSLOCK,
37 | NUMLOCK,
38 | SCROLL_LOCK,
39 | F1,
40 | F2,
41 | F3,
42 | F4,
43 | F5,
44 | F6,
45 | F7,
46 | F8,
47 | F9,
48 | F10,
49 | PAD_HOME,
50 | HOME,
51 | PAD_UP,
52 | KEY_UP,
53 | PAD_PGUP,
54 | PGUP,
55 | PAD_MINUS,
56 | PAD_LEFT,
57 | KEY_LEFT,
58 | PAD_MID,
59 | PAD_RIGHT,
60 | KEY_RIGHT,
61 | PAD_PLUS,
62 | PAD_END,
63 | KEY_END,
64 | PAD_DOWN,
65 | KEY_DOWN,
66 | PAD_PGDOWN,
67 | PGDOWN,
68 | PAD_INSERT,
69 | KEY_INSERT,
70 | PAD_DOT,
71 | KEY_DELETE,
72 | F11,
73 | F12,
74 | LGUI,
75 | RGUI,
76 | APPS,
77 | PAUSEBREAK
78 | };
--------------------------------------------------------------------------------
/libs/libkmod/export/sys_task/__sys_task_fn.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * __sys_task_fn.c
12 | *
13 | * sys_task 函数功能
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 |
23 | #include
24 |
25 | //共享内存
26 | PUBLIC void *share_memory(void *addr, int pages, int target) {
27 | char buffer[SHARE_MEMORY_ARGS_SIZE];
28 | void *buf = buffer;
29 |
30 | //写入参数
31 | ARG_WRITE(buf, void *, addr);
32 | ARG_WRITE(buf, int, pages);
33 | ARG_WRITE(buf, int, target);
34 |
35 | //调用
36 | void *new_addr = remote_call(SHARE_MEMORY_RETURN_TYPE, SYSTASK_SERVICE, SHARE_MEMORY_FN, MAKE_ARGS(buffer, SHARE_MEMORY_ARGS_SIZE));
37 |
38 | return new_addr;
39 | }
40 |
41 | //创建共享内存
42 | PUBLIC void *create_share_memory(int pages) {
43 | //调用
44 | void *addr = remote_call(CREATE_SHARE_MEMORY_RETURN_TYPE, SYSTASK_SERVICE, CREATE_SHARE_MEMORY_FN, MAKE_ARGS(&pages, CREATE_SHARE_MEMORY_ARGS_SIZE));
45 | return addr;
46 | }
--------------------------------------------------------------------------------
/libs/libkmod/export/sys_task/__sys_task_fn.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * __sys_task_fn.h
12 | *
13 | * sys_task 函数功能
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //共享内存
24 | #define SHARE_MEMORY_FN (0)
25 | #define SHARE_MEMORY_ARGS_SIZE (sizeof(void *) + sizeof(int) * 2)
26 | #define SHARE_MEMORY_RETURN_TYPE void *
27 |
28 | //创建共享内存
29 | #define CREATE_SHARE_MEMORY_FN (1)
30 | #define CREATE_SHARE_MEMORY_ARGS_SIZE (sizeof(int))
31 | #define CREATE_SHARE_MEMORY_RETURN_TYPE void *
32 |
33 | //共享内存
34 | PUBLIC void *share_memory(void *addr, int pages, int target);
35 | //创建共享内存
36 | PUBLIC void *create_share_memory(int pages);
--------------------------------------------------------------------------------
/libs/libkmod/export/tty/__tty_driver_fn.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * __tty_driver_fn.c
12 | *
13 | * __tty_driver_fn 函数功能
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 |
23 | #include
24 | #include
25 |
26 | #include
27 | #include
28 |
29 | PUBLIC void flush_console(int console_no) {
30 | char buffer[FLUSH_CONSOLE_ARGS_SIZE];
31 | void *buf = buffer;
32 |
33 | ARG_WRITE(buf, int, console_no);
34 |
35 | bool ret = remote_call(FLUSH_CONSOLE_RETURN_TYPE, TTY_DRIVER_SERVICE, FLUSH_CONSOLE_FN, MAKE_ARGS(buffer, FLUSH_CONSOLE_ARGS_SIZE));
36 | assert(ret);
37 | }
38 |
39 | PUBLIC bool tty_putchar(int console_no, char ch) {
40 | char buffer[PUTCHAR_ARGS_SIZE];
41 | void *buf = buffer;
42 |
43 | ARG_WRITE(buf, int, console_no);
44 | ARG_WRITE(buf, char, ch);
45 |
46 | bool ret = remote_call(PUTCHAR_RETURN_TYPE, TTY_DRIVER_SERVICE, PUTCHAR_FN, MAKE_ARGS(buffer, PUTCHAR_ARGS_SIZE));
47 | return ret;
48 | }
49 |
50 | PUBLIC bool tty_puts(int console_no, const char *str) {
51 | int num = strlen(str);
52 | int size = num + PUTS_ARGS_BASE_SIZE;
53 |
54 | void *buffer = malloc(size);
55 | void *buf = buffer;
56 |
57 | ARG_WRITE(buf, int, console_no);
58 | ARG_WRITE(buf, int, num);
59 |
60 | memcpy(buf, str, num);
61 |
62 | bool ret = remote_call(PUTS_RETURN_TYPE, TTY_DRIVER_SERVICE, PUTS_FN, MAKE_ARGS(buffer, size));
63 |
64 | return ret;
65 | }
66 |
67 | PUBLIC char tty_getchar(int console_no) {
68 | char buffer[GETCHAR_ARGS_SIZE];
69 | void *buf = buffer;
70 |
71 | ARG_WRITE(buf, int, console_no);
72 |
73 | char ret = remote_call(GETCHAR_RETURN_TYPE, TTY_DRIVER_SERVICE, GETCHAR_FN, MAKE_ARGS(buffer, GETCHAR_ARGS_SIZE));
74 |
75 | return ret;
76 | }
--------------------------------------------------------------------------------
/libs/libkmod/export/tty/__tty_driver_fn.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * __tty_driver_fn.h
12 | *
13 | * __tty_driver_fn 函数功能
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | #define PUTCHAR_FN (0)
24 | #define PUTCHAR_ARGS_SIZE (sizeof(int) + sizeof(char))
25 | #define PUTCHAR_RETURN_TYPE bool
26 |
27 | #define FLUSH_CONSOLE_FN (1)
28 | #define FLUSH_CONSOLE_ARGS_SIZE (sizeof(int))
29 | #define FLUSH_CONSOLE_RETURN_TYPE bool
30 |
31 | #define PUTS_FN (2)
32 | #define PUTS_ARGS_SIZE (-1)
33 | #define PUTS_ARGS_BASE_SIZE (sizeof(int) + sizeof(int))
34 | #define PUTS_RETURN_TYPE bool
35 |
36 | #define GETCHAR_FN (3)
37 | #define GETCHAR_ARGS_SIZE (sizeof(int))
38 | #define GETCHAR_RETURN_TYPE char
39 |
40 | PUBLIC void flush_console(int console_no);
41 | PUBLIC bool tty_putchar(int console_no, char ch);
42 | PUBLIC bool tty_puts(int console_no, const char *str);
43 | PUBLIC char tty_getchar(int console_no);
--------------------------------------------------------------------------------
/libs/libkmod/export/video/__video_driver_fn.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * __video_driver_fn.c
12 | *
13 | * video_driver 函数功能
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 |
23 | #include
24 | #include
25 |
26 | #include
27 |
28 | PUBLIC bool write_char(int column, int line, byte color, byte ch) {
29 | char buffer[TEXT_WRITE_CHAR_ARGS_SIZE];
30 | void *buf = buffer;
31 |
32 | ARG_WRITE(buf, int, column);
33 | ARG_WRITE(buf, int, line);
34 | ARG_WRITE(buf, byte, color);
35 | ARG_WRITE(buf, byte, ch);
36 |
37 | bool ret = remote_call(TEXT_WRITE_CHAR_RETURN_TYPE, VIDEO_DRIVER_SERVICE, TEXT_WRITE_CHAR_FN, MAKE_ARGS(buffer, TEXT_WRITE_CHAR_ARGS_SIZE));
38 |
39 | return ret;
40 | }
41 |
42 | PUBLIC bool write_string(int column, int line, byte color, const char *str) {
43 | int num = strlen(str);
44 | int size = num + TEXT_WRITE_STRING_ARGS_BASE_SIZE;
45 |
46 | char buffer = malloc(size);
47 | void *buf = buffer;
48 |
49 | ARG_WRITE(buf, int, column);
50 | ARG_WRITE(buf, int, line);
51 | ARG_WRITE(buf, byte, color);
52 | ARG_WRITE(buf, int, num);
53 |
54 | memcpy(buf, str, num);
55 |
56 | bool ret = remote_call(TEXT_WRITE_STRING_RETURN_TYPE, VIDEO_DRIVER_SERVICE, TEXT_WRITE_STRING_FN, MAKE_ARGS(buffer, size));
57 |
58 | return ret;
59 | }
60 |
61 | PUBLIC create_framebuffer_result create_framebuffer(int column, int line, int width, int height) {
62 | char buffer[CREATE_FRAMEBUFFER_ARGS_SIZE];
63 | void *buf = buffer;
64 |
65 | ARG_WRITE(buf, int, column);
66 | ARG_WRITE(buf, int, line);
67 | ARG_WRITE(buf, int, width);
68 | ARG_WRITE(buf, int, height);
69 |
70 | create_framebuffer_result framebuffer = remote_call(CREATE_FRAMEBUFFER_RETURN_TYPE, VIDEO_DRIVER_SERVICE, CREATE_FRAMEBUFFER_FN, MAKE_ARGS(buffer, CREATE_FRAMEBUFFER_ARGS_SIZE));
71 |
72 | return framebuffer;
73 | }
74 |
75 | PUBLIC bool swap_framebuffer(int id) {
76 | char buffer[SWAP_FRAMEBUFFER_ARGS_SIZE];
77 | void *buf = buffer;
78 |
79 | ARG_WRITE(buf, bool, id);
80 |
81 | bool ret = remote_call(SWAP_FRAMEBUFFER_RETURN_TYPE, VIDEO_DRIVER_SERVICE, SWAP_FRAMEBUFFER_FN, MAKE_ARGS(buffer, SWAP_FRAMEBUFFER_ARGS_SIZE));
82 |
83 | return ret;
84 | }
--------------------------------------------------------------------------------
/libs/libkmod/export/video/__video_driver_fn.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * __video_driver_fn.h
12 | *
13 | * video_driver 函数功能
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | #define TEXT_WRITE_CHAR_FN (0)
24 | #define TEXT_WRITE_CHAR_ARGS_SIZE (sizeof(int) * 2 + sizeof(byte) * 2)
25 | #define TEXT_WRITE_CHAR_RETURN_TYPE bool
26 |
27 | #define TEXT_WRITE_STRING_FN (1)
28 | #define TEXT_WRITE_STRING_ARGS_SIZE (-1)
29 | #define TEXT_WRITE_STRING_ARGS_BASE_SIZE (sizeof(int) * 2 + sizeof(byte) + sizeof(int))
30 | #define TEXT_WRITE_STRING_RETURN_TYPE bool
31 |
32 | #define CREATE_FRAMEBUFFER_FN (2)
33 | #define CREATE_FRAMEBUFFER_ARGS_SIZE (sizeof(int) * 4)
34 | #define CREATE_FRAMEBUFFER_RETURN_TYPE create_framebuffer_result
35 |
36 | #define SWAP_FRAMEBUFFER_FN (3)
37 | #define SWAP_FRAMEBUFFER_ARGS_SIZE (sizeof(int))
38 | #define SWAP_FRAMEBUFFER_RETURN_TYPE bool
39 |
40 | typedef struct {
41 | void *framebuffer;
42 | int id;
43 | } create_framebuffer_result;
44 |
45 | PUBLIC bool write_char(int column, int line, byte color, byte ch);
46 | PUBLIC bool write_string(int column, int line, byte color, const char *str);
47 | PUBLIC create_framebuffer_result create_framebuffer(int column, int line, int width, int height);
48 | PUBLIC bool swap_framebuffer(int id);
--------------------------------------------------------------------------------
/libs/libkmod/init.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * init.c
12 | *
13 | * 初始化
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 | #include
21 | #include
22 | #include
23 |
24 | #include
25 |
26 | #include
27 |
28 | #include
29 |
30 | #include
31 | #include
32 |
33 | #define MAIL_SIZE (8 * 1024)
34 |
35 | PRIVATE void __message_loop(void *args) {
36 | //分配邮箱缓冲区
37 | void *mail_buffer = malloc(MAIL_SIZE);
38 | set_mailbuffer(mail_buffer, MAIL_SIZE);
39 |
40 | set_allow(ANY_TASK);
41 |
42 | //通知init该模块已初始化完成
43 | bool status = true;
44 | bool send_ret = send_msg((msgno_id){.message_no = MSG_NORMAL_IPC, .msg_id = get_msgid()}, &status, sizeof(bool), INIT_SERVICE);
45 | assert(send_ret);
46 |
47 | message_loop();
48 | exit_thread(NULL);
49 | }
50 |
51 | PUBLIC void __kmod_init__(void) {
52 | register int magic __asm__("rax");
53 | register void *heap_start __asm__("rbx");
54 | register void *heap_end __asm__("rcx");
55 | register int pid __asm__("rdx");
56 |
57 | if (magic != KMOD_MAGIC) {
58 | while (true);
59 | }
60 |
61 | //设置日志名
62 | set_logging_name("KMod Init");
63 | //初始化堆
64 | init_heap(pid, heap_start, heap_end);
65 |
66 | self_pid = pid;
67 |
68 | kmod_init();
69 |
70 | create_thread(__message_loop, NULL);
71 |
72 | kmod_main();
73 |
74 | id_t signal = create_signal(1, 0, false);
75 | down_signal(signal);
76 |
77 | while (true);
78 | //exit();
79 | }
80 |
--------------------------------------------------------------------------------
/libs/libkmod/libs/ctype.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2021, 2021 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * ctype.h
12 | *
13 | * 标准库ctype实现
14 | *
15 | */
16 |
17 | #include
18 |
19 | //利用提供好的宏实现
20 | //目前只支持ascii字符
21 |
22 | //空字符
23 | int isspace(int ch) {
24 | return __isspace(ch);
25 | }
26 |
27 | //大写字母
28 | int isupper(int ch) {
29 | return __isupper(ch);
30 | }
31 |
32 | //小写字母
33 | int islower(int ch) {
34 | return __islower(ch);
35 | }
36 |
37 | //字母
38 | int isalpha(int ch) {
39 | return __isalpha(ch);
40 | }
41 |
42 | //数字
43 | int isdigit(int ch) {
44 | return __isdigit(ch);
45 | }
46 |
47 | //字母/数字
48 | int isalnum(int ch) {
49 | return __isalnum(ch);
50 | }
51 |
52 | //空格
53 | int isblank(int ch) {
54 | return __isblank(ch);
55 | }
56 |
57 | //控制符
58 | int iscntrl(int ch) {
59 | return __iscntrl(ch);
60 | }
61 |
62 | //可输出字符
63 | int isprint(int ch) {
64 | return __isprint(ch);
65 | }
66 |
67 | //可见字符
68 | int isgraph(int ch) {
69 | return __isgraph(ch);
70 | }
71 |
72 | //符号
73 | int ispunct(int ch) {
74 | return __ispunct(ch);
75 | }
76 |
77 | //16进制位
78 | int isxdigit(int ch) {
79 | return __isxdigit(ch);
80 | }
81 |
82 | //8进制位
83 | int isodigit(int ch) {
84 | return __isodigit(ch);
85 | }
86 |
87 | //大写字母->小写字母
88 | int tolower(int ch) {
89 | return __tolower(ch);
90 | }
91 |
92 | //小写字母->大写字母
93 | int toupper(int ch) {
94 | return __toupper(ch);
95 | }
--------------------------------------------------------------------------------
/libs/libkmod/libs/string.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2021, 2021 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * string.c
12 | *
13 | * 标准库string实现
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 | #include
21 |
22 | int strcmp(const char *str1, const char *str2) {
23 | int len1 = strlen(str1);
24 | int len2 = strlen(str2);
25 | //长度不等
26 | if (len1 != len2) {
27 | if (len1 < len2) {
28 | return -1;
29 | }
30 | else {
31 | return 1;
32 | }
33 | }
34 | int res = 0;
35 | while (*str1 != '\0') {
36 | res = (*str1) - (*str2);
37 | //字符不等
38 | if (res != 0) {
39 | break;
40 | }
41 | str1 ++;
42 | str2 ++;
43 | }
44 |
45 | //前者小于后者
46 | if (res < 0) {
47 | return -1;
48 | }
49 | else if (res > 0) {
50 | return 1;
51 | }
52 | //二者相等
53 | return 0;
54 | }
55 |
56 | int strlen(const char *str) {
57 | int cnt = 0x7FFFFFFF;
58 | //使用字符串扫描指令
59 | __asm__ __volatile__ (
60 | "cld\n"
61 | "repnz\n"
62 | "scasb" :
63 | "+c" (cnt) : "D" (str), "a" (0));
64 | return 0x7FFFFFFF - cnt - 1;
65 | }
66 |
67 | char *strcpy(void *dst, const char *src) {
68 | char *r = dst;
69 | do {
70 | *(r ++) = *(src ++);
71 | } while (*src != '\0'); //遇到\0结束(复制后)
72 | return dst;
73 | }
--------------------------------------------------------------------------------
/libs/libkmod/memory/malloc.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * malloc.h
12 | *
13 | * 分配内存
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | PUBLIC bool init_heap(int pid, void *heap_start, void *heap_end);
24 | PUBLIC void *malloc(int size);
25 | PUBLIC void free(void *addr);
--------------------------------------------------------------------------------
/libs/libkmod/memory/sharemem.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * sharemem.h
12 | *
13 | * 共享内存
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 | #include
--------------------------------------------------------------------------------
/libs/libkmod/printf.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * printf.h
12 | *
13 | * printf
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | //获取/设置输出颜色
25 | PUBLIC int get_print_color(void);
26 | PUBLIC void set_print_color(int color);
27 | //设置tty
28 | PUBLIC int get_tty(void);
29 | PUBLIC void set_tty(int tty);
30 | PUBLIC void set_active_tty(int tty);
31 | //获取/改变输出位置
32 | PUBLIC void change_pos(int x, int y);
33 | PUBLIC int get_pos_x(void);
34 | PUBLIC int get_pos_y(void);
35 | //获取/改变滚动行位置
36 | PUBLIC int get_scroll_line(void);
37 | PUBLIC void set_scroll_line(int line);
38 | //滚动屏幕
39 | PUBLIC void scroll_screen(int lines);
40 | //刷新到屏幕上
41 | PUBLIC void flush_to_screen(void);
42 |
43 | //清屏
44 | PUBLIC void clrscr(void);
45 | //打印字符(串)
46 | PUBLIC void putchar(char ch);
47 | PUBLIC void puts(const char *str);
48 | //printf变体
49 | PUBLIC int vsprintf(char *buffer, const char *format, va_list args);
50 | PUBLIC int printf(const char *format, ...);
51 | PUBLIC int sprintf(char *buffer, const char *format, ...);
--------------------------------------------------------------------------------
/libs/libkmod/syscall/ipc.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * ipc.h
12 | *
13 | * IPC
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | PUBLIC bool send_msg(msgno_id msgno, void *msg, qword size, int dst);
25 | PUBLIC void check_ipc(void);
26 | PUBLIC void set_allow(int pid);
27 | PUBLIC msgpack_struct recv_msg(void *msg);
28 | PUBLIC void set_mailbuffer(void *buffer, int size);
29 |
30 | typedef void(* normal_ipc_handler_t)(int, void *);
31 | PUBLIC void register_normal_ipc_handler(normal_ipc_handler_t handler);
32 |
33 | typedef void(* irq_handler_t)(int);
34 | PUBLIC void register_irq_handler(irq_handler_t handler);
35 |
36 | PUBLIC void message_loop(void);
37 |
38 | EXTERN PUBLIC int self_pid;
39 | EXTERN PUBLIC word msgid_counter;
40 |
41 | static inline word get_msgid(void) {
42 | if (msgid_counter == 65535) {
43 | msgid_counter = 0;
44 | return 65535;
45 | }
46 | return msgid_counter ++;
47 | }
48 |
49 | PUBLIC void set_rpc_result(int msgid, id_t signal, void *data);
50 |
--------------------------------------------------------------------------------
/libs/libkmod/syscall/rpc.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * rpc.h
12 | *
13 | * 远程过程调用
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | typedef qword rpc_func;
25 | typedef dword rpc_size;
26 |
27 | typedef struct {
28 | qword data : 44;
29 | int size : 20;
30 | } rpc_args_struct;
31 |
32 | typedef rpc_args_struct(* rpc_proccess_wrapper)(int, rpc_func, rpc_args_struct);
33 |
34 | #define ARG_READ(args, type) *(type *)(((args) = (((void *)(args)) + sizeof(type))) - sizeof(type))
35 | #define ARG_WRITE(args, type, value) *(type *)(((args) = (((void *)(args)) + sizeof(type))) - sizeof(type)) = value
36 |
37 | #define MAKE_ARGS(args, sz) ((rpc_args_struct){.data = (args), .size = (sz)})
38 |
39 | PUBLIC void deal_rpc_request(msgpack_struct pack, void *msg);
40 | PUBLIC void rpc_register(rpc_func func, rpc_proccess_wrapper process, rpc_size return_size, rpc_size args_size); //当args_size = -1时, 不对参数大小进行检验
41 | PUBLIC rpc_args_struct __rpc_call(int service, rpc_func func, rpc_args_struct args, rpc_size return_size, void *result);
42 | PUBLIC void *rpc_call(int service, rpc_func func, rpc_args_struct args, rpc_size return_size);
43 |
44 | #define remote_call(ret_type, service, func, args) (*(ret_type *)rpc_call(service, func, args, sizeof(ret_type)))
--------------------------------------------------------------------------------
/libs/libkmod/syscall/syscall.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * syscall.h
12 | *
13 | * 系统调用
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | PUBLIC void moo(void);
24 | PUBLIC void reg_irq(int irq);
25 | PUBLIC void exit(int ret);
26 | PUBLIC bool test_and_lock(bool *val);
27 |
28 | PUBLIC id_t create_signal(int max_signals, int value, bool soft);
29 | PUBLIC int get_signals(id_t id);
30 | PUBLIC void up_signal(id_t id);
31 | PUBLIC void down_signal(id_t id);
32 | PUBLIC bool is_soft_signal(id_t id);
33 |
34 | typedef void(*thread_function)(void *);
35 | PUBLIC int create_thread(thread_function entry, void *args);
36 | PUBLIC void exit_thread(void *retval);
37 |
--------------------------------------------------------------------------------
/loader/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/loader/
20 | BINDIR := $(BINDIR)/loader/
21 | LOADERDIR := $(ROOTDIR)/loader/
22 | export OBJECTSDIR LOADERDIR
23 |
24 | #编译
25 | .PHONY: build
26 | build:
27 | $(CD) grub_loader && $(MAKE) build
28 |
29 | #清理
30 | .PHONY: clean
31 | clean:
32 | $(CD) grub_loader && $(MAKE) clean
33 |
34 | .PHONY: image
35 | image:
36 | $(CD) grub_loader && $(MAKE) image
37 |
--------------------------------------------------------------------------------
/loader/grub_loader/Makefile:
--------------------------------------------------------------------------------
1 | OBJECTSDIR := $(OBJECTSDIR)/grubld/
2 |
3 | GRUB_LOADER_OUTPUT := $(BINDIR)/grubld.bin
4 |
5 | CFLAGS = -m32 -mregparm=1 -Wall -Wno-int-conversion -Wstrict-prototypes -march=i386 -mfpmath=387\
6 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
7 | -mno-mmx -mno-sse -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone\
8 | -mpreferred-stack-boundary=2 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
9 |
10 | LIBS_C_SRC := ctype string tostring
11 | FS_C_SRC := common fat32
12 | LM_C_SRC := load_kernel setup_lm
13 | C_SRC := main printf init int_handlers lheap disk info_parser logging show_icon \
14 | $(foreach src, $(LIBS_C_SRC), libs/$(src)) \
15 | $(foreach src, $(FS_C_SRC), fs/$(src))\
16 | $(foreach src, $(LM_C_SRC), lm/$(src))
17 | ASM_SRC := lm/lm_operators _int_handlers
18 | C_DEFS := $(ARCHDEF_C) -DLOADER32BIT
19 |
20 | ifeq ($(VBE_MODE), ENABLE)
21 | C_DEFS += -DVBE_ENABLE
22 | endif
23 |
24 | C_INCLUDE := -I"$(ROOTDIR)/include/std/" -I"$(ROOTDIR)/include" -I"$(LOADERDIR)/grub_loader"
25 |
26 | OBJECTS := $(foreach obj, $(ASM_SRC), $(OBJECTSDIR)/$(obj).o) \
27 | $(foreach obj, $(C_SRC), $(OBJECTSDIR)/$(obj).o)
28 |
29 | LD_FLAGS := -s -z max-page-size=0x100
30 | LIBRARIES :=
31 |
32 | $(OBJECTSDIR)/%.o : %.c
33 | $(MKDIR) $(dir $@)
34 | $(GCC) -c $(CFLAGS) $(C_INCLUDE) $(C_DEFS) -o $@ $^
35 |
36 | $(OBJECTSDIR)/%.o : %.asm
37 | $(MKDIR) $(dir $@)
38 | $(ASM) $^ -f elf -o $@
39 |
40 | .PHONY: build
41 | build: $(OBJECTS)
42 | $(MKDIR) $(dir $(GRUB_LOADER_OUTPUT))
43 | $(LD) $(LD_FLAGS) -T loader.ld -o $(GRUB_LOADER_OUTPUT) $(OBJECTS)
44 |
45 | .PHONY: clean
46 | clean:
47 | $(RM) $(GRUB_LOADER_OUTPUT) $(OBJECTS)
48 |
49 | .PHONY: image
50 | image:
51 | $(SUDO) $(COPY) $(GRUB_LOADER_OUTPUT) $(TAYHUANGOS_MOUNT_DIR)
--------------------------------------------------------------------------------
/loader/grub_loader/README.md:
--------------------------------------------------------------------------------
1 | # GRUB Loader
2 |
3 | 由GRUB引导 任务是加载内核文件,setup.mod以及进入长模式
--------------------------------------------------------------------------------
/loader/grub_loader/disk.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * disk.h
12 | *
13 | * 硬盘操作函数
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | #define DISK_SEL_IDE0 (0)
25 | #define DISK_SEL_IDE1 (1)
26 | #define DISK_SEL_MASTER (0)
27 | #define DISK_SEL_SLAVE (2)
28 | #define DISK_SEL_IDE0_MASTER (DISK_SEL_IDE0 | DISK_SEL_MASTER)
29 | #define DISK_SEL_IDE0_SLAVE (DISK_SEL_IDE0 | DISK_SEL_SLAVE)
30 | #define DISK_SEL_IDE1_MASTER (DISK_SEL_IDE1 | DISK_SEL_MASTER)
31 | #define DISK_SEL_IDE1_SLAVE (DISK_SEL_IDE1 | DISK_SEL_SLAVE)
32 |
33 | PUBLIC void identify_disk(int selector, void *dst); //获取硬盘参数
34 | PUBLIC void read_sector(dword lba, int num, int selector, void *dst); //读扇区
35 | PUBLIC void get_partition(int selector, int number, partition_info *member);
--------------------------------------------------------------------------------
/loader/grub_loader/fs/common.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * common.c
12 | *
13 | * 通用FS接口函数
14 | *
15 | */
16 |
17 |
18 | #include
19 | #include
20 |
21 | #include
22 | #include
23 |
24 | #include
25 |
26 | #include
27 |
28 | #include
29 | #include
30 |
31 |
32 | typedef enum {
33 | FS_UNKNOWN = 0,
34 | FS_FAT32,
35 | FS_NTFS,
36 | FS_EXT2
37 | } FS_TYPES;
38 |
39 | PRIVATE FS_TYPES get_fs_type(int disk_selector, int partition_id) {
40 | partition_info partition;
41 | get_partition(disk_selector, partition_id, &partition); //获取分区
42 |
43 | void *superblock = lmalloc(512);
44 | read_sector(partition.start_lba, 1, disk_selector, superblock); //读取超级块
45 |
46 | char bpb_filesystem[9];
47 |
48 | memcpy(bpb_filesystem, superblock + 0x36, 8); //
49 | bpb_filesystem[8] = '\0';
50 |
51 | lfree (superblock);
52 |
53 | if (strcmp(bpb_filesystem, "fat32")) { //是fat32
54 | return FS_FAT32;
55 | }
56 |
57 | return FS_UNKNOWN;
58 | }
59 |
60 | PUBLIC fs_context load_fs(int disk_selector, int partition_id){
61 | FS_TYPES type = get_fs_type(disk_selector, partition_id); //获取类型
62 |
63 | if (type == FS_FAT32) {
64 | return load_fat32_fs(disk_selector, partition_id);
65 | }
66 | else {
67 | return NULL;
68 | }
69 | }
70 |
71 | PUBLIC void display_fs_info(fs_context context) {
72 | if (*((dword *)context) == FAT32_CONTEXT_MAGIC) { //比较context魔数
73 | display_fat32_fs_info(context);
74 | }
75 | }
76 |
77 | PUBLIC bool load_file(fs_context context, const char *name, void *dst) {
78 | if (*((dword *)context) == FAT32_CONTEXT_MAGIC) { //比较context魔数
79 | return load_fat32_file(context, name, dst);
80 | }
81 | return false;
82 | }
83 |
84 | PUBLIC void terminate_fs(fs_context context) {
85 | if (*((dword *)context) == FAT32_CONTEXT_MAGIC) { //比较context魔数
86 | terminate_fat32_fs(context);
87 | }
88 | }
--------------------------------------------------------------------------------
/loader/grub_loader/fs/common.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * common.h
12 | *
13 | * 通用FS接口函数
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //文件系统上下文
24 | typedef void *fs_context;
25 |
26 | //加载文件系统上下文
27 | PUBLIC fs_context load_fs(int disk_selector, int partition_id);
28 | //读取文件
29 | PUBLIC bool load_file(fs_context context, const char *name, void *dst);
30 | //释放文件系统上下文
31 | PUBLIC void terminate_fs(fs_context context);
32 | //显示文件系统信息
33 | PUBLIC void display_fs_info(fs_context context);
--------------------------------------------------------------------------------
/loader/grub_loader/fs/fat32.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * fat32.h
12 | *
13 | * FAT32文件系统
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | #define FAT32_CONTEXT_MAGIC (0x93186A8E)
25 | PUBLIC fs_context load_fat32_fs(int disk_selector, int partition_id);
26 | PUBLIC bool load_fat32_file(fs_context context, const char *name, void *dst);
27 | PUBLIC void terminate_fat32_fs(fs_context context);
28 | PUBLIC void display_fat32_fs_info(fs_context context);
--------------------------------------------------------------------------------
/loader/grub_loader/info_parser.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * info_parser.c
12 | *
13 | * 解析multiboot2参数
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 |
23 | PRIVATE void parse_mem_info(struct multiboot_tag_mmap *tag, qword *memsz) {
24 | int size = tag->size - 4 * 4;
25 | int entry_num = size / tag->entry_size;
26 |
27 | *memsz = 0;
28 |
29 | for (int i = 0 ; i < entry_num ; i ++) {
30 | if (tag->entries[i].type == MULTIBOOT_MEMORY_AVAILABLE) {
31 | *memsz = max(*memsz, (dword)tag->entries[i].addr + (dword)tag->entries[i].len);
32 | }
33 | }
34 |
35 | *memsz += 0x20000; //最终结果会少128KB
36 | }
37 |
38 | PRIVATE void parse_vbe(struct multiboot_tag_vbe *tag) {
39 | }
40 |
41 | PRIVATE void parse_framebuffer(struct multiboot_tag_framebuffer *tag, void **framebuffer, int *width, int *height) {
42 | *framebuffer = (void *)(tag->common.framebuffer_addr & 0xFFFFFFFF);
43 | *width = tag->common.framebuffer_width;
44 | *height = tag->common.framebuffer_height;
45 | }
46 |
47 | PUBLIC void parse_args(struct multiboot_tag *tag, parse_result_struct *result) {
48 | tag = ((void *)tag) + 8;
49 | while (tag->type != MULTIBOOT_TAG_TYPE_END) {
50 | switch (tag->type)
51 | {
52 | case MULTIBOOT_TAG_TYPE_MMAP: {
53 | parse_mem_info((struct multiboot_tag_mmap*)tag, &result->memsz);
54 | break;
55 | }
56 | case MULTIBOOT_TAG_TYPE_VBE: {
57 | parse_vbe((struct multiboot_tag_vbe*)tag);
58 | break;
59 | }
60 | case MULTIBOOT_TAG_TYPE_FRAMEBUFFER: {
61 | parse_framebuffer((struct multiboot_tag_framebuffer*)tag, &result->framebuffer, &result->screen_width, &result->screen_height);
62 | break;
63 | }
64 | default: {
65 | break;
66 | }
67 | }
68 |
69 | int tag_size = (tag->size + 7) & ~7;
70 | tag = (struct multiboot_tag*)(((void *)tag) + tag_size);
71 | }
72 |
73 | #ifndef VBE_ENABLE
74 | result->is_graphic = false;
75 | result->screen_width = 80;
76 | result->screen_height = 25;
77 | result->framebuffer = (void *)0xB8000;
78 | #else
79 | result->is_graphic = true;
80 | #endif
81 | }
--------------------------------------------------------------------------------
/loader/grub_loader/info_parser.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * info_parser.h
12 | *
13 | * 解析multiboot2参数
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | typedef struct {
25 | qword memsz;
26 | bool is_graphic;
27 | int screen_width;
28 | int screen_height;
29 | void *framebuffer;
30 | } parse_result_struct;
31 |
32 | PUBLIC void parse_args(struct multiboot_tag *tag, parse_result_struct *result);
--------------------------------------------------------------------------------
/loader/grub_loader/init.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * init.h
12 | *
13 | * 初始化函数
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | typedef void(* int_handler)(void);
24 |
25 | #define GDT_SIZE (16)
26 | #define IDT_SIZE (128)
27 |
28 | //GDT IDT表
29 | EXTERN PUBLIC struct desc_struct GDT[GDT_SIZE];
30 | EXTERN PUBLIC struct gdt_ptr gdtr;
31 | EXTERN PUBLIC gate_desc IDT[IDT_SIZE];
32 | EXTERN PUBLIC struct desc_ptr idtr;
33 |
34 | //初始化函数
35 | PUBLIC void init_gdt(void);
36 | PUBLIC void init_pic(void);
37 | PUBLIC void init_idt_desc(byte vector, byte type, int_handler handler, byte privilege);
38 | PUBLIC void init_idt(void);
--------------------------------------------------------------------------------
/loader/grub_loader/int_handlers.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * int_handlers.h
12 | *
13 | * 中断处理函数
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //异常处理器
24 | PUBLIC void exception_handler(int vector_no, int errcode, int eip, int cs, int eflags);
25 | //异常
26 | PUBLIC void divide_by_zero_error(void);
27 | PUBLIC void single_step_debug(void);
28 | PUBLIC void non_maskable_interrup(void);
29 | PUBLIC void breakpoint(void);
30 | PUBLIC void overflow(void);
31 | PUBLIC void bound_range_exceeded(void);
32 | PUBLIC void invalid_opcode(void);
33 | PUBLIC void device_not_available(void);
34 | PUBLIC void double_fault(void);
35 | PUBLIC void coprocessor_segment_overrun(void);
36 | PUBLIC void invalid_tss(void);
37 | PUBLIC void segment_not_present(void);
38 | PUBLIC void stack_segment_fault(void);
39 | PUBLIC void general_protection_fault(void);
40 | PUBLIC void page_fault(void);
41 | PUBLIC void reserved1_excepetion(void);
42 | PUBLIC void x87_floating_point_exception(void);
43 | PUBLIC void alignment_check(void);
44 | PUBLIC void machine_check(void);
45 | PUBLIC void simd_floating_point_exception(void);
46 | PUBLIC void virtualization_exception(void);
47 | PUBLIC void control_protection_exception(void);
48 | PUBLIC void reserved2_excepetion(void);
49 | PUBLIC void reserved3_excepetion(void);
50 | PUBLIC void reserved4_excepetion(void);
51 | PUBLIC void reserved5_excepetion(void);
52 | PUBLIC void reserved6_excepetion(void);
53 | PUBLIC void reserved7_excepetion(void);
54 | PUBLIC void hypervisor_injection_exception(void);
55 | PUBLIC void vmm_communication_exception(void);
56 | PUBLIC void security_exception(void);
57 | PUBLIC void reserved8_excepetion(void);
58 | //IRQ处理器
59 | PUBLIC void irq_interrup_handler(int irq);
60 | //IRQ中断
61 | PUBLIC void irq0_handler(void);
62 | PUBLIC void irq1_handler(void);
63 | PUBLIC void irq2_handler(void);
64 | PUBLIC void irq3_handler(void);
65 | PUBLIC void irq4_handler(void);
66 | PUBLIC void irq5_handler(void);
67 | PUBLIC void irq6_handler(void);
68 | PUBLIC void irq7_handler(void);
69 | PUBLIC void irq8_handler(void);
70 | PUBLIC void irq9_handler(void);
71 | PUBLIC void irq10_handler(void);
72 | PUBLIC void irq11_handler(void);
73 | PUBLIC void irq12_handler(void);
74 | PUBLIC void irq13_handler(void);
75 | PUBLIC void irq14_handler(void);
76 | PUBLIC void irq15_handler(void);
77 |
78 | typedef void(* irq_handler)(int);
79 | //注册IRQ中断处理器
80 | PUBLIC void register_irq_handler(int irq, irq_handler handler);
81 | PUBLIC void enable_irq(int irq); //启用IRQ
82 | PUBLIC void disable_irq(int irq); //禁用IRQ
--------------------------------------------------------------------------------
/loader/grub_loader/lheap.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * lheap.h
12 | *
13 | * Loader堆
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | #define LHEAP_BASE (0x2200000)
24 |
25 | PUBLIC void init_lheap(void *lheap_size); //初始化堆
26 | PUBLIC void *lmalloc(int size); //分配内存
27 | static inline void *lcalloc(int num, int size) { //分配num个size的内存
28 | return lmalloc(num * size);
29 | }
30 | PUBLIC void lfree(void *ptr); //释放内存
--------------------------------------------------------------------------------
/loader/grub_loader/libs/ctype.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2021, 2021 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * ctype.h
12 | *
13 | * 标准库ctype实现
14 | *
15 | */
16 |
17 | #include
18 |
19 | int isspace(int ch) {
20 | return __isspace(ch);
21 | }
22 |
23 | int isupper(int ch) {
24 | return __isupper(ch);
25 | }
26 |
27 | int islower(int ch) {
28 | return __islower(ch);
29 | }
30 |
31 | int isalpha(int ch) {
32 | return __isalpha(ch);
33 | }
34 |
35 | int isdigit(int ch) {
36 | return __isdigit(ch);
37 | }
38 |
39 | int isalnum(int ch) {
40 | return __isalnum(ch);
41 | }
42 |
43 | int isblank(int ch) {
44 | return __isblank(ch);
45 | }
46 |
47 | int iscntrl(int ch) {
48 | return __iscntrl(ch);
49 | }
50 |
51 | int isprint(int ch) {
52 | return __isprint(ch);
53 | }
54 |
55 | int isgraph(int ch) {
56 | return __isgraph(ch);
57 | }
58 |
59 | int ispunct(int ch) {
60 | return __ispunct(ch);
61 | }
62 |
63 | int isxdigit(int ch) {
64 | return __isxdigit(ch);
65 | }
66 |
67 | int isodigit(int ch) {
68 | return __isodigit(ch);
69 | }
70 |
71 | int tolower(int ch) {
72 | return __tolower(ch);
73 | }
74 |
75 | int toupper(int ch) {
76 | return __toupper(ch);
77 | }
--------------------------------------------------------------------------------
/loader/grub_loader/libs/string.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2021, 2021 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * string.c
12 | *
13 | * 标准库string实现
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 | #include
21 |
22 | int strcmp(const char *str1, const char *str2) {
23 | int len1 = strlen(str1);
24 | int len2 = strlen(str2);
25 | if (len1 != len2) {
26 | if (len1 < len2) {
27 | return -1;
28 | }
29 | else {
30 | return 1;
31 | }
32 | }
33 | int res = 0;
34 | while (*str1) {
35 | res = (*str1) - (*str2);
36 | if (res != 0) {
37 | break;
38 | }
39 | str1 ++;
40 | str2 ++;
41 | }
42 |
43 | if (res < 0) {
44 | return -1;
45 | }
46 | else if (res > 0) {
47 | return 1;
48 | }
49 | return 0;
50 | }
51 |
52 | int strlen(const char *str) {
53 | int cnt = 0x7FFFFFFF;
54 | __asm__ __volatile__ (
55 | "cld\n"
56 | "repnz\n"
57 | "scasb" :
58 | "+c" (cnt) : "D" (str), "a" (0));
59 | return 0x7FFFFFFF - cnt - 1;
60 | }
61 |
62 | char *strcpy(void *dst, const char *src) {
63 | char *r = dst;
64 | while(*src != '\0') {
65 | *(r ++) = *(src ++);
66 | }
67 | *r = '\0';
68 | return dst;
69 | }
--------------------------------------------------------------------------------
/loader/grub_loader/libs/tostring.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * tostring.c
12 | *
13 | * xx转字符串
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | static const char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
22 |
23 | char *itoa(int val, char *buffer, int base) {
24 | char *save = buffer;
25 | if (val == 0) {
26 | *buffer = '0';
27 | buffer ++;
28 | *buffer = '\0';
29 | return save;
30 | }
31 | if (val < 0) {
32 | *buffer = '-';
33 | buffer ++;
34 | val = -val;
35 | }
36 | char _buffer[12];
37 | int cnt = 0;
38 | while (val > 0) {
39 | _buffer[cnt] = digits[val % base];
40 | cnt ++;
41 | val /= base;
42 | }
43 | for (int i = cnt - 1; i >= 0 ; i --) {
44 | *buffer = _buffer[i];
45 | buffer ++;
46 | }
47 | return save;
48 | }
49 |
50 | char *uitoa(unsigned int val, char *buffer, int base) {
51 | char *save = buffer;
52 | if (val == 0) {
53 | *buffer = '0';
54 | buffer ++;
55 | *buffer = '\0';
56 | return save;
57 | }
58 | char _buffer[12];
59 | int cnt = 0;
60 | while (val > 0) {
61 | _buffer[cnt] = digits[val % base];
62 | cnt ++;
63 | val /= base;
64 | }
65 | for (int i = cnt - 1; i >= 0 ; i --) {
66 | *buffer = _buffer[i];
67 | buffer ++;
68 | }
69 | return save;
70 | }
--------------------------------------------------------------------------------
/loader/grub_loader/lm/lm_operators.asm:
--------------------------------------------------------------------------------
1 | ;
2 | ; SPDX-License-Identifier: LGPL-2.1-only
3 | ; -------------------------------*-TayhuangOS-*-----------------------------------
4 | ;
5 | ; Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | ;
7 | ; --------------------------------------------------------------------------------
8 | ;
9 | ; 作者: theflysong
10 | ;
11 | ; arch/x86_64/tayhuang/stage3/lm/lm_operators.asm
12 | ;
13 | ; 长模式相关汇编操作
14 | ;
15 |
16 |
17 | global __set_cr0
18 | global __get_cr0
19 | global __set_cr2
20 | global __get_cr2
21 | global __set_cr3
22 | global __get_cr3
23 | global __set_cr4
24 | global __get_cr4
25 | global __set_efer
26 | global __get_efer
27 |
28 | __set_cr0:
29 | mov cr0, eax
30 | ret
31 |
32 | __get_cr0:
33 | mov eax, cr0
34 | ret
35 |
36 | __set_cr2:
37 | mov cr2, eax
38 | ret
39 |
40 | __get_cr2:
41 | mov eax, cr2
42 | ret
43 |
44 | __set_cr3:
45 | mov cr3, eax
46 | ret
47 |
48 | __get_cr3:
49 | mov eax, cr3
50 | ret
51 |
52 | __set_cr4:
53 | mov cr4, eax
54 | ret
55 |
56 | __get_cr4:
57 | mov eax, cr4
58 | ret
59 |
60 | __get_efer:
61 | push edx
62 | push ecx
63 | xor edx, edx
64 | mov ecx, 0xC0000080 ;EFER
65 | rdmsr
66 | pop ecx
67 | pop edx
68 | ret
69 |
70 | __set_efer:
71 | push edx
72 | push ecx
73 | xor edx, edx
74 | mov ecx, 0xC0000080 ;EFER
75 | wrmsr
76 | pop ecx
77 | pop edx
78 | ret
79 |
80 | global goto_ia32e
81 | goto_ia32e:
82 | mov edi, dword [esp + 4] ;edx -> boot_args
83 | mov bx, word [esp + 8] ;bx -> selector64
84 | mov word [.jmp_seg], bx
85 | mov dword [.jmp_pos], eax
86 | add bx, 8
87 | mov ds, bx
88 |
89 | mov esp, eax
90 | sub esp, 0x20 ;设置esp
91 | mov ebp, esp
92 |
93 | xor eax, eax
94 | xor ebx, ebx
95 | xor ecx, ecx
96 | xor edx, edx
97 | xor esi, esi
98 |
99 | db 0xEA
100 | .jmp_pos: dd 0
101 | .jmp_seg: dw 0
102 |
103 | %define CS_SELECTOR (1 << 3)
104 |
105 | global flush_cs
106 | flush_cs:
107 | jmp CS_SELECTOR:flush_end ;利用长跳刷新流水线并更新CS
108 |
109 | flush_end:
110 | ret
111 |
--------------------------------------------------------------------------------
/loader/grub_loader/lm/lm_operators.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * setup_longmode.h
12 | *
13 | * 设置长模式
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | PUBLIC void goto_ia32e(void *entrypoint, void *args, word selector64);
24 | PUBLIC void flush_cs(void);
--------------------------------------------------------------------------------
/loader/grub_loader/lm/load_kernel.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * load_kernel.h
12 | *
13 | * 加载内核
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | #define KERNEL_BIN_SIZE (256 * 1024)
24 | #define SETUP_MOD_SIZE (256 * 1024)
25 |
26 | typedef struct {
27 | bool status;
28 | void *kernel_start;
29 | void *kernel_limit;
30 | void *kernel_entry;
31 | void *setup_mod;
32 | } load_result_struct;
33 |
34 | PUBLIC void load_kernel(load_result_struct *result);
--------------------------------------------------------------------------------
/loader/grub_loader/lm/setup_lm.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * setup_lm.h
12 | *
13 | * 设置长模式
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | #define PAGE_ADDRESS (0x3000000)
24 | PUBLIC void setup_longmode(void *pml4);
25 | PUBLIC void *setup_paging(qword memsz, void ** limit);
26 |
27 | PUBLIC void goto_longmode(word selector64, qword memsz, bool is_graphic, int screen_width, int screen_height, void *framebuffer);
--------------------------------------------------------------------------------
/loader/grub_loader/loader.ld:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * loader.ld
12 | *
13 | * 加载器LD脚本
14 | *
15 | */
16 |
17 |
18 |
19 | ENTRY(entry)
20 | OUTPUT_FORMAT("elf32-i386")
21 | OUTPUT_ARCH("i386")
22 |
23 | SECTIONS
24 | {
25 | . = 0x2008000;
26 | .text : {
27 | . = ALIGN(8);
28 | KEEP(*(.multiboot));
29 | *(.text)
30 | }
31 | .data : { *(.data) }
32 | .bss : { *(.bss) }
33 | }
34 |
--------------------------------------------------------------------------------
/loader/grub_loader/logging.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * logging.c
12 | *
13 | * 日志
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 | #include
23 |
24 | PUBLIC void init_serial(void) {
25 | outb(SERIAL_INT_VALID, 0); //禁用COM中断
26 | outb(SERIAL_CONTROL, 0x80); //启用DLAB
27 | outb(SERIAL_SEND, 0x03); //设置比特波除数(低)
28 | outb(SERIAL_INT_VALID, 0x00); //设置比特波除数(高)
29 | outb(SERIAL_CONTROL, 0x03); //无奇偶性 1停止位
30 | outb(SERIAL_INT_ID, 0xC7); //FIFO(size = 14)
31 | outb(SERIAL_MODEM_CONTROL, 0x0B); //
32 | outb(SERIAL_MODEM_CONTROL, 0x1E); //
33 | outb(SERIAL_SEND, 0xAE);
34 | outb(SERIAL_MODEM_CONTROL, 0x0F);
35 | }
36 |
37 | PUBLIC void write_serial_char(char ch) {
38 | while ((inb(SERIAL_STATUS) & 0x20) == 0);
39 | outb(SERIAL_SEND, ch);
40 | }
41 |
42 | PUBLIC void write_serial_str(const char *str) {
43 | while (*str != '\0') {
44 | write_serial_char(*str);
45 | str ++;
46 | }
47 | }
48 |
49 | PUBLIC void _log(const char *name, const char *type, const char *msg) {
50 | asmv ("cli");
51 | write_serial_char('(');
52 | write_serial_str(name);
53 | write_serial_char(')');
54 | write_serial_char('[');
55 | write_serial_str(type);
56 | write_serial_char(']');
57 | write_serial_str(msg);
58 | write_serial_char('\n');
59 | asmv ("sti");
60 | }
61 |
62 | PUBLIC void log(const char *name, const int type, const char *msg) {
63 | const char *typename;
64 | switch (type) {
65 | case INFO: typename = "INFO"; break;
66 | case WARN: typename = "WARN"; break;
67 | case ERROR: typename = "ERROR"; break;
68 | case FATAL: typename = "FATAL"; break;
69 | case TIPS: typename = "TIPS"; break;
70 | case ATTENTION: typename = "ATTENTION"; break;
71 | default: typename = "UNKNOWN"; break;
72 | }
73 | _log(name, typename, msg);
74 | }
75 |
76 | PUBLIC void linfo(const char *name, const char *msg) {
77 | log(name, INFO, msg);
78 | }
79 |
80 | PUBLIC void lwarn(const char *name, const char *msg) {
81 | log(name, WARN, msg);
82 | }
83 |
84 | PUBLIC void lerror(const char *name, const char *msg) {
85 | log(name, ERROR, msg);
86 | }
87 |
88 | PUBLIC void lfatal(const char *name, const char *msg) {
89 | log(name, FATAL, msg);
90 | }
91 |
92 | PUBLIC void ltips(const char *name, const char *msg) {
93 | log(name, TIPS, msg);
94 | }
95 |
96 | PUBLIC void lattention(const char *name, const char *msg) {
97 | log(name, ATTENTION, msg);
98 | }
--------------------------------------------------------------------------------
/loader/grub_loader/logging.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * logging.h
12 | *
13 | * 日志
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | PUBLIC void init_serial(void);
24 | PUBLIC void write_serial_char(char ch);
25 | PUBLIC void write_serial_str(const char *str);
26 |
27 | enum {
28 | INFO = 0,
29 | WARN,
30 | ERROR,
31 | FATAL,
32 | TIPS,
33 | ATTENTION
34 | };
35 |
36 | PUBLIC void _log(const char *name, const char *type, const char *msg);
37 | PUBLIC void log(const char *name, const int type, const char *msg);
38 | PUBLIC void linfo(const char *name, const char *msg);
39 | PUBLIC void lwarn(const char *name, const char *msg);
40 | PUBLIC void lerror(const char *name, const char *msg);
41 | PUBLIC void lfatal(const char *name, const char *msg);
42 | PUBLIC void ltips(const char *name, const char *msg);
43 | PUBLIC void lattention(const char *name, const char *msg);
--------------------------------------------------------------------------------
/loader/grub_loader/printf.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * printf.h
12 | *
13 | * printf
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | #include
24 |
25 | #define VIDEO_ADDRESS (0xB8000) //显存地址
26 | #define SCREEN_WIDTH (80)
27 | #define SCREEN_HEIGHT (25)
28 |
29 | //写生字符(串)
30 | PUBLIC void write_char(char ch, int color, int posx, int posy);
31 | PUBLIC void write_str(const char *str, int color, int posx, int posy);
32 |
33 | PUBLIC void change_pos(int x, int y);
34 | PUBLIC int get_pos_x(void);
35 | PUBLIC int get_pos_y(void);
36 | PUBLIC void putchar(char ch);
37 | PUBLIC void puts(const char *str);
38 |
39 | PUBLIC void clrscr(void);
40 | PUBLIC int get_print_color(void);
41 | PUBLIC void set_print_color(int color);
42 |
43 | PUBLIC int vsprintf(char *buffer, const char *format, va_list args);
44 | PUBLIC int sprintf(char *buffer, const char *format, ...);
45 | PUBLIC int printf(const char *format, ...);
--------------------------------------------------------------------------------
/loader/grub_loader/show_icon.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * show_icon.h
12 | *
13 | * 显示图标
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | PUBLIC void show_icon(void *framebuffer, int width, int height);
--------------------------------------------------------------------------------
/module/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/module/
20 | BINDIR := $(BINDIR)/module/
21 | MODULEDIR := $(ROOTDIR)/module/
22 | export OBJECTSDIR MODULEDIR
23 |
24 | #编译
25 | .PHONY: build
26 | build:
27 | $(CD) driver && $(MAKE) build
28 | $(CD) setup && $(MAKE) build
29 | $(CD) testbench && $(MAKE) build
30 |
31 | #清理
32 | .PHONY: clean
33 | clean:
34 | -$(CD) driver && $(MAKE) clean
35 | -$(CD) setup && $(MAKE) clean
36 | -$(CD) testbench && $(MAKE) clean
37 |
38 | .PHONY: image
39 | image:
40 | $(CD) driver && $(MAKE) image
41 | $(CD) setup && $(MAKE) image
42 | $(CD) testbench && $(MAKE) image
43 |
--------------------------------------------------------------------------------
/module/driver/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/driver/
20 | BINDIR := $(BINDIR)/driver/
21 | DRIVERDIR := $(MODULEDIR)/driver/
22 | export OBJECTSDIR BINDIR DRIVERDIR
23 |
24 | #编译
25 | .PHONY: build
26 | build:
27 | $(CD) video && $(MAKE) build
28 | $(CD) keyboard && $(MAKE) build
29 | $(CD) vdd && $(MAKE) build
30 |
31 | #清理
32 | .PHONY: clean
33 | clean:
34 | $(CD) video && $(MAKE) clean
35 | $(CD) keyboard && $(MAKE) clean
36 | $(CD) vdd && $(MAKE) clean
37 |
38 | .PHONY: image
39 | image:
40 | $(CD) video && $(MAKE) image
41 | $(CD) keyboard && $(MAKE) image
42 | $(CD) vdd && $(MAKE) image
--------------------------------------------------------------------------------
/module/driver/keyboard/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 |
20 | #编译
21 | .PHONY: build
22 | build:
23 | $(CD) ioman && $(MAKE) build
24 | $(CD) driver && $(MAKE) build
25 |
26 | #清理
27 | .PHONY: clean
28 | clean:
29 | $(CD) ioman && $(MAKE) clean
30 | $(CD) driver && $(MAKE) clean
31 |
32 | .PHONY: image
33 | image:
34 | $(CD) ioman && $(MAKE) image
35 | $(CD) driver && $(MAKE) image
--------------------------------------------------------------------------------
/module/driver/keyboard/driver/import/__ioman.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * __ioman.c
12 | *
13 | * IO Manager Functions
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 | #include
21 | #include
22 |
23 | PUBLIC void *share_keybuffer(bool flag) {
24 | char buffer[SHARE_KEYBUFFER_ARGS_SIZE];
25 | void *buf = buffer;
26 |
27 | ARG_WRITE(buf, bool, flag);
28 |
29 | void *fifo = remote_call(SHARE_KEYBUFFER_RETURN_TYPE, KEYBOARD_IOMAN_SERVICE, SHARE_KEYBUFFER_FN, ((rpc_args_struct){.data = buf, .size=SHARE_KEYBUFFER_ARGS_SIZE}));
30 |
31 | return fifo;
32 | }
--------------------------------------------------------------------------------
/module/driver/keyboard/driver/keyboard.ld:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * keyboard.ld
12 | *
13 | * keyboard LD脚本
14 | *
15 | */
16 |
17 |
18 |
19 | ENTRY(_start)
20 | OUTPUT_FORMAT("elf64-x86-64")
21 | OUTPUT_ARCH("i386:x86-64")
22 |
23 | SECTIONS
24 | {
25 | . = 0x4000000;
26 | .text : { *(.text) }
27 | .data : { *(.data) }
28 | .bss : { *(.bss) }
29 | }
--------------------------------------------------------------------------------
/module/driver/keyboard/driver/main.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * main.c
12 | *
13 | * 主函数
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 | #include
23 |
24 | #include
25 |
26 | #include
27 |
28 | #include
29 |
30 | #include
31 | #include
32 |
33 | PRIVATE void *fifo = NULL;
34 | #define LOCAL_FIFO_SIZE (8192)
35 | PRIVATE void *local_fifo = NULL;
36 |
37 | PRIVATE key_t __getkey(void) {
38 | key_t key;
39 | fifo_read_bytes(local_fifo, (byte *)&key, sizeof(key_t));
40 | return key;
41 | }
42 |
43 | PRIVATE rpc_args_struct wrapper_getkey(int caller, rpc_func func, rpc_args_struct args) {
44 | key_t *result = (key_t *)malloc(sizeof(key_t *));
45 | *result = __getkey();
46 |
47 | return (rpc_args_struct){.data = result, .size = sizeof(key_t)};
48 | }
49 |
50 | PUBLIC void kmod_init(void) {
51 | set_logging_name("Keyboard");
52 |
53 | linfo ("Hi!");
54 |
55 | rpc_register(GETKEY_FN, wrapper_getkey, sizeof(GETKEY_RETURN_TYPE), GETKEY_ARGS_SIZE);
56 | }
57 |
58 | PUBLIC void kmod_main(void) {
59 | local_fifo = create_fifo(LOCAL_FIFO_SIZE);
60 | fifo = share_keybuffer(false);
61 | while (true) {
62 | key_t key;
63 | fifo_read_bytes(fifo, (byte *)&key, sizeof(key_t));
64 |
65 | send_msg(
66 | (msgno_id){.message_no = MSG_NORMAL_IPC, .msg_id = get_msgid()},
67 | &key,
68 | sizeof(key_t),
69 | TTY_DRIVER_SERVICE
70 | ); //发送key给TTY 令其回显
71 |
72 | fifo_write_bytes(local_fifo, (byte *)&key, sizeof(key_t));
73 | }
74 | }
--------------------------------------------------------------------------------
/module/driver/keyboard/include/export/__ioman.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * __ioman.h
12 | *
13 | * IO Manager Functions
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | #define SHARE_KEYBUFFER_FN (0)
25 | #define SHARE_KEYBUFFER_ARGS_SIZE (sizeof(bool))
26 | #define SHARE_KEYBUFFER_RETURN_TYPE void *
27 |
28 | PUBLIC void *share_keybuffer(bool flag);
--------------------------------------------------------------------------------
/module/driver/keyboard/ioman/key_parser.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * key_parser.h
12 | *
13 | * 解析器
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | //状态
24 | EXTERN PUBLIC int state;
25 |
26 | enum {
27 | NORMAL = 0,
28 | E0_XX = 1,
29 | E1_XX = 2,
30 | E1_STAGE_UNIT = 4,
31 | E1_STAGE_MASK = 28,
32 | E1_STAGE_END = 20,
33 | E1_FAILED = 32,
34 | NUMLOCKING = 64,
35 | CAPSLOCKING = 128,
36 | LSHIFT_PUSHED = 1024,
37 | RSHIFT_PUSHED = 2048,
38 | };
39 |
40 | PUBLIC short do_normal(byte code);
41 | PUBLIC short do_e0(byte code);
42 | PUBLIC short do_e1(byte code);
43 | PUBLIC short do_shift(byte code);
--------------------------------------------------------------------------------
/module/driver/keyboard/ioman/keyboard.ld:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * keyboard.ld
12 | *
13 | * keyboard LD脚本
14 | *
15 | */
16 |
17 |
18 |
19 | ENTRY(_start)
20 | OUTPUT_FORMAT("elf64-x86-64")
21 | OUTPUT_ARCH("i386:x86-64")
22 |
23 | SECTIONS
24 | {
25 | . = 0x20000000;
26 | .text : { *(.text) }
27 | .data : { *(.data) }
28 | .bss : { *(.bss) }
29 | }
--------------------------------------------------------------------------------
/module/driver/keyboard/ioman/keymap.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * keymap.h
12 | *
13 | * 扫描码与按键的对应表
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | EXTERN key_t KEYMAP[128][3];
--------------------------------------------------------------------------------
/module/driver/vdd/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/vdd/
20 | BINDIR := $(BINDIR)/vdd/
21 | VDDDIR := $(DRIVERDIR)/vdd/
22 | export OBJECTSDIR BINDIR VDDDIR
23 |
24 | #编译
25 | .PHONY: build
26 | build:
27 | $(CD) tty && $(MAKE) build
28 |
29 | #清理
30 | .PHONY: clean
31 | clean:
32 | $(CD) tty && $(MAKE) clean
33 |
34 | .PHONY: image
35 | image:
36 | $(CD) tty && $(MAKE) image
--------------------------------------------------------------------------------
/module/driver/vdd/tty/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/tty/ioman
20 |
21 | KEYBOARD_OUTPUT := $(BINDIR)/tty.mod
22 | KEYBOARD_SYMBOL_OUTPUT := $(BINDIR)/tty.sym
23 |
24 | CFLAGS := -m64 -Wall -Wno-int-conversion -Wstrict-prototypes -mfpmath=387 -mcmodel=large \
25 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
26 | -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone \
27 | -mpreferred-stack-boundary=3 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
28 |
29 | CPPFLAGS := -m64 -Wall -Wno-int-conversion -Wstrict-prototypes -mfpmath=387 -mcmodel=large \
30 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
31 | -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone \
32 | -mpreferred-stack-boundary=3 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
33 |
34 | C_SRC := main console
35 |
36 | CPP_SRC :=
37 |
38 | ASMFLAGS :=
39 | ASM_SRC :=
40 |
41 | C_DEFS := $(ARCHDEF_C)
42 | C_INCLUDE := -I"$(VDDDIR)/tty" -I"$(ROOTDIR)/libs/libkmod" -I"$(ROOTDIR)/include" -I"$(ROOTDIR)/include/std"
43 |
44 | ifeq ($(MODE), debug)
45 | C_DEFS += -D_DEBUG
46 | else
47 | CPP_DEFS += -D_DEBUG
48 | endif
49 |
50 | CPP_DEFS := $(ARCHDEF_C)
51 | CPP_INCLUDE := -I"$(VDDDIR)/tty" -I"$(ROOTDIR)/libs/libkmod" -I"$(ROOTDIR)/include" -I"$(ROOTDIR)/include/std"
52 |
53 | LD_FLAGS := -static -L"$(BUILDDIR)/bin/" -z max-page-size=0x1000
54 | LIBRARIES := -lkmod
55 |
56 | OBJECTS := $(foreach obj, $(ASM_SRC), $(OBJECTSDIR)/$(obj).o) \
57 | $(foreach obj, $(C_SRC), $(OBJECTSDIR)/$(obj).o) \
58 | $(foreach obj, $(CPP_SRC), $(OBJECTSDIR)/$(obj).o)
59 |
60 | $(OBJECTSDIR)/%.o : %.cpp
61 | $(MKDIR) $(dir $@)
62 | $(GPP) -c $(CPPFLAGS) $(CPP_INCLUDE) $(CPP_DEFS) -o $@ $^
63 |
64 | $(OBJECTSDIR)/%.o : %.c
65 | $(MKDIR) $(dir $@)
66 | $(GCC) -c $(CFLAGS) $(C_INCLUDE) $(C_DEFS) -o $@ $^
67 |
68 | $(OBJECTSDIR)/%.o : %.S
69 | $(MKDIR) $(dir $@)
70 | $(GAS) -o $@ $^
71 |
72 | .PHONY: build
73 | build: $(OBJECTS)
74 | $(MKDIR) $(dir $(KEYBOARD_OUTPUT))
75 | $(LD) $(LD_FLAGS) -T tty.ld -o $(KEYBOARD_OUTPUT) $^ --start-group $(LIBRARIES) --end-group
76 | ifeq ($(MODE), debug)
77 | $(OBJCOPY) --only-keep-debug $(KEYBOARD_OUTPUT) $(KEYBOARD_SYMBOL_OUTPUT)
78 | $(OBJCOPY) --strip-debug $(KEYBOARD_OUTPUT)
79 | endif
80 |
81 | .PHONY: clean
82 | clean:
83 | $(RM) $(KEYBOARD_OUTPUT) $(OBJECTS)
84 |
85 | .PHONY: image
86 | image:
87 | $(SUDO) $(COPY) $(KEYBOARD_OUTPUT) $(TAYHUANGOS_MOUNT_DIR)
88 |
--------------------------------------------------------------------------------
/module/driver/vdd/tty/console.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * console.h
12 | *
13 | * 控制台
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 | #include
24 |
25 | //控制台结构
26 | typedef struct {
27 | void *framebuffer;
28 | int framebuffer_id;
29 | int console_no;
30 | int pos_x;
31 | int pos_y;
32 | int color;
33 | } console_t;
34 |
35 | //相关信息
36 | #define CONSOLE_NUM (4)
37 | #define CONSOLE_COLUMNS (80)
38 | #define CONSOLE_LINES (25)
39 | #define CONSOLE_SIZE (CONSOLE_LINES * CONSOLE_COLUMNS * 2)
40 | #define CONSOLE_PAGES ((CONSOLE_SIZE + (MEMUNIT_SZ - 1)) / MEMUNIT_SZ)
41 |
42 | EXTERN PUBLIC console_t consoles[CONSOLE_NUM];
43 | EXTERN PUBLIC int current_active_console;
44 | EXTERN PUBLIC id_t wait_enter;
45 |
46 | PUBLIC void init_consoles(void);
47 | PUBLIC void console_register_rpc_functions(void);
48 | //回显
49 | PUBLIC void echo_key(key_t key);
--------------------------------------------------------------------------------
/module/driver/vdd/tty/main.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * main.c
12 | *
13 | * tty主函数
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 | #include
21 |
22 | #include
23 | #include
24 |
25 | #include
26 | #include
27 |
28 | #include
29 |
30 | PRIVATE key_t key = 0;
31 | PRIVATE id_t signal = 0;
32 |
33 | PRIVATE void echo_input(void *args) {
34 | while (true) {
35 | //等待key
36 | down_signal(signal);
37 | echo_key(key);
38 | }
39 |
40 | exit_thread(NULL);
41 | }
42 |
43 | PUBLIC void normal_ipc_handler(int caller, void *msg) {
44 | if (caller == KEYBOARD_DRIVER_SERVICE) {
45 | key = *(key_t*)msg;
46 | up_signal(signal); //唤醒回显进程
47 | if (key == ENTER) {
48 | up_signal(wait_enter); //唤醒等待回车的进程
49 | }
50 | }
51 | }
52 |
53 | PUBLIC void kmod_init(void) {
54 | set_logging_name("TTY");
55 | linfo ("Hi!I'm TTY!");
56 |
57 | console_register_rpc_functions();
58 | register_normal_ipc_handler(normal_ipc_handler);
59 |
60 | //用于唤醒回显进程
61 | signal = create_signal(1, 0, false);
62 | create_thread(echo_input, NULL);
63 | }
64 |
65 | PUBLIC void kmod_main(void) {
66 | init_consoles(); //TODO: 在init_consoles调用完前拒绝rpc请求
67 | }
--------------------------------------------------------------------------------
/module/driver/vdd/tty/tty.ld:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * tty.ld
12 | *
13 | * tty LD脚本
14 | *
15 | */
16 |
17 |
18 |
19 | ENTRY(_start)
20 | OUTPUT_FORMAT("elf64-x86-64")
21 | OUTPUT_ARCH("i386:x86-64")
22 |
23 | SECTIONS
24 | {
25 | . = 0x20000000;
26 | .text : { *(.text) }
27 | .data : { *(.data) }
28 | .bss : { *(.bss) }
29 | }
--------------------------------------------------------------------------------
/module/driver/video/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/video/
20 |
21 | VIDEO_OUTPUT := $(BINDIR)/video.mod
22 | VIDEO_SYMBOL_OUTPUT := $(BINDIR)/video.sym
23 |
24 | CFLAGS := -m64 -Wall -Wno-int-conversion -Wstrict-prototypes -mfpmath=387 -mcmodel=large \
25 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
26 | -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone \
27 | -mpreferred-stack-boundary=3 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
28 |
29 | CPPFLAGS := -m64 -Wall -Wno-int-conversion -Wstrict-prototypes -mfpmath=387 -mcmodel=large \
30 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
31 | -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone \
32 | -mpreferred-stack-boundary=3 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
33 |
34 | C_SRC := main text framebuffers
35 |
36 | CPP_SRC :=
37 |
38 | ASMFLAGS :=
39 | ASM_SRC :=
40 |
41 | C_DEFS := $(ARCHDEF_C)
42 | C_INCLUDE := -I"$(DRIVERDIR)/video" -I"$(ROOTDIR)/libs/libkmod" -I"$(ROOTDIR)/include" -I"$(ROOTDIR)/include/std"
43 |
44 | ifeq ($(MODE), debug)
45 | C_DEFS += -D_DEBUG
46 | else
47 | CPP_DEFS += -D_DEBUG
48 | endif
49 |
50 | CPP_DEFS := $(ARCHDEF_C)
51 | CPP_INCLUDE := -I"$(DRIVERDIR)/video" -I"$(ROOTDIR)/libs/libkmod" -I"$(ROOTDIR)/include" -I"$(ROOTDIR)/include/std"
52 |
53 | LD_FLAGS := -static -L"$(BUILDDIR)/bin/" -z max-page-size=0x1000
54 | LIBRARIES := -lkmod
55 |
56 | OBJECTS := $(foreach obj, $(ASM_SRC), $(OBJECTSDIR)/$(obj).o) \
57 | $(foreach obj, $(C_SRC), $(OBJECTSDIR)/$(obj).o) \
58 | $(foreach obj, $(CPP_SRC), $(OBJECTSDIR)/$(obj).o)
59 |
60 | $(OBJECTSDIR)/%.o : %.cpp
61 | $(MKDIR) $(dir $@)
62 | $(GPP) -c $(CPPFLAGS) $(CPP_INCLUDE) $(CPP_DEFS) -o $@ $^
63 |
64 | $(OBJECTSDIR)/%.o : %.c
65 | $(MKDIR) $(dir $@)
66 | $(GCC) -c $(CFLAGS) $(C_INCLUDE) $(C_DEFS) -o $@ $^
67 |
68 | $(OBJECTSDIR)/%.o : %.S
69 | $(MKDIR) $(dir $@)
70 | $(GAS) -o $@ $^
71 |
72 | .PHONY: build
73 | build: $(OBJECTS)
74 | $(MKDIR) $(dir $(VIDEO_OUTPUT))
75 | $(LD) $(LD_FLAGS) -T video.ld -o $(VIDEO_OUTPUT) $^ --start-group $(LIBRARIES) --end-group
76 | ifeq ($(MODE), debug)
77 | $(OBJCOPY) --only-keep-debug $(VIDEO_OUTPUT) $(VIDEO_SYMBOL_OUTPUT)
78 | $(OBJCOPY) --strip-debug $(VIDEO_OUTPUT)
79 | endif
80 |
81 | .PHONY: clean
82 | clean:
83 | $(RM) $(VIDEO_OUTPUT) $(OBJECTS)
84 |
85 | .PHONY: image
86 | image:
87 | $(SUDO) $(COPY) $(VIDEO_OUTPUT) $(TAYHUANGOS_MOUNT_DIR)
88 |
--------------------------------------------------------------------------------
/module/driver/video/framebuffers.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * framebuffers.h
12 | *
13 | * framebuffer表
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | typedef struct {
24 | void *framebuffer;
25 | int column;
26 | int line;
27 | int width;
28 | int height;
29 | } frame_t;
30 |
31 | PUBLIC frame_t *get_framebuffer(int id);
32 | PUBLIC void set_framebuffer(int id, void *framebuffer, int column, int line, int width, int height);
33 | PUBLIC bool has_framebuffer(int id);
34 | PUBLIC void remove_framebuffer(int id);
--------------------------------------------------------------------------------
/module/driver/video/global.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * command.h
12 | *
13 | * 通用部分
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | PUBLIC void text_register_rpc_functions(void);
24 |
25 | EXTERN PUBLIC video_info_struct video_info;
--------------------------------------------------------------------------------
/module/driver/video/main.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * main.c
12 | *
13 | * video主函数
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 |
23 | #include
24 |
25 | #include
26 |
27 | #include
28 | #include
29 |
30 | #include
31 | #include
32 |
33 | PUBLIC video_info_struct video_info;
34 |
35 | PUBLIC void normal_ipc_handler(int caller, void *msg) {
36 | set_allow(ANY_TASK);
37 |
38 | memcpy(&video_info, msg, sizeof(video_info_struct));
39 | }
40 |
41 | PUBLIC void kmod_init(void) {
42 | set_logging_name("Video");
43 |
44 | register_normal_ipc_handler(normal_ipc_handler);
45 | text_register_rpc_functions();
46 | set_allow(ANY_TASK);
47 | }
48 |
49 | PUBLIC void kmod_main(void) {
50 | }
--------------------------------------------------------------------------------
/module/driver/video/video.ld:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * video.ld
12 | *
13 | * video LD脚本
14 | *
15 | */
16 |
17 |
18 |
19 | ENTRY(_start)
20 | OUTPUT_FORMAT("elf64-x86-64")
21 | OUTPUT_ARCH("i386:x86-64")
22 |
23 | SECTIONS
24 | {
25 | . = 0x20000000;
26 | .text : { *(.text) }
27 | .data : { *(.data) }
28 | .bss : { *(.bss) }
29 | }
--------------------------------------------------------------------------------
/module/setup/_libkmod/README.md:
--------------------------------------------------------------------------------
1 | # _Lib KMod
2 |
3 | 为Setup设置的最小化的Libkmod
--------------------------------------------------------------------------------
/module/setup/_libkmod/debug/debug.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * debug.c
12 | *
13 | * assert与panic
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 |
23 | #include
24 |
25 | #include
26 |
27 | #include
28 |
29 | void assertion_failure(const char *expression, const char *file, const char *base_file, int line) {
30 | lerror ("assert(%s) failed; file:%s ; base file: %s ; line: %d\n", expression, file, base_file, line);
31 |
32 | while (true);
33 | asmv ("ud2"); //不应该被执行
34 | }
35 |
36 | void panic_failure(const char *expression, const char *file, const char *base_file, int line) {
37 | dis_int(); //关中断(阻止进程切换)
38 |
39 | lerror("panic_assert(%s) failed; file:%s ; base file: %s ; line: %d\n", expression, file, base_file, line);
40 |
41 | while (true);
42 | asmv ("ud2"); //不应该被执行
43 | }
44 |
45 | void panic(const char *format, ...) {
46 | dis_int(); //关中断(阻止进程切换)
47 |
48 | va_list args;
49 | va_start(args, format);
50 |
51 | static char buffer[256];
52 |
53 | vsprintf (buffer, format, args);
54 |
55 | lerror ("%s", buffer);
56 |
57 | va_end(args);
58 |
59 | while (true);
60 | asmv ("ud2"); //不应该被执行
61 | }
--------------------------------------------------------------------------------
/module/setup/_libkmod/debug/logging.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * logging.h
12 | *
13 | * 日志
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | //输出字符(串)
25 | PUBLIC void write_serial_char(char ch);
26 | PUBLIC void write_serial_str(const char *str);
27 |
28 | //日志类型
29 | enum {
30 | INFO = 0,
31 | WARN,
32 | ERROR,
33 | FATAL,
34 | TIPS,
35 | ATTENTION
36 | };
37 |
38 | //设置日志名称
39 | PUBLIC void set_logging_name(const char *name);
40 |
41 | //打印日志
42 | PUBLIC void __log(const char *type, const char *msg);
43 | PUBLIC void _log(const int type, const char *fmt, va_list args);
44 | PUBLIC void log(const char *type, const char *fmt, ...);
45 | PUBLIC void linfo(const char *fmt, ...);
46 | PUBLIC void lwarn(const char *fmt, ...);
47 | PUBLIC void lerror(const char *fmt, ...);
48 | PUBLIC void lfatal(const char *fmt, ...);
49 | PUBLIC void ltips(const char *fmt, ...);
50 | PUBLIC void lattention(const char *fmt, ...);
--------------------------------------------------------------------------------
/module/setup/_libkmod/entry.S:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * entry.S
12 | *
13 | * 内核模块入口
14 | *
15 | */
16 |
17 |
18 |
19 | .code64
20 | .text
21 |
22 | .extern __kmod_init__
23 | .global _start
24 | _start:
25 | jmp __kmod_init__
26 |
--------------------------------------------------------------------------------
/module/setup/_libkmod/init.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * init.c
12 | *
13 | * 初始化
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 | #include
21 | #include
22 | #include
23 |
24 | #include
25 |
26 | #include
27 |
28 | #include
29 |
30 | #include
31 | #include
32 |
33 | #define MAIL_SIZE (8 * 1024)
34 |
35 | PUBLIC int self_pid;
36 | PUBLIC word msgid_counter;
37 |
38 | PUBLIC void __kmod_init__(void) {
39 | register int magic __asm__("rax");
40 | register void *heap_start __asm__("rbx");
41 | register void *heap_end __asm__("rcx");
42 | register int pid __asm__("rdx");
43 |
44 | if (magic != KMOD_MAGIC) {
45 | while (true);
46 | }
47 |
48 | //设置日志名
49 | set_logging_name("KMod Init");
50 | //初始化堆
51 | init_heap(pid, heap_start, heap_end);
52 |
53 | //分配邮箱缓冲区
54 | void *mail_buffer = malloc(MAIL_SIZE);
55 | set_mailbuffer(mail_buffer, MAIL_SIZE);
56 |
57 | set_allow(ANY_TASK);
58 |
59 | //通知init该模块已初始化完成
60 | bool status = true;
61 | bool send_ret = send_msg((msgno_id){.message_no = MSG_NORMAL_IPC, .msg_id = get_msgid()}, &status, sizeof(bool), INIT_SERVICE);
62 | assert(send_ret);
63 |
64 | kmod_main();
65 |
66 | while (true);
67 | //exit();
68 | }
69 |
--------------------------------------------------------------------------------
/module/setup/_libkmod/libs/ctype.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2021, 2021 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * ctype.h
12 | *
13 | * 标准库ctype实现
14 | *
15 | */
16 |
17 | #include
18 |
19 | //利用提供好的宏实现
20 | //目前只支持ascii字符
21 |
22 | //空字符
23 | int isspace(int ch) {
24 | return __isspace(ch);
25 | }
26 |
27 | //大写字母
28 | int isupper(int ch) {
29 | return __isupper(ch);
30 | }
31 |
32 | //小写字母
33 | int islower(int ch) {
34 | return __islower(ch);
35 | }
36 |
37 | //字母
38 | int isalpha(int ch) {
39 | return __isalpha(ch);
40 | }
41 |
42 | //数字
43 | int isdigit(int ch) {
44 | return __isdigit(ch);
45 | }
46 |
47 | //字母/数字
48 | int isalnum(int ch) {
49 | return __isalnum(ch);
50 | }
51 |
52 | //空格
53 | int isblank(int ch) {
54 | return __isblank(ch);
55 | }
56 |
57 | //控制符
58 | int iscntrl(int ch) {
59 | return __iscntrl(ch);
60 | }
61 |
62 | //可输出字符
63 | int isprint(int ch) {
64 | return __isprint(ch);
65 | }
66 |
67 | //可见字符
68 | int isgraph(int ch) {
69 | return __isgraph(ch);
70 | }
71 |
72 | //符号
73 | int ispunct(int ch) {
74 | return __ispunct(ch);
75 | }
76 |
77 | //16进制位
78 | int isxdigit(int ch) {
79 | return __isxdigit(ch);
80 | }
81 |
82 | //8进制位
83 | int isodigit(int ch) {
84 | return __isodigit(ch);
85 | }
86 |
87 | //大写字母->小写字母
88 | int tolower(int ch) {
89 | return __tolower(ch);
90 | }
91 |
92 | //小写字母->大写字母
93 | int toupper(int ch) {
94 | return __toupper(ch);
95 | }
--------------------------------------------------------------------------------
/module/setup/_libkmod/libs/string.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2021, 2021 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * string.c
12 | *
13 | * 标准库string实现
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 | #include
21 |
22 | int strcmp(const char *str1, const char *str2) {
23 | int len1 = strlen(str1);
24 | int len2 = strlen(str2);
25 | //长度不等
26 | if (len1 != len2) {
27 | if (len1 < len2) {
28 | return -1;
29 | }
30 | else {
31 | return 1;
32 | }
33 | }
34 | int res = 0;
35 | while (*str1 != '\0') {
36 | res = (*str1) - (*str2);
37 | //字符不等
38 | if (res != 0) {
39 | break;
40 | }
41 | str1 ++;
42 | str2 ++;
43 | }
44 |
45 | //前者小于后者
46 | if (res < 0) {
47 | return -1;
48 | }
49 | else if (res > 0) {
50 | return 1;
51 | }
52 | //二者相等
53 | return 0;
54 | }
55 |
56 | int strlen(const char *str) {
57 | int cnt = 0x7FFFFFFF;
58 | //使用字符串扫描指令
59 | __asm__ __volatile__ (
60 | "cld\n"
61 | "repnz\n"
62 | "scasb" :
63 | "+c" (cnt) : "D" (str), "a" (0));
64 | return 0x7FFFFFFF - cnt - 1;
65 | }
66 |
67 | char *strcpy(void *dst, const char *src) {
68 | char *r = dst;
69 | do {
70 | *(r ++) = *(src ++);
71 | } while (*src != '\0'); //遇到\0结束(复制后)
72 | return dst;
73 | }
--------------------------------------------------------------------------------
/module/setup/_libkmod/memory/malloc.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * malloc.h
12 | *
13 | * 分配内存
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | PUBLIC bool init_heap(int pid, void *heap_start, void *heap_end);
24 | PUBLIC void *malloc(int size);
25 | PUBLIC void free(void *addr);
--------------------------------------------------------------------------------
/module/setup/_libkmod/printf.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * printf.h
12 | *
13 | * printf
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | //获取/设置输出颜色
25 | PUBLIC int get_print_color(void);
26 | PUBLIC void set_print_color(int color);
27 | //设置tty
28 | PUBLIC int get_tty(void);
29 | PUBLIC void set_tty(int tty);
30 | PUBLIC void set_active_tty(int tty);
31 | //获取/改变输出位置
32 | PUBLIC void change_pos(int x, int y);
33 | PUBLIC int get_pos_x(void);
34 | PUBLIC int get_pos_y(void);
35 | //获取/改变滚动行位置
36 | PUBLIC int get_scroll_line(void);
37 | PUBLIC void set_scroll_line(int line);
38 | //滚动屏幕
39 | PUBLIC void scroll_screen(int lines);
40 | //刷新到屏幕上
41 | PUBLIC void flush_to_screen(void);
42 |
43 | //清屏
44 | PUBLIC void clrscr(void);
45 | //打印字符(串)
46 | PUBLIC void putchar(char ch);
47 | PUBLIC void puts(const char *str);
48 | //printf变体
49 | PUBLIC int vsprintf(char *buffer, const char *format, va_list args);
50 | PUBLIC int printf(const char *format, ...);
51 | PUBLIC int sprintf(char *buffer, const char *format, ...);
--------------------------------------------------------------------------------
/module/setup/_libkmod/syscall/ipc.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * ipc.h
12 | *
13 | * IPC
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | PUBLIC bool send_msg(msgno_id msgno, void *msg, qword size, int dst);
25 | PUBLIC void check_ipc(void);
26 | PUBLIC void set_allow(int pid);
27 | PUBLIC msgpack_struct recv_msg(void *msg);
28 | PUBLIC void set_mailbuffer(void *buffer, int size);
29 |
30 | EXTERN PUBLIC int self_pid;
31 | EXTERN PUBLIC word msgid_counter;
32 |
33 | static inline word get_msgid(void) {
34 | if (msgid_counter == 65535) {
35 | msgid_counter = 0;
36 | return 65535;
37 | }
38 | return msgid_counter ++;
39 | }
40 |
41 | PUBLIC void set_rpc_result(int msgid, id_t signal, void *data);
42 |
--------------------------------------------------------------------------------
/module/setup/_libkmod/syscall/syscall.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * syscall.h
12 | *
13 | * 系统调用
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | PUBLIC void moo(void);
24 | PUBLIC void reg_irq(int irq);
25 | PUBLIC void exit(int ret);
26 | PUBLIC bool test_and_lock(bool *val);
27 |
28 | PUBLIC id_t create_signal(int max_signals, int value, bool soft);
29 | PUBLIC int get_signals(id_t id);
30 | PUBLIC void up_signal(id_t id);
31 | PUBLIC void down_signal(id_t id);
32 | PUBLIC bool is_soft_signal(id_t id);
33 |
34 | typedef void(*thread_function)(void *);
35 | PUBLIC int create_thread(thread_function entry, void *args);
36 | PUBLIC void exit_thread(void *retval);
37 |
--------------------------------------------------------------------------------
/module/setup/disk.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: Flysong
10 | *
11 | * disk.h
12 | *
13 | * 硬盘操作函数
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 |
24 | #define DISK_SEL_IDE0 (0)
25 | #define DISK_SEL_IDE1 (1)
26 | #define DISK_SEL_MASTER (0)
27 | #define DISK_SEL_SLAVE (2)
28 | #define DISK_SEL_IDE0_MASTER (DISK_SEL_IDE0 | DISK_SEL_MASTER)
29 | #define DISK_SEL_IDE0_SLAVE (DISK_SEL_IDE0 | DISK_SEL_SLAVE)
30 | #define DISK_SEL_IDE1_MASTER (DISK_SEL_IDE1 | DISK_SEL_MASTER)
31 | #define DISK_SEL_IDE1_SLAVE (DISK_SEL_IDE1 | DISK_SEL_SLAVE)
32 |
33 | PUBLIC void identify_disk(int selector, void *dst); //获取硬盘参数
34 | PUBLIC void read_sector(dword lba, int num, int selector, void *dst); //读扇区
35 | PUBLIC void get_partition(int selector, int number, partition_info *member);
--------------------------------------------------------------------------------
/module/setup/fs/common.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: Flysong
10 | *
11 | * common.c
12 | *
13 | * 通用FS接口函数
14 | *
15 | */
16 |
17 |
18 | #include
19 | #include
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 |
27 | typedef enum {
28 | FS_UNKNOWN = 0,
29 | FS_FAT32,
30 | FS_NTFS,
31 | FS_EXT2
32 | } FS_TYPES;
33 |
34 | PRIVATE FS_TYPES get_fs_type(int disk_selector, int partition_id, partition_info *partition) {
35 | get_partition(disk_selector, partition_id, partition); //获取分区
36 |
37 | void *superblock = malloc(512);
38 | read_sector(partition->start_lba, 1, disk_selector, superblock); //读取超级块
39 |
40 | char bpb_filesystem[9];
41 |
42 | memcpy(bpb_filesystem, superblock + 0x36, 8);
43 | bpb_filesystem[8] = '\0';
44 |
45 | free (superblock);
46 |
47 | if (strcmp(bpb_filesystem, "fat32")) {
48 | return FS_FAT32;
49 | }
50 |
51 | return FS_UNKNOWN;
52 | }
53 |
54 | PUBLIC fs_context load_fs(int disk_selector, int partition_id){
55 | partition_info partition;
56 | FS_TYPES type = get_fs_type(disk_selector, partition_id, &partition);
57 | if (type == FS_FAT32) {
58 | return load_fat32_fs(disk_selector, &partition);
59 | }
60 | else {
61 | return NULL;
62 | }
63 | }
64 |
65 | PUBLIC void display_fs_info(fs_context context) {
66 | if (*((dword *)context) == FAT32_CONTEXT_MAGIC) {
67 | display_fat32_fs_info(context);
68 | }
69 | }
70 |
71 | PUBLIC bool load_file(fs_context context, const char *name, void *dst) {
72 | if (*((dword *)context) == FAT32_CONTEXT_MAGIC) {
73 | return load_fat32_file(context, name, dst);
74 | }
75 | return false;
76 | }
77 |
78 | PUBLIC void terminate_fs(fs_context context) {
79 | if (*((dword *)context) == FAT32_CONTEXT_MAGIC) {
80 | terminate_fat32_fs(context);
81 | }
82 | }
--------------------------------------------------------------------------------
/module/setup/fs/common.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: Flysong
10 | *
11 | * common.h
12 | *
13 | * 通用FS接口函数
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 |
23 | typedef void *fs_context;
24 |
25 | PUBLIC fs_context load_fs(int disk_selector, int partition_id);
26 | PUBLIC bool load_file(fs_context context, const char *name, void *dst);
27 | PUBLIC void terminate_fs(fs_context context);
28 | PUBLIC void display_fs_info(fs_context context);
--------------------------------------------------------------------------------
/module/setup/fs/fat32.h:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: Flysong
10 | *
11 | * fat32.h
12 | *
13 | * FAT32文件系统
14 | *
15 | */
16 |
17 |
18 |
19 | #pragma once
20 |
21 | #include
22 | #include
23 | #include
24 |
25 | #define FAT32_CONTEXT_MAGIC (0x93186A8E)
26 | PUBLIC fs_context load_fat32_fs(int disk_selector, partition_info *info);
27 | PUBLIC bool load_fat32_file(fs_context context, const char *name, void *dst);
28 | PUBLIC void terminate_fat32_fs(fs_context context);
29 | PUBLIC void display_fat32_fs_info(fs_context context);
--------------------------------------------------------------------------------
/module/setup/main.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * main.c
12 | *
13 | * setup主函数
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 |
23 | #include
24 | #include
25 |
26 | #include
27 |
28 | #include
29 | #include
30 |
31 | #include
32 | #include
33 |
34 | PUBLIC void kmod_main(void) {
35 | set_logging_name("Setup");
36 |
37 | reg_irq(14);
38 | reg_irq(15);
39 |
40 | char name[256];
41 | void *buffer;
42 |
43 | fs_context context = NULL;
44 |
45 | while (true) {
46 | set_allow(INIT_SERVICE);
47 |
48 | check_ipc();
49 | int recv_ret = recv_msg(name).source;
50 | assert(recv_ret != -1);
51 |
52 | check_ipc();
53 | recv_ret = recv_msg(&buffer).source;
54 | assert(recv_ret != -1);
55 |
56 | if (context == NULL) {
57 | context = load_fs(DISK_SEL_IDE0_MASTER, 0);
58 | }
59 |
60 | linfo ("%s:%p", name, buffer);
61 |
62 | bool status = load_file(context, name, buffer);
63 |
64 | bool send_ret = send_msg((msgno_id){.message_no = MSG_NORMAL_IPC, .msg_id = get_msgid()}, &status, sizeof(bool), INIT_SERVICE);
65 | assert(send_ret);
66 | }
67 |
68 | terminate_fs (context);
69 |
70 | while (true);
71 | }
72 |
--------------------------------------------------------------------------------
/module/setup/setup.ld:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * setup.ld
12 | *
13 | * setup LD脚本
14 | *
15 | */
16 |
17 |
18 |
19 | ENTRY(_start)
20 | OUTPUT_FORMAT("elf64-x86-64")
21 | OUTPUT_ARCH("i386:x86-64")
22 |
23 | SECTIONS
24 | {
25 | . = 0x20000000;
26 | .text : { *(.text) }
27 | .data : { *(.data) }
28 | .bss : { *(.bss) }
29 | }
--------------------------------------------------------------------------------
/module/testbench/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/module/
20 | BINDIR := $(BINDIR)/module/
21 | TESTBENCHDIR := $(MODULEDIR)/testbench/
22 | export OBJECTSDIR BINDIR TESTBENCHDIR
23 |
24 | #编译
25 | .PHONY: build
26 | build:
27 | $(CD) testbench1 && $(MAKE) build
28 | $(CD) testbench2 && $(MAKE) build
29 |
30 | #清理
31 | .PHONY: clean
32 | clean:
33 | $(CD) testbench1 && $(MAKE) clean
34 | $(CD) testbench2 && $(MAKE) clean
35 |
36 | .PHONY: image
37 | image:
38 | $(CD) testbench1 && $(MAKE) image
39 | $(CD) testbench2 && $(MAKE) image
--------------------------------------------------------------------------------
/module/testbench/testbench1/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/testbench1/
20 |
21 | VIDEO_OUTPUT := $(BINDIR)/tb1.mod
22 | VIDEO_SYMBOL_OUTPUT := $(BINDIR)/tb1.sym
23 |
24 | CFLAGS := -m64 -Wall -Wno-int-conversion -Wstrict-prototypes -mfpmath=387 -mcmodel=large \
25 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
26 | -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone \
27 | -mpreferred-stack-boundary=3 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
28 |
29 | CPPFLAGS := -m64 -Wall -Wno-int-conversion -Wstrict-prototypes -mfpmath=387 -mcmodel=large \
30 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
31 | -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone \
32 | -mpreferred-stack-boundary=3 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
33 |
34 | C_SRC := main
35 |
36 | CPP_SRC :=
37 |
38 | ASMFLAGS :=
39 | ASM_SRC :=
40 |
41 | C_DEFS := $(ARCHDEF_C)
42 | C_INCLUDE := -I"$(TESTBENCHDIR)/testbench1" -I"$(ROOTDIR)/libs/libkmod" -I"$(ROOTDIR)/libs/libfifo" -I"$(ROOTDIR)/include" -I"$(ROOTDIR)/include/std"
43 |
44 | ifeq ($(MODE), debug)
45 | C_DEFS += -D_DEBUG
46 | else
47 | CPP_DEFS += -D_DEBUG
48 | endif
49 |
50 | CPP_DEFS := $(ARCHDEF_C)
51 | CPP_INCLUDE := -I"$(TESTBENCHDIR)/testbench1" -I"$(ROOTDIR)/libs/libkmod" -I"$(ROOTDIR)/libs/libfifo" -I"$(ROOTDIR)/include" -I"$(ROOTDIR)/include/std"
52 |
53 | LD_FLAGS := -static -L"$(BUILDDIR)/bin/" -z max-page-size=0x1000
54 | LIBRARIES := -lkmod -lfifo
55 |
56 | OBJECTS := $(foreach obj, $(ASM_SRC), $(OBJECTSDIR)/$(obj).o) \
57 | $(foreach obj, $(C_SRC), $(OBJECTSDIR)/$(obj).o) \
58 | $(foreach obj, $(CPP_SRC), $(OBJECTSDIR)/$(obj).o)
59 |
60 | $(OBJECTSDIR)/%.o : %.cpp
61 | $(MKDIR) $(dir $@)
62 | $(GPP) -c $(CPPFLAGS) $(CPP_INCLUDE) $(CPP_DEFS) -o $@ $^
63 |
64 | $(OBJECTSDIR)/%.o : %.c
65 | $(MKDIR) $(dir $@)
66 | $(GCC) -c $(CFLAGS) $(C_INCLUDE) $(C_DEFS) -o $@ $^
67 |
68 | $(OBJECTSDIR)/%.o : %.S
69 | $(MKDIR) $(dir $@)
70 | $(GAS) -o $@ $^
71 |
72 | .PHONY: build
73 | build: $(OBJECTS)
74 | $(MKDIR) $(dir $(VIDEO_OUTPUT))
75 | $(LD) $(LD_FLAGS) -T testbench1.ld -o $(VIDEO_OUTPUT) $^ --start-group $(LIBRARIES) --end-group
76 | ifeq ($(MODE), debug)
77 | $(OBJCOPY) --only-keep-debug $(VIDEO_OUTPUT) $(VIDEO_SYMBOL_OUTPUT)
78 | $(OBJCOPY) --strip-debug $(VIDEO_OUTPUT)
79 | endif
80 |
81 | .PHONY: clean
82 | clean:
83 | $(RM) $(VIDEO_OUTPUT) $(OBJECTS)
84 |
85 | .PHONY: image
86 | image:
87 | $(SUDO) $(COPY) $(VIDEO_OUTPUT) $(TAYHUANGOS_MOUNT_DIR)
88 |
--------------------------------------------------------------------------------
/module/testbench/testbench1/main.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * main.c
12 | *
13 | * testbench1主函数
14 | *
15 | */
16 |
17 |
18 |
19 | #include
20 |
21 | #include
22 |
23 | #include
24 |
25 | #include
26 |
27 | #include
28 | #include
29 |
30 | #include
31 |
32 | PUBLIC void normal_ipc_handler(int caller, void *msg) {
33 | set_allow(ANY_TASK);
34 |
35 | void *fifo = *(void **)msg;
36 | linfo ("%p", fifo);
37 | qword data;
38 |
39 | fifo_read_bytes(fifo, (byte *)&data, sizeof(qword));
40 |
41 | linfo ("%d", data);
42 | }
43 |
44 | PUBLIC void kmod_init(void) {
45 | set_logging_name("Testbench1");
46 |
47 | linfo ("Hi!I'm testbench1!");
48 |
49 | register_normal_ipc_handler(normal_ipc_handler);
50 | set_allow(ANY_TASK);
51 | }
52 |
53 | PUBLIC void kmod_main(void) {
54 | }
--------------------------------------------------------------------------------
/module/testbench/testbench1/testbench1.ld:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * testbench1.ld
12 | *
13 | * testbench1 LD脚本
14 | *
15 | */
16 |
17 |
18 |
19 | ENTRY(_start)
20 | OUTPUT_FORMAT("elf64-x86-64")
21 | OUTPUT_ARCH("i386:x86-64")
22 |
23 | SECTIONS
24 | {
25 | . = 0x20000000;
26 | .text : { *(.text) }
27 | .data : { *(.data) }
28 | .bss : { *(.bss) }
29 | }
--------------------------------------------------------------------------------
/module/testbench/testbench2/testbench2.ld:
--------------------------------------------------------------------------------
1 | /*
2 | * SPDX-License-Identifier: LGPL-2.1-only
3 | * -------------------------------*-TayhuangOS-*-----------------------------------
4 | *
5 | * Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | *
7 | * --------------------------------------------------------------------------------
8 | *
9 | * 作者: theflysong
10 | *
11 | * testbench2.ld
12 | *
13 | * testbench2 LD脚本
14 | *
15 | */
16 |
17 |
18 |
19 | ENTRY(_start)
20 | OUTPUT_FORMAT("elf64-x86-64")
21 | OUTPUT_ARCH("i386:x86-64")
22 |
23 | SECTIONS
24 | {
25 | . = 0x20000000;
26 | .text : { *(.text) }
27 | .data : { *(.data) }
28 | .bss : { *(.bss) }
29 | }
--------------------------------------------------------------------------------
/setup/MakefileA:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/{SUBMODULE}/
20 | BINDIR := $(BINDIR)/{SUBMODULE}/
21 | {SUBMODULE_UPPER}DIR := $(ROOTDIR)/{SUBMODULE}/
22 | export OBJECTSDIR {SUBMODULE}DIR
23 |
24 | #编译
25 | .PHONY: build
26 | build:
27 | $(CD) {SUBMODULE0} && $(MAKE) build
28 |
29 | #清理
30 | .PHONY: clean
31 | clean:
32 | $(CD) {SUBMODULE0} && $(MAKE) clean
33 |
34 | .PHONY: image
35 | image:
36 | $(CD) {SUBMODULE0} && $(MAKE) image
--------------------------------------------------------------------------------
/setup/MakefileB:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # Makefile
12 | #
13 | # Makefile
14 | #
15 | #
16 |
17 |
18 |
19 | OBJECTSDIR := $(OBJECTSDIR)/{SUBMODULE}/
20 |
21 | {SUBMODULE_UPPER}_OUTPUT := $(BINDIR)/{SUBMODULE}.mod
22 | {SUBMODULE_UPPER}_SYMBOL_OUTPUT := $(BINDIR)/{SUBMODULE}.sym
23 |
24 | CFLAGS := -m64 -Wall -Wno-int-conversion -Wstrict-prototypes -mfpmath=387 -mcmodel=large \
25 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
26 | -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone \
27 | -mpreferred-stack-boundary=3 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
28 |
29 | CPPFLAGS := -m64 -Wall -Wno-int-conversion -Wstrict-prototypes -mfpmath=387 -mcmodel=large \
30 | -fno-strict-aliasing -fomit-frame-pointer -fno-pic -fno-asynchronous-unwind-tables \
31 | -ffreestanding -fno-stack-protector -Wno-int-to-pointer-cast -mno-red-zone \
32 | -mpreferred-stack-boundary=3 -fno-toplevel-reorder -fno-tree-scev-cprop -Os
33 |
34 | {SUB_FOLDER_UPPER}_C_SRC :=
35 | C_SRC := \
36 | $(foreach src, $(${SUB_FOLDER_UPPER}_C_SRC), {SUB_FOLDER}/$(src))
37 |
38 | CPP_SRC :=
39 |
40 | ASMFLAGS :=
41 | ASM_SRC :=
42 |
43 | C_DEFS := $(ARCHDEF_C)
44 | C_INCLUDE := -I"$(MODULEDIR)/{SUBMODULE}" -I"$(ROOTDIR)/include" -I"$(ROOTDIR)/include/std"
45 |
46 | CPP_DEFS := $(ARCHDEF_C)
47 | CPP_INCLUDE := -I"$(MODULEDIR)/{SUBMODULE}" -I"$(ROOTDIR)/include" -I"$(ROOTDIR)/include/std"
48 |
49 | LD_FLAGS := -static -L"$(BUILDDIR)/bin/"
50 | LIBRARIES :=
51 |
52 | OBJECTS := $(foreach obj, $(ASM_SRC), $(OBJECTSDIR)/$(obj).o) \
53 | $(foreach obj, $(C_SRC), $(OBJECTSDIR)/$(obj).o) \
54 | $(foreach obj, $(CPP_SRC), $(OBJECTSDIR)/$(obj).o)
55 |
56 | $(OBJECTSDIR)/%.o : %.cpp
57 | $(MKDIR) $(dir $@)
58 | $(GPP) -c $(CPPFLAGS) $(CPP_INCLUDE) $(CPP_DEFS) -o $@ $^
59 |
60 | $(OBJECTSDIR)/%.o : %.c
61 | $(MKDIR) $(dir $@)
62 | $(GCC) -c $(CFLAGS) $(C_INCLUDE) $(C_DEFS) -o $@ $^
63 |
64 | $(OBJECTSDIR)/%.o : %.S
65 | $(MKDIR) $(dir $@)
66 | $(GAS) -o $@ $^
67 |
68 | .PHONY: build
69 | build: $(OBJECTS)
70 | $(MKDIR) $(dir $({SUBMODULE_UPPER}_OUTPUT))
71 | $(LD) $(LD_FLAGS) -T {SUBMODULE}.ld -o $({SUBMODULE_UPPER}_OUTPUT) $^ --start-group $(LIBRARIES) --end-group
72 | ifeq ($(MODE), debug)
73 | $(OBJCOPY) --only-keep-debug $({SUBMODULE_UPPER}_OUTPUT) $({SUBMODULE_UPPER}_SYMBOL_OUTPUT)
74 | $(OBJCOPY) --strip-debug $({SUBMODULE_UPPER}_OUTPUT)
75 | endif
76 |
77 | .PHONY: clean
78 | clean:
79 | $(RM) $({SUBMODULE_UPPER}_OUTPUT) $(OBJECTS)
80 |
81 | .PHONY: image
82 | image:
83 | $(SUDO) $(COPY) $({SUBMODULE_UPPER}_OUTPUT) $(TAYHUANGOS_MOUNT_DIR)
84 |
--------------------------------------------------------------------------------
/setup/config.mk:
--------------------------------------------------------------------------------
1 | #
2 | # SPDX-License-Identifier: LGPL-2.1-only
3 | # -------------------------------*-TayhuangOS-*-----------------------------------
4 | #
5 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
6 | #
7 | # --------------------------------------------------------------------------------
8 | #
9 | # 作者: theflysong
10 | #
11 | # config.mk
12 | #
13 | # 设置
14 | #
15 | #
16 |
17 |
18 |
19 | # ARCHITECTURE := x86_64
20 | # FILESYSTEM := fat32
21 | # MODE := debug
22 | # VBE_MODE := DISABLE
23 | # LOOPA := /dev/loop19
24 | # LOOPB := /dev/loop20
25 | # KERNEL_PARTITION_OFFSET := 1048576
--------------------------------------------------------------------------------
/setup/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "fileHeaderComment.parameter":{
3 | "*":{
4 | "author": "{AUTHOR}",
5 | "lic_spdx":[
6 | " SPDX-License-Identifier: LGPL-2.1-only"
7 | ],
8 | "lic_tayhuang": [
9 | " -------------------------------*-TayhuangOS-*-----------------------------------",
10 | " ",
11 | " Copyright (C) ${year}, ${year} TayhuangOS Development Team - All Rights Reserved",
12 | " ",
13 | " --------------------------------------------------------------------------------",
14 | " ",
15 | " 作者: ${author}",
16 | " ",
17 | " ${filename}",
18 | " ",
19 | " 在此处键入描述",
20 | " "
21 | ]
22 | }
23 | },
24 | "fileHeaderComment.template":{
25 | "header":[
26 | "${commentbegin}",
27 | "${commentprefix}${lic_spdx}",
28 | "${commentprefix}${lic_tayhuang}",
29 | "${commentend}",
30 | "",
31 | "",
32 | ""
33 | ]
34 | },
35 | "C_Cpp.default.systemIncludePath": ["${workspaceFolder}/"]
36 | }
--------------------------------------------------------------------------------
/tools/build_counter/counter.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python3
2 | #
3 | # SPDX-License-Identifier: LGPL-2.1-only
4 | # -------------------------------*-TayhuangOS-*-----------------------------------
5 | #
6 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
7 | #
8 | # --------------------------------------------------------------------------------
9 | #
10 | # 作者: theflysong
11 | #
12 | # counter.py
13 | #
14 | # 构建计数器
15 | #
16 | #
17 |
18 |
19 |
20 | import pathlib
21 |
22 | build_times_file = "./configs/times.txt"
23 |
24 | build_times = 0
25 |
26 | if pathlib.Path(build_times_file).exists():
27 | build_file = open(build_times_file, mode='r')
28 |
29 | build_times = int(build_file.read(-1))
30 |
31 | build_file.close()
32 |
33 | build_file = open(build_times_file, mode='w+')
34 |
35 | build_times = build_times + 1
36 |
37 | build_file.write(str(build_times))
38 |
39 | build_file.close()
--------------------------------------------------------------------------------
/tools/get_loop_devices/get_loop.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | #
3 | # SPDX-License-Identifier: LGPL-2.1-only
4 | # -------------------------------*-TayhuangOS-*-----------------------------------
5 | #
6 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
7 | #
8 | # --------------------------------------------------------------------------------
9 | #
10 | # 作者: 383494
11 | #
12 | # get_loop.sh
13 | #
14 | # 检测当前可用的回环设备
15 | #
16 | # 输入(stdin):$1 前一个回环设备(默认0)
17 | # 输出(stdout):当前第一个可用的回环设备
18 | # 例: get_loop.sh 0 输出 /dev/loop1
19 | # get_loop.sh /dev/loop0 输出 /dev/loop1
20 | # get_loop.sh 1(/dev/loop2不可用) 输出 /dev/loop3
21 | #
22 |
23 |
24 | loop=$(echo $1 | tr -cd "[0-9]")
25 | let loop++
26 | while [[ -n $(losetup -a | grep "loop"$loop":") ]]
27 | do
28 | let loop++
29 | done
30 | echo "/dev/loop"$loop
31 |
--------------------------------------------------------------------------------
/tools/png_converter/converter.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python3
2 | #
3 | # SPDX-License-Identifier: LGPL-2.1-only
4 | # -------------------------------*-TayhuangOS-*-----------------------------------
5 | #
6 | # Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
7 | #
8 | # --------------------------------------------------------------------------------
9 | #
10 | # 作者: theflysong
11 | #
12 | # converter.py
13 | #
14 | # 图片转换器
15 | #
16 | #
17 |
18 |
19 |
20 | import cv2
21 | import sys
22 |
23 | img = cv2.imread(sys.argv[1], flags = 1)
24 | output = open(sys.argv[2], mode='wb+')
25 |
26 | width = img.shape[0]
27 | height = img.shape[1]
28 |
29 | shrink = 2
30 |
31 | output.write(int(width / shrink).to_bytes(4, 'little'))
32 | output.write(int(height / shrink).to_bytes(4, 'little'))
33 |
34 | for i in range(int(width / shrink)):
35 | for j in range(int(height / shrink)):
36 | red = int(img[i * shrink, j * shrink, 2])
37 | green = int(img[i * shrink, j * shrink, 1])
38 | blue = int(img[i * shrink, j * shrink, 0])
39 | output.write(red.to_bytes(1, 'little'))
40 | output.write(green.to_bytes(1, 'little'))
41 | output.write(blue.to_bytes(1, 'little'))
42 | output.write(int(0).to_bytes(1, 'little'))
43 |
--------------------------------------------------------------------------------
/version.mk:
--------------------------------------------------------------------------------
1 | # 所有对version.mk的修改应在version分支进行
2 | # build version放在config.mk中
3 |
4 | CODE_VERSION := alpha
5 | MAJOR_VERSION := 2
6 | MINOR_VERSION := 27
7 | PATCH_VERSION := 2
--------------------------------------------------------------------------------