Add: launch elf process in paging

This commit is contained in:
brice.boisson 2023-09-25 11:38:14 +09:00
parent edb93e3967
commit 62837e9c44
11 changed files with 83 additions and 138 deletions

View File

@ -3,8 +3,9 @@
#include <types.h> #include <types.h>
#include "serial.h" #include "serial.h"
#include "debug.h" #include "debug.h"
#include "paging.h"
int load_elf(char *elf_data_start) int load_elf(char *elf_data_start, int uid)
{ {
DEBUG_INFO("LOAD ELF: ", elf_data_start); DEBUG_INFO("LOAD ELF: ", elf_data_start);
@ -19,8 +20,15 @@ int load_elf(char *elf_data_start)
struct elf_header_table *elf_header_table = (struct elf_header_table *) (elf_data_start + elf_header->program_header_table_offset + sizeof(struct elf_header_table) * i); struct elf_header_table *elf_header_table = (struct elf_header_table *) (elf_data_start + elf_header->program_header_table_offset + sizeof(struct elf_header_table) * i);
u32 segment_type = elf_header_table->segment_type; u32 segment_type = elf_header_table->segment_type;
if (segment_type == LOAD && elf_header_table->p_vaddr < 0x100000) DEBUG_INFO("ELF type : %d", segment_type == LOAD);
if (segment_type == LOAD && elf_header_table->p_vaddr >= 0x6000000)
{ {
for (u32 mem = (elf_header_table->p_vaddr >> 12); mem < ((elf_header_table->p_vaddr + elf_header_table->p_filesz) >> 12) + 1; mem += 1)
{
DEBUG_INFO("ELF ALLOCATE %d: ", mem << 12);
allocate_new_page(uid, mem << 12);
}
memcpy(elf_header_table->p_vaddr, elf_data_start + elf_header_table->p_offset, elf_header_table->p_filesz); memcpy(elf_header_table->p_vaddr, elf_data_start + elf_header_table->p_offset, elf_header_table->p_filesz);
if (elf_header_table->p_filesz < elf_header_table->p_memsz) if (elf_header_table->p_filesz < elf_header_table->p_memsz)

View File

@ -46,6 +46,6 @@ enum elf_segment_type {
NOTE = 4 NOTE = 4
}; };
int load_elf(char *elf_data_start); int load_elf(char *elf_data_start, int uid);
#endif ELF_H #endif ELF_H

View File

@ -14,7 +14,9 @@ void *memcpy(void *dest, const void *src, size_t n)
char *d = dest; char *d = dest;
for (size_t i = 0; i < n; i++) for (size_t i = 0; i < n; i++)
{
*d++ = *s++; *d++ = *s++;
}
return dest; return dest;
} }

View File

@ -7,7 +7,7 @@
void isr_default_int(void) void isr_default_int(void)
{ {
DEBUG_INFO("An INT has been raised, entering default interrupt handler."); // DEBUG_INFO("An INT has been raised, entering default interrupt handler.");
} }
void isr_page_fault(void) void isr_page_fault(void)

View File

@ -22,44 +22,8 @@ void main(void)
char *data_end = _binary_a_out_end; char *data_end = _binary_a_out_end;
size_t data_size = (size_t)_binary_a_out_size; size_t data_size = (size_t)_binary_a_out_size;
create_process(0, data_start);
create_new_userland_page(0); switch_to_process(0);
DEBUG_INFO("PAGE DIR: %d", userland_data->userland_data[0].page_directories);
struct page_table_entry *ttt = userland_data->userland_data[0].page_directories[20].address << 12;
DEBUG_INFO("PAGE DIR: %d", ttt->address);
asm volatile (" \
mov $0x3020000, %eax \n \
mov %eax, %cr3");
// DEBUG_INFO("BIN %b", 0x13000000);
allocate_new_page(0, 0x32400000);
// asm volatile (" \
// xor $0x80000000, %eax \n \
// mov %eax, %cr0");
// u32 *data2 = 50466816;
// DEBUG_INFO("BIN %b", *data2);
// u32 *data = 83886080;
// DEBUG_INFO("BIN %b", *data);
// struct page_directory_entry *var = 0x3020000;
// DEBUG_INFO("address %d", var[76].address);
// u32 *var = 0x13000000;
u32 *var = 0x32403400;
*var = 10;
DEBUG_INFO("%d", *var);
// asm volatile (" \
// mov %0, %%eax \n \
// mov %%eax, %%cr3" : "=m" (userland_data->userland_data[0].page_directories));
// load_elf(data_start);
// launch_process(0x28, 0x30000, 0x20, 0x18, 0x20);
for (;;) for (;;)
{ {

View File

@ -3,7 +3,7 @@
#include "tss.h" #include "tss.h"
#include "debug.h" #include "debug.h"
void launch_process(int tss, int memory_start, int userland_stack, int userland_code, int userland_data) void launch_process(u16 tss, int memory_start, int userland_stack, int userland_code, u16 userland_data)
{ {
// Setting DPL and GDT bits // Setting DPL and GDT bits
userland_stack += 3; userland_stack += 3;
@ -13,29 +13,22 @@ void launch_process(int tss, int memory_start, int userland_stack, int userland_
DEBUG_INFO("LAUCHING USER LAND PROCESS"); DEBUG_INFO("LAUCHING USER LAND PROCESS");
asm volatile (" \n \ asm volatile (" \n \
movw %0, %%ax \n \ mov %0, %%ax \n \
ltr %%ax \n \ ltr %%ax \n \
movw %%ss, %1 \n \ movw %%ss, %1 \n \
movl %%esp, %2 \n \ movl %%esp, %2 \n \
cli \n \ movw $0x23, %%ax \n \
push %3 \n \
push %4 \n \
pushfl \n \
popl %%eax \n \
orl $0x200, %%eax \n \
and $0xffffbfff, %%eax \n \
push %%eax \n \
push %5 \n \
push $0x30000 \n \
movl $0x20000, %6 \n \
movw %7, %%ax \n \
movw %%ax, %%ds \n \ movw %%ax, %%ds \n \
iret" : "=m" (tss), movw %%ax, %%es \n \
movw %%ax, %%fs \n \
movw %%ax, %%gs \n \
mov %%esp, %%eax \n \
push $0x23 \n \
push $0x7FFFFFFF \n \
pushfl \n \
push $0x1B \n \
push $0x6000000 \n \
iret" : "+r" (tss),
"=m" (user_land_tss.ss0), "=m" (user_land_tss.ss0),
"=m" (user_land_tss.esp0), "=m" (user_land_tss.esp0));
"=m" (userland_stack),
"=m" (memory_start),
"=m" (userland_code),
"=m" (user_land_tss.esp0),
"=m" (userland_data));
} }

View File

@ -1,6 +1,8 @@
#ifndef LAUNCH_PROCESS_H #ifndef LAUNCH_PROCESS_H
#define LAUNCH_PROCESS_H #define LAUNCH_PROCESS_H
void launch_process(int tss, int memory_start, int userland_stack, int userland_code, int userland_data); #include <types.h>
void launch_process(u16 tss, int memory_start, int userland_stack, int userland_code, u16 userland_data);
#endif /* !LAUNCH_PROCESS_H */ #endif /* !LAUNCH_PROCESS_H */

Binary file not shown.

View File

@ -218,45 +218,6 @@ int create_new_userland_page(int uid)
} }
} }
// int find_empty_page(struct page_directory_entry *page_dir)
// {
// for (int i = 20; i < 1024; i++)
// {
// if (page_dir[i].address == 0)
// {
// int page_table_entry = 0;
// if (i != 20)
// {
// if ((page_table_entry = find_empty_page_table(page_dir[i - 1].address)) != -1)
// return (i - 1, )
// }
// }
// }
// return -1;
// }
// int find_empty_page_table(struct page_table_entry *page_table)
// {
// for (int i = 0; i < 1024; i++)
// {
// if (page_table[i].address == 0)
// {
// return i;
// }
// }
// return -1;
// }
// int find_last_page_dir_entry(struct page_directory_entry *page_dir)
// {
// for (int i = NB_KERNEL_PAGE_DIR; i < 1024; i++)
// {
// if (page_dir[i].address == 0)
// return i - 1;
// }
// return -1;
// }
int allocate_new_page(int uid, int address) int allocate_new_page(int uid, int address)
{ {
struct page_directory_entry *userland_page_dir = userland_data->userland_data[uid].page_directories; struct page_directory_entry *userland_page_dir = userland_data->userland_data[uid].page_directories;
@ -269,25 +230,24 @@ int allocate_new_page(int uid, int address)
// CPU does't use page table when decoding page table, so it need the physical address // CPU does't use page table when decoding page table, so it need the physical address
int new_page_table_real = (int) userland_data->userland_data[uid].page_table[dir_address].address; int new_page_table_real = (int) userland_data->userland_data[uid].page_table[dir_address].address;
struct page_table_entry *new_page_table = USERLAND_BASE_ADDRESS + dir_address * 1024 * 4; DEBUG_INFO("PAGE TABLE REAL ADDRESS %d", new_page_table_real << 12);
struct page_table_entry *new_page_table = USERLAND_BASE_ADDRESS + dir_address * 1024 * 4 + table_address * 4;
if (userland_page_dir[dir_address].address != 0 && new_page_table->address != 0) if (userland_page_dir[dir_address].address != 0 && new_page_table->address != 0)
clear_page(new_page_table->address); clear_page(new_page_table->address);
if (userland_page_dir[dir_address].address == 0) if (userland_page_dir[dir_address].address == 0)
{ {
DEBUG_INFO("NEW DIR ENTRY");
userland_page_dir[dir_address] = create_page_directory_entry((struct page_directory_param) { userland_page_dir[dir_address] = create_page_directory_entry((struct page_directory_param) {
.P = 1, .R_W = 1, .U = 1, .PWT = 0, .PCD = 0, .P = 1, .R_W = 1, .U = 1, .PWT = 0, .PCD = 0,
.A = 0, .PS = 0, .address = new_page_table_real << 12}); .A = 0, .PS = 0, .address = new_page_table_real << 12});
} }
for (struct page_table_entry *i = new_page_table; i < new_page_table + 1024; i++)
{
int avl_address = find_page_avl(); int avl_address = find_page_avl();
set_page(avl_address); set_page(avl_address);
*i = create_page_table_entry((struct page_table_param) { *new_page_table = create_page_table_entry((struct page_table_param) {
.P = 1, .R_W = 1, .U = 1, .PWT = 0, .PCD = 0, .P = 1, .R_W = 1, .U = 1, .PWT = 0, .PCD = 0,
.A = 0, .D = 0, .PAT = 0, .G = 0, .A = 0, .D = 0, .PAT = 0, .G = 0,
.address = (avl_address >> 12)}); .address = (avl_address >> 12)});
}
} }

View File

@ -1,28 +1,43 @@
#include "userland.h" #include "userland.h"
#include "debug.h"
#include "paging.h"
#include "launch_process.h"
struct userlands_data *userland_data = 0x3020000; struct userlands_data *userland_data = 0x3020000;
// void userland(void) int create_process(int uid, char *data_start)
// { {
// int res = 0; create_new_userland_page(uid);
// // asm ("mov $1, %0" : "=r" (res));
// // asm volatile ("movl $2, %eax; int $0x30"); DEBUG_INFO("CREATE PROCESS");
// // asm("movl %%eax,%0" : "=r"(res)); void *process_page_dir_adress = userland_data->userland_data[uid].page_directories;
// // asm volatile ("int $0x30");
// char *str = (void *) 0x30100; // load cr3
// str[0] = 'H'; asm volatile (" \
// str[1] = 'e'; mov %0, %%eax \n \
// str[2] = 'l'; mov %%eax, %%cr3" : "+r" (process_page_dir_adress));
// str[3] = 'l';
// str[4] = 'o'; // Allocate stack
// str[5] = '\0'; allocate_new_page(uid, 0x7FFFF000);
// asm volatile ("mov $1, %%eax; movl $0x30100, %%ebx; int $0x30; movl %%eax, %1" : "=m" (str), "=r" (res));
// // asm volatile ("mov $1, %%eax; movl %0, %%ebx; int $0x30" : "=m" (str)); // TODO : create data seg by process
// // asm ("mov $1, %eax; int $0x30"); // load elf
// // asm ("movl %0, %eax; int $0x30" : "=m" (res)); load_elf(data_start, uid);
// // asm ("movl %eax, %eax; int $0x30");
// // asm ("movl $28, %eax; movl $5, %ebx; int $0x30"); return 0;
// // asm ("movl $43, %eax; movl $7, %ebx; int $0x30"); }
// while (1);
// return; /* never go there */ int switch_to_process(int uid)
// } {
DEBUG_INFO("SWITCHING TO PROCESS");
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));
// TODO : once data by process has been implemented, load the right one
launch_process(0x28, 0x6000000, 0x20, 0x18, 0x20);
}

View File

@ -20,6 +20,7 @@ struct userlands_data {
extern struct userlands_data *userland_data; extern struct userlands_data *userland_data;
void userland(void); int create_process(int uid, char *data_start);
int switch_to_process(int uid);
#endif /* !USERLAND_H */ #endif /* !USERLAND_H */