Skip to content

Commit c84b417

Browse files
修复 usb 部分分配地址不对齐问题, 修复调度器概率死锁问题.
撤掉内核模块输出的同步锁. Co-authored-by: suhuajun-github <115517663+suhuajun-github@users.noreply.github.com>
1 parent a7f28c5 commit c84b417

File tree

11 files changed

+32
-19
lines changed

11 files changed

+32
-19
lines changed

module/all_include/mem_subsystem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#define PAGE_MASK (~(PAGE_SIZE - 1))
2020
#define ENTRY_MASK 0x1FF
2121

22+
#define PADDING_REQ(size, to) ((size + (to) - 1) & ~((to) - 1) / (to))
23+
2224
#include "cp_kernel.h"
2325

2426
typedef struct page_table_entry {

module/hid/hid.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,5 @@ __attribute__((used)) __attribute__((visibility("default"))) int dlstart(void) {
486486

487487
__attribute__((visibility("default"))) int dlmain() {
488488
register_usb_driver(&hid_driver);
489-
490-
return 0;
489+
return EOK;
491490
}

module/xhci/xhci_hcd.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ spin_t transfer_lock = SPIN_INIT;
3838

3939
// 分配DMA内存(对齐到64字节)
4040
static void *xhci_alloc_dma(size_t size, uint64_t *phys_addr) {
41-
void *ptr = malloc((size + 63) & ~63);
41+
size_t aligned_bytes = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
42+
size_t required_pages = aligned_bytes / PAGE_SIZE;
43+
uint64_t phys = alloc_frames(required_pages);
44+
page_map_range_to(get_current_directory(),phys,aligned_bytes,KERNEL_PTE_FLAGS);
45+
void *ptr = phys_to_virt(phys);
4246
if (ptr) {
4347
memset(ptr, 0, size);
44-
if (phys_addr) { *phys_addr = page_virt_to_phys((uint64_t)ptr); }
48+
if (phys_addr) { *phys_addr = phys; }
4549
}
4650
return ptr;
4751
}
@@ -70,7 +74,9 @@ xhci_ring_t *xhci_alloc_ring(uint32_t num_trbs) {
7074
void xhci_free_ring(xhci_ring_t *ring) {
7175
if (!ring) return;
7276

73-
if (ring->trbs) { free(ring->trbs); }
77+
if (ring->trbs) {
78+
unmap_page_range(get_current_directory(),(uint64_t)ring->trbs,ring->size);
79+
}
7480
free(ring);
7581
}
7682

@@ -317,15 +323,18 @@ static int xhci_hcd_init(usb_hcd_t *hcd) {
317323
if (xhci->num_scratchpads > 0) {
318324
xhci->scratchpad_array =
319325
(uint64_t *)xhci_alloc_dma(xhci->num_scratchpads * sizeof(uint64_t), NULL);
320-
xhci->scratchpad_buffers = (void **)malloc(xhci->num_scratchpads * sizeof(void *));
326+
327+
uint64_t scphy = alloc_frames(PADDING_REQ(xhci->num_scratchpads * sizeof(void *),PAGE_SIZE));
328+
page_map_range_to(get_current_directory(),scphy,xhci->num_scratchpads * sizeof(void *),KERNEL_PTE_FLAGS);
329+
xhci->scratchpad_buffers = (void **)phys_to_virt(scphy);
321330

322331
for (uint32_t i = 0; i < xhci->num_scratchpads; i++) {
323332
uint64_t phys;
324333
xhci->scratchpad_buffers[i] = xhci_alloc_dma(4096, &phys);
325334
xhci->scratchpad_array[i] = phys;
326335
}
327336

328-
xhci->dcbaa[0] = page_virt_to_phys((uint64_t)xhci->scratchpad_array);
337+
xhci->dcbaa[0] = (uint64_t)virt_to_phys((uint64_t)xhci->scratchpad_array);
329338
}
330339

331340
// 设置DCBAA指针
@@ -559,7 +568,7 @@ int xhci_setup_default_endpoint(usb_hcd_t *hcd, usb_device_t *device) {
559568
return 0;
560569
}
561570

562-
spin_t xhci_command_lock = {0};
571+
spin_t xhci_command_lock = SPIN_INIT;
563572

564573
// 启用槽位 - 使用等待机制
565574
static int xhci_enable_slot(usb_hcd_t *hcd, usb_device_t *device) {
@@ -1026,7 +1035,7 @@ static int xhci_configure_endpoint(usb_hcd_t *hcd, usb_endpoint_t *endpoint) {
10261035
return ret;
10271036
}
10281037

1029-
spin_t xhci_transfer_lock = {0};
1038+
spin_t xhci_transfer_lock = SPIN_INIT;
10301039

10311040
// 控制传输 - 使用等待机制
10321041
static int xhci_control_transfer(usb_hcd_t *hcd, usb_transfer_t *transfer,
@@ -1258,7 +1267,7 @@ typedef struct enumerater_arg {
12581267
uint8_t speed;
12591268
} enumerater_arg_t;
12601269

1261-
spin_t enumerate_lock = {0};
1270+
spin_t enumerate_lock = SPIN_INIT;
12621271

12631272
void xhci_device_enumerater(enumerater_arg_t *arg) {
12641273
spin_lock(enumerate_lock);
@@ -1387,7 +1396,7 @@ pci_device_t *deviceA = NULL;
13871396

13881397
__attribute__((used)) __attribute__((visibility("default"))) int dlstart(void) {
13891398
usb_hcd_t *xhci_hcd = xhci_init(mmio_vaddr, deviceA);
1390-
printk("xhci: %s inited - port:%d\n", xhci_hcd->name, xhci_hcd->devices->port);
1399+
printk("xhci: %s inited\n", xhci_hcd->name);
13911400
return EOK;
13921401
}
13931402

src/arch/x86_64/include/krlibc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#define PADDING_DOWN(size, to) ((size_t)(size) / (size_t)(to) * (size_t)(to))
4444
#define PADDING_UP(size, to) PADDING_DOWN((size_t)(size) + (size_t)(to) - (size_t)1, to)
45+
#define PADDING_REQ(size, to) ((size + (to) - 1) & ~((to) - 1) / (to))
4546

4647
#define waitif(cond) \
4748
((void)({ \

src/arch/x86_64/kmod/dlinker.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ void dlinker_load(kernel_mode_t *kmod,cp_module_t *module) {
189189

190190
kinfo("Loaded module %s at %#018lx", module->module_name,
191191
KERNEL_MOD_SPACE_START + kernel_modules_load_offset);
192+
logkf("kmod: loaded module %s at %#018lx\n", module->module_name, KERNEL_MOD_SPACE_START + kernel_modules_load_offset);
192193
int ret = dlinit();
193194

194195
(void)ret;

src/arch/x86_64/kmod/module.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ void load_all_kernel_module() {
8282
for (size_t i = 0; i < module_count; i++) {
8383
if (module_ls[i].is_use) {
8484
if (ends_with_km(module_ls[i].raw_name)) {
85-
logkf("kmod: loading module %s raw: %s\n", module_ls[i].module_name,
86-
module_ls[i].raw_name);
8785
cp_module_t *mod = get_module(module_ls[i].module_name);
8886
if (mod) {
8987
int id = id_alloc(kmod_allocator);

src/arch/x86_64/task/pcb.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ _Atomic volatile pid_t now_tid = 0;
3636
_Noreturn void process_exit() {
3737
uint64_t rax = 0;
3838
__asm__("movq %%rax,%0" ::"r"(rax) :);
39-
printk("Kernel thread exit, Code: %d\n", rax);
39+
logkf("Kernel thread exit, Code: %d\n", rax);
4040
kill_thread(get_current_task());
41+
open_interrupt;
4142
loop __asm__ volatile("hlt");
4243
}
4344

src/arch/x86_64/task/scheduler.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ void scheduler_nano_sleep(uint64_t nano) {
6868
*/
6969
int add_task(tcb_t new_task) {
7070
if (new_task == NULL) return -1;
71+
open_interrupt;
72+
enable_scheduler();
7173
spin_lock(scheduler_lock);
7274

7375
smp_cpu_t *cpu0 = get_cpu_smp(bsp_processor_id);

src/driver/usb/usb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "usb.h"
2-
#include "../../../module/all_include/cp_kernel.h"
32
#include "heap.h"
43
#include "kprint.h"
54
#include "krlibc.h"
5+
#include "klog.h"
66

77
usb_driver_t *usb_drivers[MAX_USB_DRIVERS_NUM];
88

src/driver/usb/usb_enumeration.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "usb_enumeration.h"
22
#include "heap.h"
33
#include "kprint.h"
4+
#include "klog.h"
45

56
// 全局地址分配计数器
67
static uint8_t next_device_address = 1;
@@ -46,7 +47,7 @@ static void usb_free_enum_context(usb_enum_context_t *ctx) {
4647
// 解析配置描述符
4748
int usb_parse_config_descriptor(usb_device_t *device, uint8_t *buffer, uint32_t length) {
4849
if (length < sizeof(usb_config_descriptor_t)) {
49-
printk("USB: Config descriptor too short\n");
50+
logkf("USB: Config descriptor too short\n");
5051
return -1;
5152
}
5253

0 commit comments

Comments
 (0)