Skip to content

Commit

Permalink
优化终端输出速度
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyi1212 committed Dec 15, 2024
1 parent 5687026 commit 7851dfc
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 183 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ cmake-build-debug/
# MacOS Cache
.DS_Store

assets/*.elf

Binary file added libs/liballoc-i686.a
Binary file not shown.
50 changes: 30 additions & 20 deletions src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "vsound.h"
#include "shell.h"
#include "iic_core.h"
#include "os_terminal.h"

extern void* program_break_end;

Expand All @@ -53,10 +54,18 @@ _Noreturn void reboot(){
while (1);
}

static void play_music(){
printk("Playing..\n");
wav_player("/music_box.mp3");
printk("PlayEnd\n");
//static void play_music(){
// printk("Playing..\n");
// wav_player("/music_box.mp3");
// printk("PlayEnd\n");
//}

int terminal_manual_flush(void *arg) {
terminal_set_auto_flush(0);
while (1) {
terminal_flush();
io_hlt();
}
}

/*
Expand All @@ -75,15 +84,14 @@ _Noreturn void kernel_main(multiboot_t *multiboot, uint32_t kernel_stack) {
extern void check_memory(multiboot_t *multiboot);
check_memory(multiboot);

if(multiboot->flags & (1 << 2)) {

}

// if (multiboot->flags & (1 << 2)) {
//
// }

page_init(multiboot); //分页开启
setup_free_page();
terminal_setup(false);
printk("CoolPotOS %s (Limine Multiboot) on an i386\n",KERNEL_NAME);
printk("CoolPotOS %s (Limine Multiboot) on an i386\n", KERNEL_NAME);
printk("KernelArea: 0x00000000 - 0x%08x | GraphicsBuffer: 0x%08x \n",
program_break_end,
multiboot->framebuffer_addr);
Expand All @@ -105,7 +113,7 @@ _Noreturn void kernel_main(multiboot_t *multiboot, uint32_t kernel_stack) {

devfs_regist();

io_cli(); //ide驱动会打开中断以加载硬盘设备, 需重新关闭中断以继续初始化其余OS功能
io_cli(); //ide等块设备驱动会打开中断以加载硬盘设备, 需重新关闭中断以继续初始化其余OS功能
iso9660_regist();
fatfs_regist();
init_pcb();
Expand All @@ -123,8 +131,10 @@ _Noreturn void kernel_main(multiboot_t *multiboot, uint32_t kernel_stack) {
// create_kernel_thread((void*)play_music,NULL,"music");
// klogf(true,"Enable music service.\n");

create_kernel_thread((void*)setup_shell, NULL, "Shell");
klogf(true,"Enable kernel shell service.\n");
create_kernel_thread(terminal_manual_flush, NULL, "Terminal");

create_kernel_thread((void *) setup_shell, NULL, "Shell");
klogf(true, "Enable kernel shell service.\n");

// 挂载最后一个块设备(通常为引导设备)
vfs_node_t dev = vfs_open("/dev");
Expand All @@ -133,25 +143,25 @@ _Noreturn void kernel_main(multiboot_t *multiboot, uint32_t kernel_stack) {
list_foreach(dev->child, i) {
c = (vfs_node_t) i->data;
char buf[20];
sprintf(buf,"/dev/%s",c->name);
if(vfs_mount(buf, vfs_open("/")) != -1){
sprintf(buf, "/dev/%s", c->name);
if (vfs_mount(buf, vfs_open("/")) != -1) {
win = true;
break;
}
}
if(c == NULL || !win) {
klogf(false,"Cannot mount a drive device.\n");
}else{
klogf(true,"Block device mount success!\n");
if (c == NULL || !win) {
klogf(false, "Cannot mount a drive device.\n");
} else {
klogf(true, "Block device mount success!\n");
}

klogf(true,"Kernel load done!\n");
klogf(true, "Kernel load done!\n");
beep();
clock_sleep(100);
enable_scheduler();
io_sti(); //内核加载完毕, 打开中断以启动进程调度器, 开始运行

while(1){
while (1) {
free_pages();
io_hlt();
}
Expand Down
23 changes: 17 additions & 6 deletions src/core/memory/free_page.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ void free_pages(){
enable_scheduler();
}

uint32_t get_all_memusage(){
extern void* program_break;
extern void* program_break_end;
uint32_t bytes = (uint32_t)program_break_end - (uint32_t)program_break;

extern pcb_t *running_proc_head;
pcb_t *pcb = running_proc_head;
while(pcb != NULL){
pcb = pcb->next;
if(pcb->task_level != TASK_KERNEL_LEVEL){
bytes += (uint32_t)pcb->program_break_end - (uint32_t)pcb->program_break;
bytes += pcb->exe_file->size;
}
}
return bytes;
}

/*
* 用于回收进程创建的页表项, 由于进程退出过程中还是在该进程的页表中无法直接回收
* 故移动到内核IDLE进程统一回收处理
Expand All @@ -42,9 +59,3 @@ void setup_free_page(){
uint8_t *buf = kmalloc(sizeof(uint32_t) * MAX_FREE_QUEUE);
fifo8_init(fifo8,sizeof(uint32_t) * MAX_FREE_QUEUE,buf);
}

uint32_t kh_usage_memory_byte = 0;

uint32_t get_kernel_memory_usage(){
return kh_usage_memory_byte;
}
6 changes: 4 additions & 2 deletions src/core/memory/kmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@

extern page_directory_t *kernel_directory; //page.c

void *program_break = (void*)0x3e0000;
void *program_break = (void *) 0x3e0000;
void *program_break_end;

#define getpagesize() PAGE_SIZE

uint32_t kh_usage_memory_byte;

static void *sbrk(int incr) { //内核堆扩容措施
if (program_break == 0) {
return (void *) -1;
}

if (program_break + incr >= program_break_end) {
ral:
if(program_break_end >= (void*)0x01bf8f7d) goto alloc_error; // 奇怪的界限, 不设置内核会卡死
if (program_break_end >= (void *) 0x01bf8f7d) goto alloc_error; // 奇怪的界限, 不设置内核会卡死
if ((uint32_t) program_break_end < USER_AREA_START) {
uint32_t ai = (uint32_t) program_break_end;
for (; ai < (uint32_t) program_break_end + PAGE_SIZE * 10;) {
Expand Down
52 changes: 33 additions & 19 deletions src/driver/pci/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
unsigned int PCI_ADDR_BASE;
unsigned int PCI_NUM = 0;

struct
{
struct {
uint32_t classcode;
char *name;
} pci_classnames[] = {
Expand Down Expand Up @@ -146,7 +145,7 @@ struct
pci_device_t *pci_device[PCI_DEVICE_MAX];
uint32_t device_number = 0;

uint32_t get_pci_num(){
uint32_t get_pci_num() {
return PCI_NUM;
}

Expand Down Expand Up @@ -182,7 +181,7 @@ void pci_write_command_status(uint8_t bus, uint8_t slot, uint8_t func, uint32_t
write_pci(bus, slot, func, 0x04, value);
}

uint32_t pci_dev_read32(pci_device_t* pdev, uint16_t offset) {
uint32_t pci_dev_read32(pci_device_t *pdev, uint16_t offset) {
return read_pci(pdev->bus, pdev->slot, pdev->func, offset);
}

Expand Down Expand Up @@ -217,10 +216,10 @@ base_address_register get_base_address_register(uint8_t bus, uint8_t device, uin
case 2: // 64
break;
}
result.address = (uint8_t * )(bar_value & ~0x3);
result.address = (uint8_t *) (bar_value & ~0x3);
result.prefetchable = 0;
} else {
result.address = (uint8_t * )(bar_value & ~0x3);
result.address = (uint8_t *) (bar_value & ~0x3);
result.prefetchable = 0;
}
return result;
Expand All @@ -236,43 +235,43 @@ void pci_config(unsigned int bus, unsigned int f, unsigned int equipment, unsign

char *pci_classname(uint32_t classcode) {
for (size_t i = 0; pci_classnames[i].name != NULL; i++) {
if (pci_classnames[i].classcode == classcode){
if (pci_classnames[i].classcode == classcode) {
return pci_classnames[i].name;
}
if (pci_classnames[i].classcode == (classcode & 0xFFFF00)){
if (pci_classnames[i].classcode == (classcode & 0xFFFF00)) {
return pci_classnames[i].name;
}
}
return "Unknown device";
}

pci_device_t *pci_find_vid_did(uint16_t vendor_id,uint16_t device_id){
pci_device_t *pci_find_vid_did(uint16_t vendor_id, uint16_t device_id) {
for (int i = 0; i < device_number; i++) {
if(pci_device[i]->vendor_id == vendor_id &&
pci_device[i]->device_id == device_id)
if (pci_device[i]->vendor_id == vendor_id &&
pci_device[i]->device_id == device_id)
return pci_device[i];
}
return NULL;
}

pci_device_t *pci_find_class(uint32_t class_code){
pci_device_t *pci_find_class(uint32_t class_code) {
for (int i = 0; i < device_number; i++) {
if (pci_device[i]->class_code == class_code) {
return pci_device[i];
}
if(class_code == (pci_device[i]->class_code & 0xFFFF00)){
if (class_code == (pci_device[i]->class_code & 0xFFFF00)) {
return pci_device[i];
}
}
return NULL;
}

base_address_register find_bar(pci_device_t *device,uint8_t barNum){
base_address_register find_bar(pci_device_t *device, uint8_t barNum) {
base_address_register bar = get_base_address_register(device->bus, device->slot, device->func, barNum);
return bar;
}

void load_pci_device(uint32_t BUS,uint32_t Equipment,uint32_t F){
void load_pci_device(uint32_t BUS, uint32_t Equipment, uint32_t F) {
uint32_t value_c = read_pci(BUS, Equipment, F, PCI_CONF_REVISION);
uint32_t class_code = value_c >> 8;

Expand Down Expand Up @@ -306,10 +305,25 @@ void load_pci_device(uint32_t BUS,uint32_t Equipment,uint32_t F){
device->name);
}

void print_all_pci() {
printk("Bus:Slot:Func\t[Vendor:Device]\tClass Code\tName\n");
for (int i = 0; i < device_number; i++) {
pci_device_t *device = pci_device[i];
printk("%03d:%02d:%02d\t[0x%04X:0x%04X]\t<0x%08x>\t%s\n",
device->bus,
device->slot,
device->func,
device->vendor_id,
device->device_id,
device->class_code,
device->name);
}
}

void init_pci() {
PCI_ADDR_BASE = (uint32_t) kmalloc(1 * 1024 * 1024);
unsigned int i, BUS, Equipment, F, ADDER, *i1;
unsigned char *PCI_DATA = (char*)PCI_ADDR_BASE, *PCI_DATA1;
unsigned char *PCI_DATA = (char *) PCI_ADDR_BASE, *PCI_DATA1;

for (BUS = 0; BUS < 256; BUS++) { //查询总线
for (Equipment = 0; Equipment < 32; Equipment++) { //查询设备
Expand All @@ -333,7 +347,7 @@ void init_pci() {
for (ADDER = 0; ADDER < 256; ADDER = ADDER + 4) {
pci_config(BUS, F, Equipment, ADDER);
i = io_in32(PCI_DATA_PORT);
i1 = (uint32_t *)i;
i1 = (uint32_t *) i;
//*i1 = PCI_DATA1;
memcpy(PCI_DATA1, &i, 4);
PCI_DATA1 = PCI_DATA1 + 4;
Expand All @@ -343,15 +357,15 @@ void init_pci() {
get_base_address_register(BUS, Equipment, F, barNum);
if (bar.address && (bar.type == input_output)) {
PCI_DATA1 += 4;
int i = ((uint32_t)(bar.address));
int i = ((uint32_t) (bar.address));
memcpy(PCI_DATA1, &i, 4);
}
}
PCI_DATA = PCI_DATA + 0x110 + 4;
key = 0;
}
PCI_NUM++;
load_pci_device(BUS,Equipment,F);
load_pci_device(BUS, Equipment, F);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/driver/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ static void tty_print(tty_t *tty_d, const char *msg) {
}

static void tty_putchar(tty_t *tty_d, int c) {
char buf[2] = {c, 0};
tty_print(tty_d, buf);
if (tty_status == TTY_VGA_OUTPUT) {
vga_putchar(c);
} else if (tty_status == TTY_OST_OUTPUT) {
Expand Down
17 changes: 17 additions & 0 deletions src/include/alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "ctypes.h"

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

bool heap_init(uint8_t *address, size_t size);

void *malloc(size_t size);

void free(void *ptr);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
3 changes: 1 addition & 2 deletions src/include/kmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ void *krealloc(void *cp, size_t nbytes);
void *kmalloc(size_t nbytes);
void *kcalloc(size_t nelem, size_t elsize);
void kfree(void *cp);
size_t kmalloc_usable_size(void *cp);
uint32_t get_kernel_memory_usage();
size_t kmalloc_usable_size(void *cp);
Loading

0 comments on commit 7851dfc

Please sign in to comment.