Skip to content

Commit a35c723

Browse files
committed
更新ISO9660文件系统
修复SMBIOS的缺陷 优化pcb调度队列
1 parent 6b556aa commit a35c723

File tree

28 files changed

+880
-174
lines changed

28 files changed

+880
-174
lines changed

assets/limine.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ timeout: 0
22

33
/CoolPotOS_CP_Kernel-x86_64
44
protocol: limine
5-
module_path: boot():/sys/sysfont.ttf
5+
# module_path: boot():/sys/sysfont.ttf
66
kernel_path: boot():/sys/cpkrnl.elf

libs/libos_terminal.a

202 KB
Binary file not shown.

libs/os_terminal.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ typedef struct {
2323
uint32_t ansi_colors[16];
2424
} TerminalPalette;
2525

26+
typedef struct {
27+
const char *(*get)(void);
28+
void (*set)(const char*);
29+
} TerminalClipboard;
30+
2631
#ifdef __cplusplus
2732
extern "C" {
2833
#endif // __cplusplus
@@ -67,11 +72,13 @@ void terminal_set_color_scheme(size_t palette_index);
6772

6873
void terminal_set_custom_color_scheme(const TerminalPalette *palette);
6974

70-
void terminal_string_free(char *s);
75+
void terminal_set_clipboard(TerminalClipboard clipboard);
76+
77+
void terminal_set_pty_writer(void (*writer)(const uint8_t*));
7178

72-
char *terminal_handle_keyboard(uint8_t scancode);
79+
void terminal_handle_keyboard(uint8_t scancode);
7380

74-
char *terminal_handle_mouse_scroll(ptrdiff_t delta);
81+
void terminal_handle_mouse_scroll(ptrdiff_t delta);
7582

7683
#ifdef __cplusplus
7784
} // extern "C"

src/core/cpu/gdt.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ gdt_entries_t gdt_entries;
77
struct gdt_register gdt_pointer;
88
tss_t tss0;
99
tss_stack_t tss_stack;
10+
extern uint32_t bsp_processor_id;
1011

1112
extern smp_cpu_t cpus[MAX_CPU];
1213

@@ -59,7 +60,7 @@ void tss_setup() {
5960

6061
void set_kernel_stack(uint64_t rsp){
6162
uint64_t cpuid = get_current_cpuid();
62-
if(cpuid == 0) tss0.rsp[0] = rsp;
63+
if(cpuid == bsp_processor_id) tss0.rsp[0] = rsp;
6364
else {
6465
cpus[cpuid].tss0.rsp[0] = rsp;
6566
}

src/core/cpu/smp.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ extern bool x2apic_mode; //apic.c
1919
ticketlock apu_lock;
2020

2121
smp_cpu_t cpus[MAX_CPU];
22-
uint32_t bsp_lapic_id;
22+
uint32_t bsp_processor_id;
2323
uint64_t cpu_count = 0;
2424

25+
static void apu_hlt(){
26+
cpu_hlt;
27+
}
28+
2529
uint64_t cpu_num(){
2630
return cpu_count;
2731
}
@@ -110,29 +114,34 @@ void apu_entry(){
110114
apu_idle->kernel_stack = apu_idle->context0.rsp = get_rsp();
111115
apu_idle->user_stack = apu_idle->kernel_stack;
112116
apu_idle->tty = get_default_tty();
113-
apu_idle->context0.rflags = get_rflags();
117+
apu_idle->context0.rflags = get_rflags() | 0x200;
114118
apu_idle->cpu_timer = nanoTime();
115119
apu_idle->time_buf = alloc_timer();
116120
apu_idle->cpu_id = get_current_cpuid();
121+
apu_idle->context0.rip = (uint64_t)apu_hlt;
117122
char name[50];
118123
sprintf(name,"CP_IDLE_CPU%lu",get_current_cpuid());
119124
memcpy(apu_idle->name, name, strlen(name));
125+
apu_idle->name[strlen(name)] = '\0';
120126
smp_cpu_t *cpu = get_cpu_smp(get_current_cpuid());
121127
if(cpu == NULL){
122-
return;
128+
logkf("Error: smp cpu info null %d\n",get_current_cpuid());
129+
cpu_hlt;
123130
}
124131
cpu->idle_pcb = apu_idle;
125132
apu_idle->queue_index = queue_enqueue(cpu->scheduler_queue,apu_idle);
126133
if(apu_idle->queue_index == -1){
127134
logkf("Error: scheduler null %d\n",get_current_cpuid());
135+
cpu_hlt;
128136
}
129137
pivfs_update(kernel_head_task);
138+
logkf("APU %d started.\n",get_current_cpuid());
130139
ticket_unlock(&apu_lock);
131-
cpu_hlt;
140+
open_interrupt;
141+
apu_hlt();
132142
}
133143

134144
smp_cpu_t *get_cpu_smp(uint32_t processor_id){
135-
if(processor_id == bsp_lapic_id) return &cpus[MAX_CPU - 1];
136145
smp_cpu_t cpu = cpus[processor_id];
137146
if(cpu.flags == 1){
138147
return &cpus[processor_id];
@@ -142,22 +151,18 @@ smp_cpu_t *get_cpu_smp(uint32_t processor_id){
142151
void apu_startup(struct limine_smp_request smp_request){
143152
struct limine_smp_response *response = smp_request.response;
144153
cpu_count = response->cpu_count;
145-
bsp_lapic_id = response->bsp_lapic_id;
146154
for (uint64_t i = 0; i < cpu_count && i < MAX_CPU - 1; i++){
147155
struct limine_smp_info *info = response->cpus[i];
148-
if(info->lapic_id == response->bsp_lapic_id) {
149-
cpus[MAX_CPU - 1].lapic_id = response->bsp_lapic_id;
150-
cpus[MAX_CPU - 1].flags = 1;
151-
cpus[MAX_CPU - 1].scheduler_queue = queue_init();
152-
cpus[MAX_CPU - 1].iter_node = NULL;
153-
cpus[MAX_CPU - 1].idle_pcb = kernel_head_task;
154-
continue;
155-
}
156156
cpus[info->processor_id].scheduler_queue = queue_init();
157-
cpus[info->processor_id].lapic_id = info->lapic_id;
158157
cpus[info->processor_id].flags = 1;
159158
cpus[info->processor_id].iter_node = NULL;
160-
info->goto_address = (void*)apu_entry;
159+
cpus[info->processor_id].lapic_id = info->lapic_id;
160+
if(info->lapic_id == response->bsp_lapic_id){
161+
bsp_processor_id = info->processor_id;
162+
cpus[info->processor_id].idle_pcb = kernel_head_task;
163+
} else {
164+
info->goto_address = (void*)apu_entry;
165+
}
161166
}
162167
smp_cpu_t *cpu = get_cpu_smp(get_current_cpuid());
163168
if(cpu == NULL){

src/core/error_handle.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ void print_register(interrupt_frame_t *frame){
1919
}
2020

2121
void print_task_info(pcb_t pcb){
22-
printk("Current process PID: %d (%s) CPU%d\n",pcb->pid,pcb->name,get_current_cpuid());
22+
if(pcb == NULL) printk("Current process PID: 0 (Kernel) CPU%d\n",get_current_cpuid());
23+
else printk("Current process PID: %d (%s) CPU%d\n",pcb->pid,pcb->name,get_current_cpuid());
2324
}
2425

2526
void kernel_error(const char *msg,uint64_t code,interrupt_frame_t *frame) {

src/core/main.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ __attribute__((used, section(".limine_requests_end")))
5353
static volatile LIMINE_REQUESTS_END_MARKER
5454

5555
extern void error_setup(); //error_handle.c
56+
extern void iso9660_regist(); //iso9660.c
5657

5758
_Noreturn void cp_shutdown() {
5859
printk("Shutdown %s...\n", KERNEL_NAME);
@@ -80,32 +81,34 @@ void kmain(void) {
8081
printk("CoolPotOS %s (%s version %s) (Limine Bootloader) on an x86_64\n", KERNEL_NAME, COMPILER_NAME, COMPILER_VERSION);
8182
init_cpuid();
8283
kinfo("Video: 0x%p - %d x %d", framebuffer->address, framebuffer->width, framebuffer->height);
83-
kinfo("SMBIOS %d.%d.0 present.\n",smbios_major_version(), smbios_minor_version());
8484
gdt_setup();
8585
idt_setup();
8686
page_setup();
8787
error_setup();
88+
smbios_setup();
8889
acpi_setup();
8990
keyboard_setup();
9091
mouse_setup();
9192
char *date = get_date_time();
9293
kinfo("RTC time %s", date);
9394
free(date);
9495
vfs_init();
96+
iso9660_regist();
97+
9598
vdisk_init();
9699
devfs_setup();
97100
pcie_init();
98101
ide_setup();
99102
//nvme_setup();
100-
//ahci_setup();
103+
// ahci_setup();
101104
//xhci_setup();
102105

103106

104107
pivfs_setup();
105108
init_pcb();
106109
smp_setup();
107110
build_stream_device();
108-
111+
109112
init_iic();
110113

111114
/*TODO*/ create_kernel_thread(terminal_flush_service, NULL, "TerminalFlush");

src/core/mem/page.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ __IRQHANDLER static void page_fault_handle(interrupt_frame_t *frame,uint64_t err
2828
uint64_t faulting_address;
2929
__asm__ volatile("mov %%cr2, %0" : "=r"(faulting_address));
3030
logkf("Page fault, virtual address 0x%x\n", faulting_address);
31-
logkf("Current process PID: %d (%s)\n",get_current_task()->pid,get_current_task()->name);
31+
if(get_current_task() != NULL){
32+
logkf("Current process PID: %d (%s)\n",get_current_task()->pid,get_current_task()->name);
33+
}
3234
printk("\n");
3335
printk("\033[31m:3 Your CP_Kernel ran into a problem.\nERROR CODE >(PageFault%s:0x%p)<\033[0m\n",
3436
!(error_code & 0x1) ? "NotPresent" :
3537
error_code & 0x2 ? "WriteError" :
3638
error_code & 0x4 ? "UserMode" :
3739
error_code & 0x8 ? "ReservedBitsSet" :
3840
error_code & 0x10 ? "DecodeAddress" : "Unknown",faulting_address);
39-
printk("Current process PID: %d (%s) at CPU%d\n",get_current_task()->pid,get_current_task()->name,get_current_cpuid());
41+
if(get_current_task() != NULL){
42+
printk("Current process PID: %d (%s) at CPU%d\n",get_current_task()->pid,get_current_task()->name,get_current_cpuid());
43+
}
4044
print_register(frame);
4145
update_terminal();
4246
cpu_hlt;

src/core/task/pcb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,5 @@ void init_pcb(){
131131
memcpy(current_task->name, name, strlen(name));
132132
pivfs_update(kernel_head_task);
133133

134-
kinfo("Load task schedule. | KernelProcessName: %s PID: %d", current_task->name, current_task->pid);
134+
kinfo("Load task schedule. | KernelProcess PID: %d", current_task->pid);
135135
}

src/core/task/scheduler.c

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ pcb_t current_task = NULL;
1111
pcb_t kernel_head_task = NULL;
1212
bool is_scheduler = false;
1313

14+
extern uint64_t cpu_count; //smp.c
15+
extern uint32_t bsp_processor_id; //smp.c
16+
1417
ticketlock scheduler_lock;
18+
ticketlock scheduler_lock_rw;
1519

1620
pcb_t get_current_task(){
1721
return current_task;
@@ -29,20 +33,31 @@ int add_task(pcb_t new_task) {
2933
if (new_task == NULL) return -1;
3034
ticket_lock(&scheduler_lock);
3135

32-
smp_cpu_t *cpu = get_cpu_smp(get_current_cpuid());
33-
if(cpu == NULL){
36+
smp_cpu_t *cpu0 = get_cpu_smp(bsp_processor_id);
37+
uint32_t cpuid = bsp_processor_id;
38+
39+
// for (int i = 0; i < cpu_count; i++) {
40+
// smp_cpu_t *cpu = get_cpu_smp(i);
41+
// if(cpu == NULL) continue;
42+
// if(cpu->flags == 1 && cpu->scheduler_queue->size < cpu0->scheduler_queue->size){
43+
// cpu0 = cpu;
44+
// cpuid = i;
45+
// }
46+
// }
47+
48+
if(cpu0 == NULL){
3449
ticket_unlock(&scheduler_lock);
3550
return -1;
3651
}
37-
new_task->queue_index = queue_enqueue(cpu->scheduler_queue,new_task);
52+
new_task->cpu_id = cpuid;
53+
new_task->queue_index = queue_enqueue(cpu0->scheduler_queue,new_task);
3854
if(new_task->queue_index == -1){
3955
logkf("Error: scheduler null %d\n",get_current_cpuid());
4056
return -1;
4157
}
58+
if(new_task->pid == cpu0->idle_pcb->pid) goto ret;
4259

43-
if(new_task->pid == kernel_head_task->pid) goto ret;
44-
45-
pivfs_update(kernel_head_task);
60+
//pivfs_update(kernel_head_task);
4661
ret:
4762
ticket_unlock(&scheduler_lock);
4863
return new_task->queue_index;
@@ -58,7 +73,7 @@ void remove_task(pcb_t task){
5873
return;
5974
}
6075
queue_remove_at(cpu->scheduler_queue,task->queue_index);
61-
pivfs_update(kernel_head_task);
76+
//pivfs_update(kernel_head_task);
6277
ticket_unlock(&scheduler_lock);
6378
}
6479

@@ -109,17 +124,30 @@ void change_proccess(registers_t *reg,pcb_t taget){
109124
reg->rip = taget->context0.rip;
110125
reg->rsp = taget->context0.rsp;
111126

127+
112128
current_task = taget;
113129
}
114130

115131
/**
116-
* CP_Kernel 默认单核调度器 - 循环公平调度
132+
* CP_Kernel 默认多核调度器 - 循环公平调度
117133
* @param reg 当前进程上下文
118134
*/
119135
void scheduler(registers_t *reg){
120136
if(is_scheduler){
121137
if(current_task != NULL){
138+
ticket_lock(&scheduler_lock);
122139
smp_cpu_t *cpu = get_cpu_smp(get_current_cpuid());
140+
if(cpu == NULL) {
141+
logkf("Error: scheduler null %d\n",get_current_cpuid());
142+
ticket_unlock(&scheduler_lock);
143+
return;
144+
}
145+
146+
if(cpu->scheduler_queue->size == 1){
147+
ticket_unlock(&scheduler_lock);
148+
return;
149+
}
150+
123151
if (cpu->iter_node == NULL) {
124152
iter_head:
125153
cpu->iter_node = cpu->scheduler_queue->head;
@@ -133,7 +161,6 @@ void scheduler(registers_t *reg){
133161
}
134162

135163
pcb_t next = (pcb_t)data;
136-
// logkf("p: %p\n",next);
137164

138165
current_task->cpu_clock++;
139166
if(current_task->time_buf != NULL){
@@ -147,6 +174,8 @@ void scheduler(registers_t *reg){
147174
change_proccess(reg,next);
148175
enable_scheduler();
149176
}
177+
178+
ticket_unlock(&scheduler_lock);
150179
}
151180
}
152181
send_eoi();

0 commit comments

Comments
 (0)