Skip to content

Commit a7f28c5

Browse files
committed
增加新的内核模块加载机制, 补充版权信息.
1 parent 74a5805 commit a7f28c5

File tree

13 files changed

+81
-18
lines changed

13 files changed

+81
-18
lines changed

module/all_include/driver_subsystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,4 @@ int usb_interrupt_transfer(usb_device_t *device, uint8_t endpoint, void *data,
301301
usb_transfer_callback_t callback, void *user_data);
302302
usb_hcd_t *usb_register_hcd(const char *name, usb_hcd_ops_t *ops, void *regs, void *data);
303303
void usb_unregister_hcd(usb_hcd_t *hcd);
304+
int usb_enumerate_device(usb_hcd_t *hcd, uint8_t port_id, uint8_t speed);

module/e1000/e1000.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ errno_t e1000_receive(void *dev_desc, void *buffer, uint32_t buffer_size) {
304304
return have_data ? packet_len : 0;
305305
}
306306

307+
__attribute__((used)) __attribute__((visibility("default"))) int dlstart(void) {
308+
return EOK;
309+
}
310+
307311
__attribute__((used)) __attribute__((visibility("default"))) int dlmain(void) {
308312
pci_device_t *device = pci_find_vid_did(0x8086, 0x100e);
309313
if (device == NULL) {

module/extfs/ext.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ static struct vfs_callback callbacks = {
378378
.dup = (vfs_dup_t)ext_dup,
379379
};
380380

381+
__attribute__((used)) __attribute__((visibility("default"))) int dlstart(void) {
382+
return EOK;
383+
}
384+
381385
__attribute__((used)) __attribute__((visibility("default"))) int dlmain(void) {
382386
ext_fsid = vfs_regist("ext3", &callbacks, 0, 0xef53);
383387
if (ext_fsid == -1) {

module/hid/hid.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
// Copyright (C) 2025 lihanrui2913
1+
/**
2+
* NeoAetherOS XHCI HID Driver
3+
* Copyright (C) 2025 lihanrui2913
4+
*/
25
#include "hid.h"
36
#include "proc_subsystem.h"
7+
#include "errno.h"
48

59
static bool ctrlPressed = false;
610
static bool shiftPressed = false;
@@ -476,6 +480,10 @@ usb_driver_t hid_driver = {
476480
.remove = hid_remove,
477481
};
478482

483+
__attribute__((used)) __attribute__((visibility("default"))) int dlstart(void) {
484+
return EOK;
485+
}
486+
479487
__attribute__((visibility("default"))) int dlmain() {
480488
register_usb_driver(&hid_driver);
481489

module/nvme/nvme.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ NVME_CONTROLLER *nvme_driver_init(uint64_t bar0, uint64_t bar_size) {
463463
return ctrl;
464464
}
465465

466+
__attribute__((used)) __attribute__((visibility("default"))) int dlstart(void) {
467+
return EOK;
468+
}
469+
466470
__attribute__((used)) __attribute__((visibility("default"))) int dlmain(void) {
467471
pci_device_t *device = pci_find_class(0x10802);
468472
if (device == NULL) { return -ENODEV; }

module/xhci/xhci_hcd.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
// Copyright (C) 2025 lihanrui2913
1+
/**
2+
* NeoAetherOS XHCI Driver
3+
* Copyright (C) 2025 lihanrui2913
4+
*/
25
#include "xhci_hcd.h"
36
#include "errno.h"
47
#include "int_subsystem.h"
@@ -1261,7 +1264,7 @@ void xhci_device_enumerater(enumerater_arg_t *arg) {
12611264
spin_lock(enumerate_lock);
12621265
usb_hcd_t *hcd = arg->hcd;
12631266
if (hcd) {
1264-
int ret = 0; //TODO usb_enumerate_device(hcd, arg->port_id, arg->speed);
1267+
int ret = usb_enumerate_device(hcd, arg->port_id, arg->speed);
12651268
if (ret == 1) {
12661269
printk("XHCI: Device on port %d enumerated successfully\n", arg->port_id);
12671270
} else {
@@ -1379,6 +1382,15 @@ void xhci_shutdown(usb_hcd_t *hcd) {
13791382

13801383
//oid xhci_hcd_driver_shutdown(pci_device_t *dev) { xhci_shutdown(dev->desc); }
13811384

1385+
void *mmio_vaddr = NULL;
1386+
pci_device_t *deviceA = NULL;
1387+
1388+
__attribute__((used)) __attribute__((visibility("default"))) int dlstart(void) {
1389+
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);
1391+
return EOK;
1392+
}
1393+
13821394
__attribute__((visibility("default"))) int dlmain() {
13831395
pci_device_t *device = pci_find_class(0x000C0330);
13841396
if (device == NULL) {
@@ -1400,10 +1412,9 @@ __attribute__((visibility("default"))) int dlmain() {
14001412
return -1;
14011413
}
14021414

1403-
void *mmio_vaddr = (void *)phys_to_virt(mmio_base);
1415+
mmio_vaddr = (void *)phys_to_virt(mmio_base);
14041416
page_map_range(get_current_directory(), (uint64_t)mmio_vaddr, mmio_base, mmio_size,
14051417
PTE_PRESENT | PTE_WRITEABLE | PTE_DIS_CACHE);
1406-
usb_hcd_t *xhci_hcd = xhci_init(mmio_vaddr, device);
1407-
printk("xhci: %s inited - port:%d\n", xhci_hcd->name, xhci_hcd->devices->port);
1418+
deviceA = device;
14081419
return EOK;
14091420
}

src/arch/x86_64/include/dlinker.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#define MAX_KERNEL_MODULE 256
4+
35
#include "krlibc.h"
46
#include "module.h"
57

@@ -18,11 +20,18 @@ typedef struct {
1820
void *addr;
1921
} dlfunc_t;
2022

23+
typedef struct kernel_mode {
24+
cp_module_t *module;
25+
dlinit_t entry;
26+
dlinit_t task_entry;
27+
}kernel_mode_t;
28+
2129
/**
2230
* 加载一个内核模块
31+
* @param kmod 内核模块管理单元
2332
* @param module 文件句柄
2433
*/
25-
void dlinker_load(cp_module_t *module);
34+
void dlinker_load(kernel_mode_t *kmod,cp_module_t *module);
2635

2736
dlfunc_t *find_func(const char *name);
2837

@@ -33,3 +42,5 @@ void dlinker_init();
3342
void module_setup();
3443

3544
void load_all_kernel_module();
45+
46+
void start_all_kernel_module();

src/arch/x86_64/kmod/dlinker.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void *find_symbol_address(const char *symbol_name, Elf64_Ehdr *ehdr, uint64_t of
7777
return NULL;
7878
}
7979

80-
dlinit_t load_dynamic(Elf64_Phdr *phdrs, Elf64_Ehdr *ehdr, uint64_t offset) {
80+
dlinit_t load_dynamic(kernel_mode_t *kmod,Elf64_Phdr *phdrs, Elf64_Ehdr *ehdr, uint64_t offset) {
8181
Elf64_Dyn *dyn_entry = NULL;
8282
for (size_t i = 0; i < ehdr->e_phnum; i++) {
8383
if (phdrs[i].p_type == PT_DYNAMIC) {
@@ -142,12 +142,17 @@ dlinit_t load_dynamic(Elf64_Phdr *phdrs, Elf64_Ehdr *ehdr, uint64_t offset) {
142142

143143
void *entry = find_symbol_address("dlmain", ehdr, offset);
144144
if (entry == NULL) { entry = find_symbol_address("_dlmain", ehdr, offset); }
145+
kmod->entry = entry;
146+
147+
void *tentry = find_symbol_address("dlstart", ehdr, offset);
148+
if (tentry == NULL) { tentry = find_symbol_address("_dlstart", ehdr, offset); }
149+
kmod->task_entry = tentry;
145150

146151
dlinit_t dlinit_func = (dlinit_t)entry;
147152
return dlinit_func;
148153
}
149154

150-
void dlinker_load(cp_module_t *module) {
155+
void dlinker_load(kernel_mode_t *kmod,cp_module_t *module) {
151156
if (module == NULL) return;
152157

153158
Elf64_Ehdr *ehdr = (Elf64_Ehdr *)module->data;
@@ -173,7 +178,7 @@ void dlinker_load(cp_module_t *module) {
173178
}
174179

175180
dlinit_t dlinit =
176-
load_dynamic(phdrs, ehdr, KERNEL_MOD_SPACE_START + kernel_modules_load_offset);
181+
load_dynamic(kmod,phdrs, ehdr, KERNEL_MOD_SPACE_START + kernel_modules_load_offset);
177182
if (dlinit == NULL) {
178183
dlinit = (dlinit_t)ehdr->e_entry;
179184
if (dlinit == NULL) {
@@ -184,7 +189,6 @@ void dlinker_load(cp_module_t *module) {
184189

185190
kinfo("Loaded module %s at %#018lx", module->module_name,
186191
KERNEL_MOD_SPACE_START + kernel_modules_load_offset);
187-
188192
int ret = dlinit();
189193

190194
(void)ret;

src/arch/x86_64/kmod/interface.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ EXPORT_SYMBOL(usb_interrupt_transfer);
123123
EXPORT_SYMBOL(register_usb_driver);
124124
EXPORT_SYMBOL(usb_register_hcd);
125125
EXPORT_SYMBOL(usb_unregister_hcd);
126+
EXPORT_SYMBOL(usb_enumerate_device);
126127

127128
/**
128129
* 网络子系统接口导出

src/arch/x86_64/kmod/module.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#include "dlinker.h"
22
#include "klog.h"
33
#include "limine.h"
4+
#include "id_alloc.h"
5+
#include "heap.h"
46

57
cp_module_t module_ls[256];
68
size_t module_count = 0;
79

810
LIMINE_REQUEST struct limine_module_request module = {.id = LIMINE_MODULE_REQUEST, .revision = 0};
911
LIMINE_REQUEST struct limine_kernel_file_request kfile = {.id = LIMINE_KERNEL_FILE_REQUEST,
1012
.response = 0};
13+
id_allocator_t *kmod_allocator = NULL;
14+
kernel_mode_t *kmods[MAX_KERNEL_MODULE];
1115

1216
void extract_name(const char *input, char *output, size_t output_size) {
1317
const char *name = strrchr(input, '/');
@@ -64,14 +68,28 @@ static bool ends_with_km(const char *str) {
6468
return strcmp(str + len - 3, ".km") == 0;
6569
}
6670

71+
void start_all_kernel_module() {
72+
for (size_t i = 0; i < MAX_KERNEL_MODULE; i++) {
73+
kernel_mode_t *kmod = kmods[i];
74+
if(kmod == NULL) continue;
75+
if(kmod->task_entry == NULL) continue;
76+
int ret = kmod->task_entry();
77+
}
78+
}
79+
6780
void load_all_kernel_module() {
81+
kmod_allocator = id_allocator_create(MAX_KERNEL_MODULE);
6882
for (size_t i = 0; i < module_count; i++) {
6983
if (module_ls[i].is_use) {
7084
if (ends_with_km(module_ls[i].raw_name)) {
7185
logkf("kmod: loading module %s raw: %s\n", module_ls[i].module_name,
7286
module_ls[i].raw_name);
7387
cp_module_t *mod = get_module(module_ls[i].module_name);
74-
if (mod) { dlinker_load(mod); }
88+
if (mod) {
89+
int id = id_alloc(kmod_allocator);
90+
kmods[id] = calloc(1,sizeof(kernel_mode_t));
91+
dlinker_load(kmods[id],mod);
92+
}
7593
}
7694
}
7795
}

0 commit comments

Comments
 (0)