diff --git a/src/isr.c b/src/isr.c index e3ea96c..64a9311 100644 --- a/src/isr.c +++ b/src/isr.c @@ -4,6 +4,7 @@ #include "pic_controler.h" #include "debug.h" #include "syscall.h" +#include "userland.h" void isr_default_int(void) { @@ -17,7 +18,15 @@ void isr_page_fault(void) void isr_clock_int(void) { - DEBUG_INFO("Enterring clock interrupt handler."); + DEBUG_INFO("Clock interrupt handler."); + for (int i = (userland_data->curent_process + 1) % 128; i != userland_data->curent_process; i = (i + 1) % 128) + { + if (userland_data->active_process[i / 32] & (1 << (i % 32))) + { + switch_to_process(i); + return; + } + } } void isr_kbd_int(void) diff --git a/src/kernel.c b/src/kernel.c index e31e104..4125271 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -22,8 +22,12 @@ void main(void) char *data_end = _binary_a_out_end; size_t data_size = (size_t)_binary_a_out_size; + u16 tss = 0x28; + create_process(0, data_start); - switch_to_process(0); + create_process(1, data_start); + userland_data->active_process[0] |= 1 << (1 % 32); + switch_to_ring_3(0); for (;;) { diff --git a/src/launch_process.c b/src/launch_process.c index 18ca823..fe3869b 100644 --- a/src/launch_process.c +++ b/src/launch_process.c @@ -2,32 +2,33 @@ #include "tss.h" #include "debug.h" +#include "userland.h" -void launch_process(u16 tss, int memory_start, int userland_stack, int userland_code, u16 userland_data) +void launch_process(u16 tss, int memory_start, int userland_stack, int userland_code, u16 userland_data_seg) { // Setting DPL and GDT bits userland_stack += 3; userland_code += 3; - userland_data += 3; + userland_data_seg += 3; DEBUG_INFO("LAUCHING USER LAND PROCESS"); - asm volatile (" \n \ - mov %0, %%ax \n \ - ltr %%ax \n \ - movw %%ss, %1 \n \ - movl %%esp, %2 \n \ - movw $0x23, %%ax \n \ - movw %%ax, %%ds \n \ - movw %%ax, %%es \n \ - movw %%ax, %%fs \n \ - movw %%ax, %%gs \n \ - mov %%esp, %%eax \n \ - push $0x23 \n \ - push $0x7FFFEFFF \n \ - pushfl \n \ - push $0x1B \n \ - push $0x6000000 \n \ + asm volatile (" \n \ + mov %0, %%ax \n \ + ltr %%ax \n \ + movw %%ss, %1 \n \ + movl %%esp, %2 \n \ + movw $0x23, %%ax \n \ + movw %%ax, %%ds \n \ + movw %%ax, %%es \n \ + movw %%ax, %%fs \n \ + movw %%ax, %%gs \n \ + mov %%esp, %%eax \n \ + push $0x23 \n \ + push $0x7FFFEFFF \n \ + pushfl \n \ + push $0x1B \n \ + push $0x6000000 \n \ iret" : "+r" (tss), "=m" (user_land_tss.ss0), "=m" (user_land_tss.esp0)); diff --git a/src/pic_controler.c b/src/pic_controler.c index 92f27ad..3c9911e 100644 --- a/src/pic_controler.c +++ b/src/pic_controler.c @@ -101,6 +101,8 @@ void pic_init(void) IRQ_set_mask(i); } + DEBUG_INFO("Unmasking IRQ 0 - Clock"); + IRQ_clear_mask(0); DEBUG_INFO("Unmasking IRQ 1 - Keyboard"); IRQ_clear_mask(1); diff --git a/src/userland.c b/src/userland.c index e154f90..44f5afc 100644 --- a/src/userland.c +++ b/src/userland.c @@ -30,6 +30,21 @@ int create_process(int uid, char *data_start) return 0; } +int switch_to_ring_3(int uid){ + DEBUG_INFO("SWITCHING TO RING 3"); + void *process_page_dir_adress = userland_data->userland_data[uid].page_directories; + + // load cr3 + asm volatile (" \ + mov %0, %%eax \n \ + mov %%eax, %%cr3" : "+r" (process_page_dir_adress)); + + userland_data->curent_process = uid; + userland_data->active_process[uid / 32] |= 1 << (uid % 32); + + launch_process(0x28, 0x6000000, 0x20, 0x18, 0x20); +} + int switch_to_process(int uid) { DEBUG_INFO("SWITCHING TO PROCESS"); @@ -40,6 +55,8 @@ int switch_to_process(int uid) mov %0, %%eax \n \ mov %%eax, %%cr3" : "+r" (process_page_dir_adress)); - // TODO : once data by process has been implemented, load the right one - launch_process(0x28, 0x6000000, 0x20, 0x18, 0x20); + userland_data->curent_process = uid; + userland_data->active_process[uid / 32] |= 1 << (uid % 32); + + return 0; } diff --git a/src/userland.h b/src/userland.h index c89a721..3ebdb84 100644 --- a/src/userland.h +++ b/src/userland.h @@ -10,17 +10,19 @@ struct userland_data { struct page_directory_entry page_directories[1024]; struct page_table_entry page_table[1024]; -}; +} __attribute__((packed)); struct userlands_data { struct userland_data userland_data[128]; - u32 page_dir[4]; -}; + int curent_process; + u32 active_process[4]; +} __attribute__((packed)); extern struct userlands_data *userland_data; int create_process(int uid, char *data_start); +int switch_to_ring_3(int uid); int switch_to_process(int uid); #endif /* !USERLAND_H */