Skip to content

Commit 693452c

Browse files
committed
smp增加核心加载等待阻塞
1 parent 0211959 commit 693452c

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

src/core/cpu/smp.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ smp_cpu_t cpus[MAX_CPU];
2222
uint32_t bsp_processor_id;
2323
uint64_t cpu_count = 0;
2424

25+
static int16_t cpu_done_count = 0;
26+
2527
static void apu_hlt(){
2628
cpu_hlt;
2729
}
@@ -91,7 +93,7 @@ static void apu_gdt_setup(){
9193
void apu_entry(){
9294
__asm__ volatile("lidt %0" : : "m"(idt_pointer));
9395

94-
ticket_trylock(&apu_lock);
96+
ticket_lock(&apu_lock);
9597
apu_gdt_setup();
9698

9799
page_table_t *physical_table = virt_to_phys((uint64_t)get_kernel_pagedir()->table);
@@ -126,6 +128,7 @@ void apu_entry(){
126128
smp_cpu_t *cpu = get_cpu_smp(get_current_cpuid());
127129
if(cpu == NULL){
128130
logkf("Error: smp cpu info null %d\n",get_current_cpuid());
131+
cpu->flags = 0;
129132
cpu_hlt;
130133
}
131134
cpu->idle_pcb = apu_idle;
@@ -135,7 +138,8 @@ void apu_entry(){
135138
logkf("Error: scheduler null %d\n",get_current_cpuid());
136139
cpu_hlt;
137140
}
138-
logkf("APU %d started.\n",get_current_cpuid());
141+
logkf("APU %d: %p %p started.\n",get_current_cpuid(),cpu->current_pcb,cpu->idle_pcb);
142+
cpu_done_count++;
139143
ticket_unlock(&apu_lock);
140144
open_interrupt;
141145
apu_hlt();
@@ -149,6 +153,7 @@ smp_cpu_t *get_cpu_smp(uint32_t processor_id){
149153
}
150154

151155
void apu_startup(struct limine_smp_request smp_request){
156+
ticket_init(&apu_lock);
152157
struct limine_smp_response *response = smp_request.response;
153158
cpu_count = response->cpu_count;
154159
for (uint64_t i = 0; i < cpu_count && i < MAX_CPU - 1; i++){
@@ -165,13 +170,21 @@ void apu_startup(struct limine_smp_request smp_request){
165170
info->goto_address = (void*)apu_entry;
166171
}
167172
}
173+
174+
while (cpu_done_count < cpu_count && cpu_done_count < (MAX_CPU - 1)) __asm__ volatile("pause" ::: "memory");
175+
logkf("APU %d processors have been enabled.\n",cpu_count);
176+
168177
smp_cpu_t *cpu = get_cpu_smp(get_current_cpuid());
169178
if(cpu == NULL){
170179
return;
171180
}
181+
cpu->idle_pcb = kernel_head_task;
172182
kernel_head_task->queue_index = queue_enqueue(cpu->scheduler_queue,kernel_head_task);
173183
if(kernel_head_task->queue_index == -1){
174184
logkf("Error: scheduler null %d\n",get_current_cpuid());
175185
}
186+
187+
188+
176189
kinfo("%d processors have been enabled.",cpu_count);
177190
}

src/core/task/pcb.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
extern pcb_t kernel_head_task;
1616
ticketlock pcb_lock;
17+
extern ticketlock scheduler_lock;
1718

1819
int now_pid = 0;
1920

@@ -100,6 +101,8 @@ int create_kernel_thread(int (*_start)(void *arg), void *args, char *name){
100101
}
101102

102103
void init_pcb(){
104+
ticket_init(&pcb_lock);
105+
ticket_init(&scheduler_lock);
103106
kernel_head_task = (pcb_t)malloc(STACK_SIZE);
104107
kernel_head_task->task_level = 0;
105108
kernel_head_task->pid = now_pid++;

src/core/task/scheduler.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ int add_task(pcb_t new_task) {
4545
}
4646
}
4747

48+
logkf("Add task CPU %d idle: %p\n",new_task->pid,cpuid,cpu0->idle_pcb);
49+
4850
if(cpu0 == NULL){
4951
ticket_unlock(&scheduler_lock);
5052
return -1;

src/include/krlibc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* 定义CP_Kernel的各种属性
55
*/
66
#define KERNEL_NAME "CP_Kernel-x86_64-0.0.8" //内核编号
7-
#define MAX_CPU 256 //最大支持CPU核心数
7+
#define MAX_CPU (256*2) //最大支持CPU核心数 256
88
#define STACK_SIZE 32768 //栈大小(byte)
99
#define MAX_WAIT_INDEX 100000 //阻塞最大循环数
1010

xmake.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set_project("CoolPotOS")
33
add_rules("mode.debug", "mode.release")
44
add_requires("zig")
55

6-
set_optimize("fastest")
6+
--set_optimize("fastest")
77
set_languages("c23")
88
--set_warnings("all", "extra", "pedantic", "error")
99

@@ -14,7 +14,7 @@ add_cflags("-target x86_64-freestanding")
1414
add_ldflags("-target x86_64-freestanding")
1515

1616
add_cflags("-mno-80387", "-mno-mmx", "-mno-sse", "-mno-sse2", "-msoft-float","-nostdinc")
17-
add_cflags("-mcmodel=kernel")
17+
add_cflags("-mcmodel=kernel","-mno-red-zone")
1818
add_ldflags("-static","-nostdlib")
1919

2020
target("kernel")

0 commit comments

Comments
 (0)