初步写好用户进程调度

This commit is contained in:
xiaoyi1212 2024-07-30 22:50:03 +08:00
parent 49f205ea9d
commit ec1ef4e6c2
34 changed files with 545 additions and 252 deletions

View File

@ -1,7 +1,7 @@
#include "../include/stdio.h"
int main(){
while (1);
put_char('A');
return 0;
}

View File

@ -1,7 +1,11 @@
#include "../include/stdio.h"
void putc(char c){
}
int main(){
putc('A');
while (1);
put_char('A');
return 0;
}

View File

@ -5,7 +5,7 @@ put_char:
push eax
mov edx,[ss:esp+12]
mov eax,0x01
int 80h
int 31h
pop eax
pop edx
ret

View File

@ -99,7 +99,7 @@ def build_data(): # 构建常用工具
def build_sysapp(): # 构建内置系统应用
print("Building sysapp source code...")
for file in os.listdir(cd + dir_ + src + 'sysapp'):
cmd = cd + gcc + "-O0 " + src + "sysapp" + dir_ + file + " -o " + "target" + dir_ + file.split(".")[0] + ".o"
cmd = cd + gcc + "-Og " + src + "sysapp" + dir_ + file + " -o " + "target" + dir_ + file.split(".")[0] + ".o"
e = os.system(cmd)
if e != 0:
return -1

View File

@ -1 +1,45 @@
global taskX_switch
global asm_syscall_handler
taskX_switch:
mov eax, [esp+4]
mov [eax+0], esp
mov [eax+4], ebp
mov [eax+8], ebx
mov [eax+12], esi
mov [eax+16], edi
mov [eax+20], edx
pushf
ret
extern syscall_handler
asm_syscall_handler:
cli
push byte 0
push 80h
pusha
mov ax, ds
push eax ; 存储ds
mov ax, 0x10 ; 将内核数据段赋值给各段
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
call syscall_handler
pop eax ; 恢复
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
popa
add esp, 8 ; 弹出错误码和中断ID
iret

View File

@ -4,8 +4,8 @@
/*.set FLAGS, 3 */
.set CHECKSUM, -(MAGIC + FLAGS)
.set MODE_TYPE, 0
.set WIDTH, 1280 /* requested width */
.set HEIGHT, 720 /* requested height */
.set WIDTH, 1024 /* requested width */
.set HEIGHT, 768 /* requested height */
/* .set WIDTH, 640
.set HEIGHT, 480 */
.set DEPTH, 32 /* requested bits per pixel BPP */

View File

@ -48,9 +48,12 @@ switch_to:
mov [eax+8], ebx
mov [eax+12], esi
mov [eax+16], edi
pushf
pop ecx
mov [eax+20], ecx
mov [eax+24], edx
pushf ;保存eflags
pop ecx
mov [eax+28], ecx
mov eax, [esp+8]
@ -59,7 +62,10 @@ switch_to:
mov ebx, [eax+8]
mov esi, [eax+12]
mov edi, [eax+16]
mov eax, [eax+20]
mov ecx, [eax+20]
mov edx, [eax+24]
mov eax, [eax+28] ;加载eflags
push eax
popf

View File

@ -18,7 +18,7 @@ static void cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, unsigne
static void get_vendor_name(cpu_t *c) {
int cpuid_level;
char x86_vendor_id[16] = {0};
static char x86_vendor_id[16] = {0};
cpuid(0x00000000, (unsigned int *) &cpuid_level,
(unsigned int *) &x86_vendor_id[0],
(unsigned int *) &x86_vendor_id[8],

View File

@ -3,6 +3,7 @@
#include "../include/memory.h"
#include "../include/queue.h"
#include "../include/io.h"
#include "../include/klog.h"
KEY_STATUS *key_status;
Queue *key_char_queue;
@ -143,6 +144,10 @@ int handle_keyboard_input(registers_t *reg){
int release = key & 0xb10000000;
char c = key_status->is_shift ? shift_keyboard_map[(unsigned char )key] : keyboard_map[(unsigned char )key];
io_cli();
info("PS/2 Keyboard: %c : %08x",c,key);
struct key_listener* h = head_listener;
while (1){
h->func(key,release,c);

View File

@ -5,26 +5,83 @@
#include "../include/dma.h"
#include "../include/file.h"
static void *const DMA_BUF_ADDR1 = (void *)0x90000; // 不能跨越 64K 边界
static void *const DMA_BUF_ADDR2 = (void *)0x90000 + DMA_BUF_SIZE; // 不能跨越 64K 边界
struct sb16 sb;
void sb_exch_dmaaddr() {
char *addr = sb.addr1;
sb.addr1 = sb.addr2;
sb.addr2 = addr;
size_t size = sb.size1;
sb.size1 = sb.size2;
sb.size2 = size;
}
static void sb_out(uint8_t cmd) {
while (io_in8(SB_WRITE) & 128)
;
io_out8(SB_WRITE, cmd);
}
void sb16_do_dma() {
// 设置采样率
sb_out(CMD_SOSR); // 44100 = 0xAC44
sb_out((SAMPLE_RATE >> 8) & 0xFF); // 0xAC
sb_out(SAMPLE_RATE & 0xFF); // 0x44
dma_xfer(sb.channel, (uint32_t)(sb.addr2), sb.size2, 0);
if (sb.mode == MODE_MONO8) {
sb_out(CMD_SINGLE_OUT8);
sb_out(MODE_MONO8);
} else {
sb_out(CMD_SINGLE_OUT16);
sb_out(MODE_STEREO16);
}
sb_out((sb.size2 - 1) & 0xFF);
sb_out(((sb.size2 - 1) >> 8) & 0xFF);
}
void sb16_do_close() {
sb_out(CMD_OFF); // 关闭声卡
sb.use_task = NULL;
}
void sb16_handler(registers_t *reg) {
io_in8(SB_INTR16);
uint8_t state = io_in8(SB_STATE);
logkf("sb16 handler state 0x%X...\n", state);
sb.flag = !sb.flag;
io_cli();
sb.size2 = 0;
if (sb.size1 > 0) {
sb_exch_dmaaddr();
sb16_do_dma();
}
io_cli();
if (sb.status > 0) {
sb16_do_close();
return;
}
}
void disable_sb16() {
sb.addr = (char*)DMA_BUF_ADDR;
sb.mode = MODE_STEREO16;
sb.channel = 5;
sb.addr1 = DMA_BUF_ADDR1;
sb.addr2 = DMA_BUF_ADDR2;
sb.mode = MODE_STEREO16;
sb.channel = 5;
sb.use_task = NULL;
register_interrupt_handler(SB16_IRQ + 0x20, sb16_handler);
sb.size1 = 0;
sb.size2 = 0;
sb.status = 0;
register_interrupt_handler(SB16_IRQ + 0x20, (uint32_t)sb16_handler);
}
static void sb_reset() {
io_out8(SB_RESET, 1);
sleep(1);
sleep(10);
io_out8(SB_RESET, 0);
uint8_t state = io_in8(SB_READ);
logkf("sb16 reset state 0x%x\n", state);
@ -39,12 +96,6 @@ static void sb_intr_irq() {
}
}
static void sb_out(uint8_t cmd) {
while (io_in8(SB_WRITE) & 128)
;
io_out8(SB_WRITE, cmd);
}
static void sb_turn(bool on) {
if (on)
sb_out(CMD_ON);
@ -56,91 +107,47 @@ static uint32_t sb_time_constant(uint8_t channels, uint16_t sample) {
return 65536 - (256000000 / (channels * sample));
}
static void sb_set_volume(uint8_t level) {
logkf("set sb16 volume to 0x%02X\n", level);
void sb16_set_volume(uint8_t level) {
logkf("set sb16 volume to %d/255\n", level);
io_out8(SB_MIXER, 0x22);
io_out8(SB_MIXER_DATA, level);
}
int sb16_set(int cmd, void* args) {
switch (cmd) {
// 设置 tty 参数
case 0:
while (sb.use_task)
;
io_cli();
sb.use_task = get_current();
io_sti();
sb_reset(); // 重置 DSP
sb_intr_irq(); // 设置中断
sb_out(CMD_ON); // 打开声霸卡
return 0;
case 1:
sb_out(CMD_OFF); // 关闭声霸卡
sb.use_task = NULL;
return 0;
case 2:
sb.mode = MODE_MONO8;
sb.channel = 1;
return 0;
case 3:
sb.mode = MODE_STEREO16;
sb.channel = 5;
return 0;
case 4:
sb_set_volume((uint32_t)args & 0xff);
return 0;
default:
break;
}
return -1;
void sb16_open() {
while (sb.use_task) {}
io_cli();
sb.use_task = get_current();
io_sti();
sb_reset(); // 重置 DSP
sb_intr_irq(); // 设置中断
sb_out(CMD_ON); // 打开声卡
sb.mode = MODE_MONO8;
sb.channel = 1;
sb.size1 = 0;
sb.size2 = 0;
sb.status = 0;
// sb.mode = MODE_STEREO16;
// sb.channel = 5;
}
int sb16_write(char* data, size_t size) {
memcpy(sb.addr, data, size);
// 设置采样率
sb_out(CMD_SOSR); // 44100 = 0xAC44
sb_out((SAMPLE_RATE >> 8) & 0xFF); // 0xAC
sb_out(SAMPLE_RATE & 0xFF); // 0x44
dma_xfer(sb.channel, sb.addr, size, 0);
if (sb.mode == MODE_MONO8) {
sb_out(CMD_SINGLE_OUT8);
sb_out(MODE_MONO8);
} else {
sb_out(CMD_SINGLE_OUT16);
sb_out(MODE_STEREO16);
size >>= 2; // size /= 4
}
void sb16_close() {
sb.status = 1;
if (sb.size2 > 0) return;
sb16_do_close();
}
int sb16_write(char *data, size_t size) {
while (sb.size1) {}
memcpy(sb.addr1, data, size);
sb.size1 = size;
if (sb.size2 > 0) return size;
sb_exch_dmaaddr();
sb16_do_dma();
sb_out((size - 1) & 0xFF);
sb_out(((size - 1) >> 8) & 0xFF);
sb.flag = 0;
//while (!sb.flag);
return size;
}
void wav_player(char* filename) {
FILE* fp = fopen(filename, "rb");
if(fp == NULL) {
logk("Open wav file was throw error.\n");
return;
}
char* buf = kmalloc(DMA_BUF_SIZE);
fread(buf, 1, 44, fp);
logk("SB3 I\n");
sb16_set(0, 0);
logk("SB3 II\n");
sb16_set(2, 0);
logk("SB3 III\n");
sb16_set(4, 0xff);
logk("SB3 IV\n");
int sz;
while (sz = fread(buf, 1, DMA_BUF_SIZE, fp)) {
logkf("%02x ",sz);
sb16_write(buf, sz);
}
logk("\nSB3 IIV\n");
sb16_set(1, 0);
logk("SB3 IIIV\n");
fclose(fp);
}

View File

@ -93,6 +93,7 @@ void vbe_draw_char(char c, int32_t x, int32_t y) {
uint8_t *font = bafont;
//uint8_t *font = ascfont;
font += c * 16;
@ -100,14 +101,14 @@ void vbe_draw_char(char c, int32_t x, int32_t y) {
for (int j = 0; j < 9; j++) {
if (font[i] & (0x80 >> j)) {
screen[(y + i) * width + x + j] = color;
} else screen[(y + i) * width + x + j] = back_color;
} //else screen[(y + i) * width + x + j] = back_color;
}
}
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 9; j++) {
if (font[i] & (0x80 >> j)) {
screen[(y + i) * width + x + j] = color;
} else screen[(y + i) * width + x + j + 1] = back_color;
} //else screen[(y + i) * width + x + j + 1] = back_color;
}
}
}

View File

@ -6,6 +6,8 @@
#include "../include/vfs.h"
#include "../include/common.h"
#include "../include/memory.h"
#include "../include/fat.h"
#include "../include/iso9660.h"
vfs_t vfsstl[26];
vfs_t vfsMount_Stl[26];
@ -95,9 +97,6 @@ bool vfs_mount_disk(uint8_t disk_number, uint8_t drive) {
seat->drive = drive;
seat->disk_number = disk_number;
seat->flag = 1;
printf("Disk %c mount success!\n",disk_number);
return true;
}
@ -404,6 +403,8 @@ void init_vfs() {
}
vfs_now = NULL;
klogf(true,"Virtual File System initialize.\n");
Register_fat_fileSys();
init_iso9660();
}
bool vfs_register_fs(vfs_t vfs) {

View File

@ -73,6 +73,8 @@ DECLARE_IRQ(12)
DECLARE_IRQ(13)
DECLARE_IRQ(14)
DECLARE_IRQ(15)
DECLARE_IRQ(80)
#undef DECLARE_IRQ
struct idt_entry_struct {

43
src/include/klog.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef CRASHPOWEROS_KLOG_H
#define CRASHPOWEROS_KLOG_H
#define CONCAT_(a, b) a##b
#define CONCAT(a, b) CONCAT_(a, b)
#define CEND "\033c6c6c6;"
#define COLOR_DEBUG "\033708090;"
#define COLOR_INFO "\0337FFFD4;"
#define COLOR_WARN "\033FFD700;"
#define COLOR_ERROR "\033ee6363;"
#define COLOR_FATAL "\033ee6363;"
#define STR_DEBUG "[" COLOR_DEBUG "Debug" CEND "] "
#define STR_INFO "[" COLOR_INFO "Info " CEND "] "
#define STR_WARN "[" COLOR_WARN "Warn " CEND "] "
#define STR_ERROR "[" COLOR_ERROR "Error" CEND "] "
#define STR_FATAL "[" COLOR_FATAL "Fatal" CEND "] "
const char *_log_basename_(const char *path);
#define ARG_LOGINFO_FUNC __func__, __LINE__
#define ARG_LOGINFO_FILE _log_basename_(__FILE__)
#define STR_LOGINFO_FILE "[\0337FFFD4;%s" CEND "] "
#define STR_LOGINFO_FUNC "[%s" CEND ":%d" CEND "] "
#define ARG_LOGINFO ARG_LOGINFO_FILE, ARG_LOGINFO_FUNC
#define STR_LOGINFO STR_LOGINFO_FILE STR_LOGINFO_FUNC
#define _LOG(type, fmt, ...) \
logkf(CONCAT(STR, type) STR_LOGINFO CONCAT(COLOR, type) fmt CEND "\n", ARG_LOGINFO, \
##__VA_ARGS__)
#define printi(fmt, ...) _LOG(_INFO, fmt, ##__VA_ARGS__)
#define printw(fmt, ...) _LOG(_WARN, fmt, ##__VA_ARGS__)
#define printe(fmt, ...) _LOG(_ERROR, fmt, ##__VA_ARGS__)
#define info(fmt, ...) printi(fmt, ##__VA_ARGS__)
#define warn(fmt, ...) printw(fmt, ##__VA_ARGS__)
#define error(fmt, ...) printe(fmt, ##__VA_ARGS__)
#endif

View File

@ -13,9 +13,6 @@
#define USER_END (USER_START + 0xf00000)
#define USER_HEAP_END (USER_END - STACK_SIZE)
#define US_B_START 0xe0000000
#define US_B_END 0xe1000000
#define INDEX_FROM_BIT(a) (a / (8*4))
#define OFFSET_FROM_BIT(a) (a % (8*4))
@ -97,7 +94,7 @@ void memclean(char *s, int len);
void *realloc(void *ptr, uint32_t size);
void alloc_frame_line(page_t *page, unsigned line,int is_kernel, int is_writable);
void alloc_frame_line(page_t *page, uint32_t line,int is_kernel, int is_writable);
void free_frame(page_t *page);

29
src/include/proc.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef CRASHPOWEROS_PROC_H
#define CRASHPOWEROS_PROC_H
#include "common.h"
struct stackframe {
uint32_t gs;
uint32_t fs;
uint32_t es;
uint32_t ds;
uint32_t edi;
uint32_t esi;
uint32_t ebp;
uint32_t kernel_esp;
uint32_t ebx;
uint32_t edx;
uint32_t ecx;
uint32_t eax;
uint32_t retaddr;
uint32_t eip;
uint32_t cs;
uint32_t eflags;
uint32_t esp;
uint32_t ss;
};
void proc_install();
#endif

View File

@ -1,50 +1,47 @@
#ifndef CRASHPOWEROS_SB16_H
#define CRASHPOWEROS_SB16_H
#define SB_MIXER 0x224 // DSP 混合器端口
#define SB_MIXER_DATA 0x225 // DSP 混合器数据端口
#define SB_RESET 0x226 // DSP 重置
#define SB_READ 0x22A // DSP 读
#define SB_WRITE 0x22C // DSP 写
#define SB_STATE 0x22E // DSP 读状态
#define SB_INTR16 0x22F // DSP 16 位中断响应
#define SB_MIXER 0x224 // DSP 混合器端口
#define SB_MIXER_DATA 0x225 // DSP 混合器数据端口
#define SB_RESET 0x226 // DSP 重置
#define SB_READ 0x22A // DSP 读
#define SB_WRITE 0x22C // DSP 写
#define SB_STATE 0x22E // DSP 读状态
#define SB_INTR16 0x22F // DSP 16 位中断响应
#define CMD_STC 0x40 // Set Time Constant
#define CMD_SOSR 0x41 // Set Output Sample Rate
#define CMD_SISR 0x42 // Set Input Sample Rate
#define CMD_STC 0x40 // Set Time Constant
#define CMD_SOSR 0x41 // Set Output Sample Rate
#define CMD_SISR 0x42 // Set Input Sample Rate
#define CMD_SINGLE_IN8 0xC8 // Transfer mode 8bit input
#define CMD_SINGLE_OUT8 0xC0 // Transfer mode 8bit output
#define CMD_SINGLE_IN16 0xB8 // Transfer mode 16bit input
#define CMD_SINGLE_OUT16 0xB0 // Transfer mode 16bit output
#define CMD_SINGLE_IN8 0xC8 // Transfer mode 8bit input
#define CMD_SINGLE_OUT8 0xC0 // Transfer mode 8bit output
#define CMD_SINGLE_IN16 0xB8 // Transfer mode 16bit input
#define CMD_SINGLE_OUT16 0xB0 // Transfer mode 16bit output
#define CMD_AUTO_IN8 0xCE // Transfer mode 8bit input auto
#define CMD_AUTO_OUT8 0xC6 // Transfer mode 8bit output auto
#define CMD_AUTO_IN16 0xBE // Transfer mode 16bit input auto
#define CMD_AUTO_OUT16 0xB6 // Transfer mode 16bit output auto
#define CMD_AUTO_IN8 0xCE // Transfer mode 8bit input auto
#define CMD_AUTO_OUT8 0xC6 // Transfer mode 8bit output auto
#define CMD_AUTO_IN16 0xBE // Transfer mode 16bit input auto
#define CMD_AUTO_OUT16 0xB6 // Transfer mode 16bit output auto
#define CMD_ON 0xD1 // Turn speaker on
#define CMD_OFF 0xD3 // Turn speaker off
#define CMD_SP8 0xD0 // Stop playing 8 bit channel
#define CMD_RP8 0xD4 // Resume playback of 8 bit channel
#define CMD_SP16 0xD5 // Stop playing 16 bit channel
#define CMD_RP16 0xD6 // Resume playback of 16 bit channel
#define CMD_VERSION 0xE1 // Turn speaker off
#define CMD_ON 0xD1 // Turn speaker on
#define CMD_OFF 0xD3 // Turn speaker off
#define CMD_SP8 0xD0 // Stop playing 8 bit channel
#define CMD_RP8 0xD4 // Resume playback of 8 bit channel
#define CMD_SP16 0xD5 // Stop playing 16 bit channel
#define CMD_RP16 0xD6 // Resume playback of 16 bit channel
#define CMD_VERSION 0xE1 // Turn speaker off
#define MODE_MONO8 0x00
// #define MODE_STEREO8 0x20
// #define MODE_MONO16 0x10
#define MODE_MONO8 0x00
#define MODE_STEREO8 0x20
#define MODE_MONO16 0x10
#define MODE_STEREO16 0x30
#define STATUS_READ 0x80 // read buffer status
#define STATUS_WRITE 0x80 // write buffer status
#define STATUS_READ 0x80 // read buffer status
#define STATUS_WRITE 0x80 // write buffer status
#define DMA_BUF_ADDR 0x90000 // 不能跨越 64K 边界
#define DMA_BUF_SIZE 0x8000 // 缓冲区长度
#define SAMPLE_RATE 44100 // 采样率
#define SB16_IRQ 5
#define DMA_BUF_SIZE 0x8000 // 缓冲区长度
#define SAMPLE_RATE 44100 // 采样率
#define SB16_IRQ 5
#include "task.h"
#include "common.h"
@ -64,17 +61,23 @@ struct WAV16_HEADER {
struct sb16 {
struct task_struct *use_task;
int status;
char* addr; // DMA 地址
uint8_t mode; // 模式
uint8_t channel; // DMA 通道
uint8_t flag;
int status;
char *addr1; // DMA 地址
volatile size_t size1; //
char *addr2; // DMA 地址
volatile size_t size2; //
uint8_t mode; // 模式
uint8_t channel; // DMA 通道
};
void disable_sb16();
void sb16_handler(registers_t *reg);
int sb16_set(int cmd, void* args);
int sb16_write(char* data, size_t size);
void wav_player(char* filename);
void sb_exch_dmaaddr();
void sb16_do_dma();
void sb16_do_close();
void disable_sb16();
void sb16_close();
int sb16_write(char *data, size_t size);
void sb16_open();
void sb16_set_volume(uint8_t level);
#endif

6
src/include/soundtest.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef CRASHPOWEROS_SOUNDTEST_H
#define CRASHPOWEROS_SOUNDTEST_H
void sound_test();
#endif

View File

@ -17,13 +17,20 @@ enum task_state {
TASK_DEATH = 4 // 终止状态
} task_state;
struct context {
struct context{
uint32_t esp;
uint32_t ebp;
uint32_t ebx;
uint32_t esi;
uint32_t edi;
uint32_t ecx;
uint32_t edx;
uint32_t eflags;
uint32_t eax;
uint32_t eip;
uint32_t ds;
uint32_t cs;
};
// 进程控制块 PCB
@ -55,9 +62,9 @@ void print_proc();
void init_sched();
void schedule();
void schedule(registers_t *reg);
void change_task_to(struct task_struct *next);
void change_task_to(registers_t *reg,struct task_struct *next);
void task_kill(int pid);

View File

@ -61,7 +61,9 @@ void gdt_install() {
gdt_flush((uint32_t)&gdt_ptr);
write_tss(5, 0x10, 0x0);
register uint32_t esp asm("esp");
write_tss(5, 0x10, esp);
tss_flush();
}

View File

@ -70,7 +70,6 @@ void idt_install() {
outb(0x21, 0x0);
outb(0xA1, 0x0);
#define REGISTER_ISR(id) idt_set_gate(id, (uint32_t) isr##id, 0x08, 0x8E)
REGISTER_ISR(0);
REGISTER_ISR(1);
@ -103,8 +102,8 @@ void idt_install() {
REGISTER_ISR(28);
REGISTER_ISR(29);
REGISTER_ISR(30);
REGISTER_ISR(31);
#undef REGISTER_ISR
idt_use_reg(31, (uint32_t) isr31);
#define REGISTER_IRQ(id, irq_id) idt_set_gate(id, (uint32_t) irq##irq_id, 0x08, 0x8E)
REGISTER_IRQ(32, 0);
REGISTER_IRQ(33, 1);

View File

@ -16,11 +16,10 @@
#include "../include/pcnet.h"
#include "../include/ide.h"
#include "../include/vfs.h"
#include "../include/fat.h"
#include "../include/iso9660.h"
#include "../include/panic.h"
#include "../include/mouse.h"
#include "../include/desktop.h"
#include "../include/soundtest.h"
#define CHECK_FLAG(flags,bit) ((flags) & (1 << (bit)))
@ -78,7 +77,6 @@ void kernel_main(multiboot_t *multiboot) {
initVBE(multiboot);
printf("CPOS_Kernel %s (GRUB Multiboot) on an i386.\n",OS_VERSION);
printf("Memory Size: %dMB\n",(multiboot->mem_upper + multiboot->mem_lower) / 1024 + 1);
printf("Graphics[ width: %d | height: %d | address: %08x ]\n",multiboot->framebuffer_width,multiboot->framebuffer_height,multiboot->framebuffer_addr);
gdt_install();
idt_install();
init_timer(1);
@ -86,6 +84,7 @@ void kernel_main(multiboot_t *multiboot) {
init_page(multiboot);
init_sched();
//proc_install();
init_keyboard();
init_pit();
@ -94,8 +93,6 @@ void kernel_main(multiboot_t *multiboot) {
init_vdisk();
ide_initialize(0x1F0, 0x3F6, 0x170, 0x376, 0x000);
init_vfs();
Register_fat_fileSys();
init_iso9660();
syscall_install();
char disk_id = '0';
@ -125,21 +122,20 @@ void kernel_main(multiboot_t *multiboot) {
//init_pcnet_card();
}
klogf(true,"Kernel load done!\n");
printf("\n\n");
kernel_thread(sound_test,NULL,"Sound");
io_sti();
clock_sleep(25);
// vfs_change_disk('B');
vfs_change_path("apps");
//klogf(user_process("service.bin","Service") != -1,"Service base process init.\n");
klogf(user_process("init.bin","Init") != -1,"Init base process init.\n");
user_process("init.bin","User-Init");
user_process("service.bin","Service");
int pid = kernel_thread(setup_shell,NULL,"CPOS-Shell");
klogf(pid != -1,"Launch kernel shell.\n");
kernel_thread(check_task,&pid,"CPOS-CK");
print_proc();
//menu_sel();
//uint32_t pid = kernel_thread(setup_shell,NULL,"CPOS-Shell");
//kernel_thread(check_task,&pid,"CPOS-SC");
//panic_pane("Proccess out of memory error!",OUT_OF_MEMORY);

View File

@ -22,7 +22,7 @@ static void set_frame(uint32_t frame_addr) {
frames[idx] |= (0x1 << off);
}
static void clear_frame(uint32_t frame_addr) {
static void clear_frame(uint32_t frame_addr) { //释放物理块
uint32_t frame = frame_addr / 0x1000;
uint32_t idx = INDEX_FROM_BIT(frame);
uint32_t off = OFFSET_FROM_BIT(frame);
@ -70,9 +70,9 @@ void alloc_frame(page_t *page, int is_kernel, int is_writable) {
page->frame = idx;
}
}
void alloc_frame_line(page_t *page, unsigned line,int is_kernel, int is_writable) {
// logk("%08x\n",line);
void alloc_frame_line(page_t *page, uint32_t line,int is_kernel, int is_writable) {
set_frame(line);
memset(page,0,4);
page->present = 1; // 现在这个页存在了
page->rw = is_writable ? 1 : 0; // 是否可写由is_writable决定
page->user = is_kernel ? 0 : 1; // 是否为用户态由is_kernel决定
@ -133,7 +133,7 @@ page_t *get_page(uint32_t address, int make, page_directory_t *dir,bool ist) {
void page_fault(registers_t *regs) {
asm("cli");
io_cli();
uint32_t faulting_address;
asm volatile("mov %%cr2, %0" : "=r" (faulting_address)); //
@ -163,8 +163,8 @@ void page_fault(registers_t *regs) {
task_kill(current->pid);
}
io_sti();
sleep(1);
io_sti();
}
static page_table_t *clone_table(page_table_t *src, uint32_t *physAddr) {
@ -225,7 +225,11 @@ void init_page(multiboot_t *mboot) {
int i = 0;
while (i < placement_address + 0x30000) {
// 内核部分对ring3而言可读不可写 | 无偏移页表映射
/*
* ring3而言不可读不可写
*
* , 线
*/
alloc_frame(get_page(i, 1, kernel_directory,false), 1, 1);
i += 0x1000;
}
@ -233,7 +237,7 @@ void init_page(multiboot_t *mboot) {
unsigned int j = mboot->framebuffer_addr,size = mboot->framebuffer_height * mboot->framebuffer_width*mboot->framebuffer_bpp;
while (j <= mboot->framebuffer_addr + size){
alloc_frame_line(get_page(j,1,kernel_directory,false),j,1,1);
alloc_frame_line(get_page(j,1,kernel_directory,false),j,0,1);
j += 0x1000;
}
@ -248,13 +252,9 @@ void init_page(multiboot_t *mboot) {
program_break = (void *) KHEAP_START;
program_break_end = (void *) (KHEAP_START + KHEAP_INITIAL_SIZE);
klogf(true,"Memory manager is enable\n"
"Kernel: 0x00 - 0x%08x "
"Framebuffer: 0x%08x - 0x%08x "
"KernelHeap: 0x%08x - 0x%08x "
"BaseFrame: 0x%08x\n",
placement_address + 0x30000,
mboot->framebuffer_addr,mboot->framebuffer_addr + size,
KHEAP_START,KHEAP_START + KHEAP_INITIAL_SIZE,
frames);
klogf(true,"Memory manager is enable\n");
printf("Kernel: 0x%08x | ",placement_address + 0x30000);
printf("GraphicsBuffer: 0x%08x - 0x%08x \n",(mboot->framebuffer_addr),(mboot->framebuffer_addr + size));
printf("KernelHeap: 0x%08x - 0x%08x | ",(KHEAP_START),(KHEAP_START + KHEAP_INITIAL_SIZE));
printf("BaseFrame: 0x%08x\n",frames);
}

View File

@ -20,6 +20,7 @@ extern uint32_t c_height;
Bmp *panic_bmp;
static GP_13(registers_t *reg){
printf("throw #GP 13 error.\n");
if(current->pid == 0){
printf("Kernel PANIC(#GP), Please restart your CPOS Kernel.\n");
while(1) io_hlt();

5
src/kernel/proc.c Normal file
View File

@ -0,0 +1,5 @@
#include "../include/proc.h"
void proc_install(){
}

View File

@ -3,14 +3,20 @@
#include "../include/isr.h"
#include "../include/description_table.h"
#include "../include/graphics.h"
#include "../include/io.h"
extern asm_syscall_handler();
void syscall_handler(registers_t regs){
io_cli();
printf("Syscall is enable.\n");
if(regs.eax == 0x01){
putchar((regs.edx));
}
io_sti();
return;
}
void syscall_install(){
idt_use_reg(80, syscall_handler);
register_interrupt_handler(31,asm_syscall_handler);
}

View File

@ -21,6 +21,8 @@ extern page_directory_t *kernel_directory;
extern void switch_to(struct context *prev, struct context *next);
extern void taskX_switch(struct context *prev, struct context *next);
int now_pid = 0;
int can_sche = 1;
@ -140,7 +142,6 @@ void task_kill(int pid) {
argv->state = TASK_DEATH;
printf("Taskkill process PID:%d Name:%s\n", current->pid, current->name);
printf("Task [%s] exit code: -130.\n", argv->name);
io_sti();
kfree(argv);
struct task_struct *head = running_proc_head;
@ -156,41 +157,39 @@ void task_kill(int pid) {
}
}
void schedule() {
void schedule(registers_t *reg) {
io_cli();
if (current && can_sche) {
if (current->next->state == TASK_SLEEPING) {
change_task_to(current->next->next);
return;
}
change_task_to(current->next);
change_task_to(reg,current->next);
}
}
void change_task_to(struct task_struct *next) {
void change_task_to(registers_t *reg,struct task_struct *next) {
if (current != next) {
struct task_struct *prev = current;
current = next;
page_switch(current->pgd_dir);
set_kernel_stack(current->stack + STACK_SIZE); // 没有 TSACK_SIZE
set_kernel_stack(current->stack + STACK_SIZE);
prev->context.eip = reg->eip;
prev->context.ds = reg->ds;
prev->context.cs = reg->cs;
prev->context.eax = reg->eax;
switch_to(&(prev->context), &(current->context));
reg->ds = current->context.ds;
reg->cs = current->context.cs;
reg->eip = current->context.eip;
reg->eax = current->context.eax;
}
}
void n() {
printf("Hello! User!\n");
for(;;);
}
int32_t user_process(char *path, char *name){
can_sche = 0;
if(path == NULL){
printf("Cannot create process! exec path is NULL\n");
return -1;
}
uint32_t size = vfs_filesize(path);
if(size == -1){
printf("Cannot font exec file\n");
return -1;
}
io_cli();
@ -235,8 +234,6 @@ int32_t user_process(char *path, char *name){
uint32_t main = load_elf(ehdr,page);
printf("Main ADDRESS: %08x\n",main);
uint32_t *stack_top = (uint32_t * )((uint32_t) new_task + STACK_SIZE);
*(--stack_top) = (uint32_t) main;
@ -246,7 +243,7 @@ int32_t user_process(char *path, char *name){
new_task->context.esp = (uint32_t) new_task + STACK_SIZE - sizeof(uint32_t) * 3;
// 设置新任务的标志寄存器未屏蔽中断,很重要
new_task->context.eflags = 0x200;
new_task->context.eflags = (0 << 12 | 0b10 | 1 << 9);
new_task->next = running_proc_head;
page_switch(kernel_directory);
@ -317,7 +314,8 @@ int32_t kernel_thread(int (*fn)(void *), void *arg, char *name) {
void kthread_exit() {
register uint32_t val asm ("eax");
printf("Task exited with value %d\n", val);
printf("Task [PID: %d] exited with value %d\n", current->pid,val);
task_kill(current->pid);
current->state = TASK_DEATH;
while (1);
}
@ -334,14 +332,7 @@ void kill_all_task() {
}
}
#define SA_RPL3 3
void A() {
printf("USE3 HELLO!\n");
asm("hlt");
for(;;);
}
void switch_to_user_mode(uint32_t func) {
io_cli();
@ -365,6 +356,7 @@ void switch_to_user_mode(uint32_t func) {
iframe.ss = GET_SEL(4 * 8, SA_RPL3);
iframe.cs = GET_SEL(3 * 8, SA_RPL3);
//set_tss_ss0(iframe.ss);
iframe.eip = func; //用户可执行程序入口
iframe.eflags = (0 << 12 | 0b10 | 1 << 9);

View File

@ -19,7 +19,7 @@ unsigned int time(void) {
static void timer_handle(registers_t *regs) {
io_cli();
tick++;
schedule();
schedule(regs);
io_sti();
}

View File

@ -1,5 +1,22 @@
#include "../include/desktop.h"
#include "../include/task.h"
#include "../include/printf.h"
void menu_sel(){
int send_ok_box(char *title,char* message){
}
static int desktop_setup(){
for (;;) {
}
return 0;
}
void menu_sel(){
klogf(true,"Launching kernel desktop service...\n");
kernel_thread(desktop_setup,NULL,"CPOS-Desktop");
}

View File

@ -24,6 +24,7 @@ char getc() {
int gets(char *buf, int buf_size) {
int index = 0;
char c;
logk("GETS DEBUG I\n");
while ((c = getc()) != '\n') {
if (c == '\b') {
if (index > 0) {
@ -35,6 +36,7 @@ int gets(char *buf, int buf_size) {
putchar(c);
}
}
logk("GETS DEBUG II\n");
buf[index] = '\0';
putchar(c);
return index;
@ -73,7 +75,7 @@ void cmd_echo(int argc, char **argv) {
void cmd_proc(int argc, char **argv) {
if (argc <= 1) {
printf("[Shell-PROC]: If there are too few parameters.\n");
print_proc();
return;
}
@ -236,7 +238,7 @@ void cmd_sb3(int argc, char **argv) {
print("[Shell-SB3]: If there are too few parameters, please specify the path.\n");
return;
}
wav_player(argv[1]);
// wav_player(argv[1]);
}
void cmd_type(int argc,char ** argv){
@ -261,12 +263,25 @@ void cmd_type(int argc,char ** argv){
printf("%s",buffer);
else printf("Cannot read file.\n");
kfree(buffer);
print("\n");
}
void cmd_exec(int argc,char** argv){
if (argc == 1) {
print("[Shell-EXEC]: If there are too few parameters, please specify the path.\n");
return;
}
if(vfs_filesize(argv[1]) == -1){
print("Cannot found exec file.\n");
return;
}
char buf[1024];
sprintf(buf,"User-%s ",argv[1]);
int32_t pid = user_process(argv[1],buf);
klogf(pid != -1,"Launching user task PID:%d Name:%s\n",pid,buf);
}
void cmd_disk(int argc, char **argv) {
if (argc > 1) {
if (!strcmp("list", argv[1])) {
@ -351,10 +366,6 @@ char *user() {
}
void setup_shell() {
char *user1 = "default";//user();
screen_clear();
printf("Welcome to %s %s (CPOS Kernel i386)\n"
"\n"
" * SourceCode: https://github.com/xiaoyi1212/CoolPotOS\n"
@ -363,7 +374,7 @@ void setup_shell() {
" System information as of %s \n"
"\n"
" Processes: %d\n"
" Users logged in: %s\n"
" Users logged in: default\n"
" Memory usage: %d B \n"
"\n"
"Copyright 2024 XIAOYI12 (Build by GCC i686-elf-tools)\n"
@ -371,15 +382,14 @@ void setup_shell() {
,OS_VERSION
,get_date_time()
,get_procs()
,user1
,memory_usage());
char com[MAX_COMMAND_LEN];
char *argv[MAX_ARG_NR];
int argc = -1;
char *buffer[255];
while (1) {
logk("DEBUG I\n");
if(hasFS) vfs_getPath(buffer);
else{
buffer[0] = 'n';
@ -389,8 +399,12 @@ void setup_shell() {
buffer[4] = 's';
buffer[5] = '\0';
}
printf("\03343cd80;%s@localhost: \0334169E1;%s\\\033c6c6c6;$ ", user1, buffer);
logk("DEBUG II\n");
printf("\03343cd80;default@localhost: \0334169E1;%s\\\033c6c6c6;$ ", buffer);
logk("DEBUG III\n");
if (gets(com, MAX_COMMAND_LEN) <= 0) continue;
logk("DEBUG IIII\n");
argc = cmd_parse(com, argv, ' ');
if (argc == -1) {
@ -426,6 +440,8 @@ void setup_shell() {
cmd_cd(argc, argv);
else if (!strcmp("sb3", argv[0]))
cmd_sb3(argc, argv);
else if (!strcmp("exec",argv[0]))
cmd_exec(argc,argv);
else if (!strcmp("help", argv[0]) || !strcmp("?", argv[0]) || !strcmp("h", argv[0])) {
print("-=[CoolPotShell Helper]=-\n");
print("help ? h Print shell help info.\n");
@ -442,6 +458,7 @@ void setup_shell() {
print("disk[list|<ID>|cg<ID>]List or view disks.\n");
print("cd <path> Change shell top directory.\n");
print("sb3 <name> Player a wav sound file.\n");
print("exec <path> Execute a application.\n");
} else printf("\033ff3030;[Shell]: Unknown command '%s'.\033c6c6c6;\n", argv[0]);
}
}

85
src/sysapp/soundtest.c Normal file
View File

@ -0,0 +1,85 @@
#include "../include/soundtest.h"
#include "../include/sb16.h"
#include "../include/printf.h"
#include "../include/timer.h"
#define buffer_len 32768
static const char l[] = " QQffQQLLLfLLDDQQQfff rrff``UU QQDDQQLLLfLLDD<<f333 r\x98rf`r`U "
"<<<9<LUUU\x80U\x80UL[[rLLL rrff`frr <<<93U999`9U3+--9&&& rrff``UU";
static int s(const char *a, int b, int c) {
return c / a[b];
}
static int d(int a) {
return a & 1 ? -1 : 1;
}
static int e(int a, int b, int t) {
return t >> 22 & 1 ? b : a;
}
static int r(int a, int t) {
return 112 >> (a >> 20 & 7) & 1 ? t >> 14 & 15 : (t >> 17 & 1) * 8;
}
static int b(const char *a, int p, int t) {
return (s(a, r(t, t) + p * 16, t << 2) * (254 >> (t >> 20 & 7) & 1)) & 20;
}
static int q(int a) {
return 120 >> (a >> 20 & 7) & 1 ? 1 : (-a >> 16 & 1);
}
static int g(int t) {
int p = 178 >> (t >> 19 & 7) & 1;
int n =
b("rrLr99rrUUrU99rULLyL<<yy``\x80`@@\x80`", p, t) * 2 +
b("qqKq88qqTTqT88qTKKxK;;xx__\x7f_??\x7f_", p, t) +
(s("rf[<r`L@\x98\x88yQ\x80\146UL", (t & 3) + (t >> 17 & 1) * 4 + p * 8, t << 2) & 7) *
(t * d(t >> 16) >> 12 & 15 ^ e(0, 5, t)) * 3 / 4 +
((s("rf[L<9-\x1e&-3&-3-3\xab\x98\x90r`UH0+&9+&\x1d&+\x98\x88yfQL<(3<D3<3-(&"
"09HL`ULU`r`UL@9",
(t >> 13 & 31) + p * 32, t << 5 - (t >> 11 & 3)) |
t >> 8) *
q(t) &
31) +
(((s(l, t >> 14 & 127, t << 6) & s(l, t >> 14 & 127, (t * e(89 / 88, 499 / 498, t)) << 6)) *
(63486 >> (t >> 15 & 15) & 1) * (102 >> (t >> 20 & 7) & 1)) &
e(42, 32, t)) +
((((253507989 >> (t >> 6 & 31)) * (1 >> (t >> 11 & 3)) * (19593 >> (t >> 13 & 15) & 1) & 1) *
50) +
((((t * t / 21 + t & (t >> 3)) | t >> 7 | t >> 8) - 7) * (3 >> (t >> 11 & 3) & 1) *
(2450526224 >> (t >> 13 & 31) & 1) &
31) *
5 / 2) *
(112 >> (t >> 20 & 7) & 1);
return n;
}
static int F, G;
static const int T = 125000;
static const int K = 10;
static uint8_t gen(int t) {
if (t < (128 - F) * K) return 128 - t / K;
t -= (128 - F) * K;
if (t < T) return g(t);
t -= T;
if (t < (128 - G) * K) return G + t / K;
return 128;
// return g(t);
}
void sound_test() {
logk("sound test has been started\n");
F = g(0);
G = g(T);
const int total = T + ((128 - F) + (128 - G)) * K;
sb16_open();
sb16_set_volume(128);
uint8_t *buffer = kmalloc(buffer_len);
for (int offset = 0;; offset++) {
if (offset * buffer_len >= total) break;
for (int i = 0; i < buffer_len; i++)
buffer[i] = gen(i + offset * buffer_len);
sb16_write(buffer, buffer_len);
}
sb16_close();
kfree(buffer);
}

View File

@ -1,5 +1,6 @@
#include "../include/elf.h"
#include "../include/common.h"
#include "../include/printf.h"
#define MAX(a, b) a > b ? a : b

31
src/util/klog.c Normal file
View File

@ -0,0 +1,31 @@
/*
* PlantOS - KernelLogger
* Copyright by copi143
*/
#include "../include/klog.h"
#include "../include/printf.h"
const char *_log_basename_(const char *path) {
static char name[128];
int i = 0;
while (path[i])
i++;
for (i--; i >= 0; i--) {
if (path[i] == '/' || path[i] == '\\') break;
}
return path + i + 1;
}
void klogf(bool isok,char* fmt,...){
int len;
va_list ap;
va_start(ap, fmt);
char *buf[1024] = {0};
len = vsprintf(buf, fmt, ap);
if(isok){
printf("[ \03300ee76;OK\033c6c6c6; ]: %s",buf);
} else{
printf("[\033ee6363;FAILED\033c6c6c6;]: %s",buf);
}
}

View File

@ -304,20 +304,6 @@ void printf(const char *formet, ...) {
va_end(ap);
}
void klogf(bool isok,char* fmt,...){
int len;
va_list ap;
va_start(ap, fmt);
char *buf[1024] = {0};
len = vsprintf(buf, fmt, ap);
if(isok){
printf("[ \03300ee76;OK\033c6c6c6; ]: %s",buf);
} else{
printf("[\033ee6363;FAILED\033c6c6c6;]: %s",buf);
}
}
void screen_clear(){
if(vbe_status){
vbe_clear();
@ -347,6 +333,6 @@ void logkf(char *formet,...){
}
void logk(char *message){
for (size_t i = 0; i < strlen(message); i++);
//write_serial(message[i]);
for (size_t i = 0; i < strlen(message); i++)
write_serial(message[i]);
}