Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
copi143 committed Dec 11, 2024
1 parent 202c00b commit 2f13ace
Show file tree
Hide file tree
Showing 19 changed files with 141 additions and 192 deletions.
16 changes: 16 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,28 @@ Checks: >
modernize-*,
-readability-braces-around-statements,
-readability-identifier-length,
-readability-magic-numbers,
-readability-implicit-bool-conversion,
-bugprone-reserved-identifier,
-bugprone-easily-swappable-parameters,
-readability-isolate-declaration,
-bugprone-sizeof-expression,
-modernize-use-nullptr,
# readability-braces-around-statements
# 我就是宏孩儿。

# readability-magic-numbers
# 典型的一刀切,反正 5 以上就是 namgic number 要写成常量。

# readability-implicit-bool-conversion
# 哥们别老是误报啊。
# if (a == 1 && b == 2) // a, b 为 int
# 这有啥错?

# bugprone-reserved-identifier
# 属于是逮着前缀下划线就报了。

# bugprone-easily-swappable-parameters
# 我一个函数传递几个连续的同类型参数怎么你了?

Expand Down
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ set_optim_flag("-g -O2") # 默认参数,请一直开启调试符号
add_compile_flag("-fvisibility=hidden") # 隐藏符号

# 目标环境
add_compile_flag("-m32 -mfpmath=387")
add_compile_flag("-mno-mmx -mno-sse")
add_compile_flag("-m32 -mmmx -mno-sse")
add_compile_flag("-nostdlib -nostdinc") # 不使用标准库和标准头
add_compile_flag("-ffreestanding") # 我们希望我们的标准库函数能够有和标准库一样的行为,所以我们希望可以不设置此项
# add_link_flag("-nolibc -Qn") # 告诉编译器没有标准库 (但似乎不需要这个)
Expand Down
12 changes: 8 additions & 4 deletions include/define/type/bool.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#pragma once

// 神经你 C23 加这鬼玩意干嘛,没活可以咬个打火机
#if !defined(__cplusplus) && __STDC_VERSION__ < 202300L
# define bool _Bool
# define true ((bool)1)
# define false ((bool)0)
#ifndef __cplusplus
# if __STDC_VERSION__ < 202300L
# define bool _Bool
# define true ((bool)1)
# define false ((bool)0)
# else
# define _Bool typeof("谁 TM 让你用 _Bool 的"[0][0])
# endif
#endif
12 changes: 6 additions & 6 deletions include/kernel/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ typedef struct __PACKED__ fpu_regs {
#define GET_SEL(cs, rpl) ((cs & SA_RPL_MASK & SA_TI_MASK) | (rpl))

typedef struct __PACKED__ TSS32 {
i32 backlink, esp0, ss0, esp1, ss1, esp2, ss2, cr3;
i32 eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi;
i32 es, cs, ss, ds, fs, gs;
i32 ldtr;
u16 trap, iomap;
uint8_t io_map[8192];
i32 backlink, esp0, ss0, esp1, ss1, esp2, ss2, cr3;
i32 eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi;
i32 es, cs, ss, ds, fs, gs;
i32 ldtr;
u16 trap, iomap;
byte io_map[8192];
} TSS32;

#define IDT_ADDR 0x0026f800 // IDT 地址
Expand Down
20 changes: 20 additions & 0 deletions include/kernel/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,23 @@ typedef struct IPC_Header { // IPC头(在TASK结构体中的头)
IPCMessage messages[MAX_IPC_MESSAGE];
lock_t l;
} IPC_Header;

/**
*\brief 发送 IPC 消息
*
*\param pid 进程 ID,-1 为广播,其余负值无效
*\param msgid 32 位有符号信息编号,负值无效
*\param data 数据指针
*\param size 数据大小
*/
void ipc_send(i32 pid, i32 msgid, const void *data, size_t size);

/**
*\brief 接收 IPC 消息
*
*\param msgid 32 位有符号信息编号,负值无效
*\param data 数据指针
*\param size 数据大小
*\return 发送者的进程 ID
*/
i32 ipc_recv(i32 msgid, void *data, size_t size);
8 changes: 3 additions & 5 deletions include/kernel/mtask.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ typedef struct __PACKED__ task {
enum STATE state; // 此项为1(RUNNING) 即正常调度,为 2(WAITING) 3
// (SLEEPING)的时候不执行 ,0 EMPTY 空闲格子
uint64_t jiffies; // 最后一次执行的全局时间片
// struct vfs_t *nfs;
i32 tid, ptid;
i32 tid;
i32 ptid;
u32 alloc_addr;
u32 *alloc_size;
u32 alloced;
struct tty *TTY;
int DisableExpFlag;
u32 CatchEIP;
char flagOfexp;
fpu_regs_t fpu;
int fpu_flag;
Expand All @@ -48,11 +47,10 @@ typedef struct __PACKED__ task {
cb_keyboard_t keyboard_press;
cb_keyboard_t keyboard_release;
char fifosleep;
int mx, my;
char *line;
struct TIMER *timer;
IPC_Header ipc_header;
u32 waittid;
i32 waittid;
int ready; // 如果为waiting 则无视wating
int sigint_up;
u8 train; // 轮询
Expand Down
27 changes: 26 additions & 1 deletion include/kernel/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,23 @@ typedef struct __PACKED__ PageInfo {
u8 count;
} PageInfo;

#ifdef __x86_64__
/**
*\brief 构建线性地址
*\brief 构建线性地址 (9 + 9 + 9 + 9 + 12)
*
*\param pml4 PML4 地址
*\param pdpt PDPT 地址
*\param pd PD 地址
*\param pt PT 地址
*\param off 页内偏移地址
*\return 线性地址
*/
finline size_t mk_linear_addr(size_t pml4, size_t pdpt, size_t pd, size_t pt, size_t off) {
return (pml4 << 39) + (pdpt << 30) + (pd << 21) + (pt << 12) + off;
}
#else
/**
*\brief 构建线性地址 (10 + 10 + 12)
*
*\param table 页目录地址
*\param page 页表地址
Expand All @@ -30,6 +45,7 @@ typedef struct __PACKED__ PageInfo {
finline size_t mk_linear_addr(size_t table, size_t page, size_t off) {
return (table << 22) + (page << 12) + off;
}
#endif

u32 page_get_attr(u32 vaddr);
u32 page_get_attr_pde(u32 vaddr, u32 pde);
Expand All @@ -48,3 +64,12 @@ void page_link_share(u32 addr);
void page_unlink(u32 addr);
u32 page_get_alloced();
void page_link_addr_pde(u32 addr, u32 pde, u32 map_addr);

// u8 PageType
enum {
PAGE_TYPE_CODE, // 代码页
PAGE_TYPE_DATA, // 数据页
PAGE_TYPE_STACK, // 栈页
PAGE_TYPE_HEAP, // 堆页
PAGE_TYPE_ALLOC, // 分配页
};
13 changes: 13 additions & 0 deletions include/libc-base/asm/asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ finline void load_idt(void *addr, size_t len) {
asm volatile("lidt %0\n\t" ::"m"(idt_r));
}

/**
*\brief 加载 TSS
*
*\param selector 选择子
*/
finline void load_tr(size_t selector) {
asm volatile("ltr %0\n\t" : : "m"(selector));
}

#define with_no_interrupts(code) \
({ \
var _flag_ = asm_get_flags(); \
asm_cli; \
(code); \
asm_set_flags(_flag_); \
})
11 changes: 7 additions & 4 deletions include/plmp/mpi32.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,25 @@ API int mpi_cmp(ARGS_2_CC) noexcept { // 比较数字
return 0;
}

#define _add2(a, b) __builtin_uadd_overflow(a, b, &(a))
#define _add3(c, a, b) __builtin_uadd_overflow(a, b, &(c))

// --------------------------------------------------
//; 加减

API bool mpi_add2(ARGS_2) noexcept { // a += b
bool overflow = false;
for (size_t i = 0; i < len; i++) {
overflow = __builtin_uadd_overflow(a[i], overflow, &a[i]);
overflow |= __builtin_uadd_overflow(a[i], b[i], &a[i]);
overflow = _add2(a[i], overflow);
overflow |= _add2(a[i], b[i]);
}
return overflow;
}
API bool mpi_add2u(ARGS_2_MU) noexcept { // a += b
if (len == 0) return b != 0;
bool overflow = __builtin_uadd_overflow(a[0], b, &a[0]);
bool overflow = _add2(a[0], b);
for (size_t i = 1; overflow && i < len; i++) {
overflow = __builtin_uadd_overflow(a[i], overflow, &a[i]);
overflow = _add2(a[i], overflow);
}
return overflow;
}
Expand Down
2 changes: 1 addition & 1 deletion include/ubscan.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ typedef struct SourceLocation {

HANDLE(add_overflow, );
HANDLE(alignment_assumption, );
HANDLE(builtin_unreachable, );
HANDLE_X(builtin_unreachable, );
HANDLE(cfi_bad_type, );
HANDLE(cfi_check_fail, );
HANDLE(divrem_overflow, );
Expand Down
1 change: 0 additions & 1 deletion src/kernel/cpu/cpu_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ finline void _ERROR_(int CODE, char *TIPS) {
void ERROR##_code_(u32 eip) { \
u32 *esp = &eip; \
_ERROR_(_code_, _tips_); \
*esp = current_task()->CatchEIP; \
}

_ERROR(0, "#DE");
Expand Down
92 changes: 10 additions & 82 deletions src/kernel/cpu/x86.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,11 @@
section .data
GLOBAL asm_get_flags, asm_set_flags
GLOBAL move_cursor_by_idx
GLOBAL memtest_sub, farjmp, farcall, start_app
GLOBAL memtest_sub, start_app
GLOBAL return_to_app, entering_v86
section .text
%define ADR_BOTPAK 0x0
farjmp: ; void farjmp(int eip, int cs);
pushad
JMP FAR [ESP + 36] ; eip, cs
popad
RET
farcall: ; void farjmp(int eip, int cs);
pushad
call FAR [ESP + 36] ; eip, cs
popad
RET
EXTERN clear
EXTERN Print_Hex
EXTERN Clear_A_Line
memtest_sub: ; u32 memtest_sub(u32 start, u32 end)
CLI
Expand All @@ -30,12 +17,10 @@ memtest_sub: ; u32 memtest_sub(u32 start, u32 end)
MOV EDI, 0x55aa55aa ; pat1 = 0x55aa55aa;
MOV EAX, [ESP + 12 + 4] ; i = start;
MOV dword[testsize], 1024 * 1024 * 1024 ; testsize = 1024 * 1024 * 1024;
mts_loop:
.mts_loop:
pushad
push eax
;call Print_Hex
add esp, 4
;call Clear_A_Line
popad
MOV EBX, EAX
ADD EBX, [testsize] ; p = i + testsize;
Expand All @@ -44,26 +29,26 @@ mts_loop:
MOV [EBX], ESI ; * p = pat0;
XOR DWORD [EBX], 0xffffffff ; * p ^= 0xffffffff;
CMP EDI, [EBX] ; if ( * p != pat1) goto fin;
JNE mts_fin
JNE .mts_fin
XOR DWORD [EBX], 0xffffffff ; * p ^= 0xffffffff;
CMP ESI, [EBX] ; if ( * p != pat0) goto fin;
JNE mts_fin
JNE .mts_fin
MOV [EBX], EDX ; * p = old;
ADD EAX, [testsize] ; i + = testsize;
CMP EAX, [ESP + 12 + 8] ; if (i <= end) goto mts_loop;
JBE mts_loop
JBE .mts_loop
STI
POP EBX
POP ESI
POP EDI
RET
mts_fin:
.mts_fin:
CMP dword[testsize], 0x1000 ; if (testsize == 0x1000) goto mts_nomore;
JE mts_nomore
JE .mts_nomore
SHR dword[testsize], 2 ; testsize / = 4;
JMP mts_loop
mts_nomore:
JMP .mts_loop
.mts_nomore:
STI
MOV [EBX], EDX ; * p = old;
POP EBX
Expand All @@ -86,7 +71,6 @@ move_cursor_by_idx: ;移动光标
in al, dx
mov bl, al
mov word bx, [esp + 4] ;获取参数中的光标位置
mov dx, 03d4h ;这段代码将改变后的光标位置写入端口内相应的地方以便下次访问
Expand All @@ -104,62 +88,6 @@ move_cursor_by_idx: ;移动光标
out dx, al
ret
GLOBAL get_cpu1
get_cpu1:
mov eax, 0
DB 0x0F, 0xA2
mov eax, ebx
ret
GLOBAL get_cpu2
get_cpu2:
mov eax, 0
DB 0x0F, 0xA2
mov eax, ecx
ret
GLOBAL get_cpu3
get_cpu3:
mov eax, 0
DB 0x0F, 0xA2
mov eax, edx
ret
GLOBAL get_cpu4
get_cpu4:
mov eax, [esp + 4]
DB 0x0F, 0xA2
mov eax, eax
ret
GLOBAL get_cpu5
get_cpu5:
mov eax, [esp + 4]
DB 0x0F, 0xA2
mov eax, ebx
ret
GLOBAL get_cpu6
get_cpu6:
mov eax, [esp + 4]
DB 0x0F, 0xA2
mov eax, ecx
ret
GLOBAL get_cpu7
get_cpu7:
mov eax, [esp + 4]
DB 0x0F, 0xA2
mov eax, edx
ret
global init_float
init_float:
cli
push eax
FNINIT ; load defaults to FPU
mov eax, cr0
and eax, ~(1<<2)
or eax, (1<<4)
mov cr0, eax
pop eax
sti
ret
extern mtask_current
global task_switch, task_start
task_switch:
Expand Down Expand Up @@ -210,7 +138,7 @@ task_start:
; pop ds
; ret
; retuen_to_app_end:

; extern void entering_v86(u32 ss, u32 esp, u32 cs, u32 eip);
entering_v86:
mov ebp, esp ; save stack pointer
Expand Down
Loading

0 comments on commit 2f13ace

Please sign in to comment.