Skip to content
Snippets Groups Projects
Commit 7d283ca3 authored by Byron Lathi's avatar Byron Lathi
Browse files

execute user code

can't halt yet, but that's fine.
parent b06ff595
No related branches found
No related tags found
No related merge requests found
#include <types.h>
#include <lib.h>
#include <x86_desc.h>
#include <Devices/Storage/ext2.h>
#include <paging.h>
#include "execute.h"
#define PCB_MASK 0xffffe000
// TEMPORARY!!!
static uint32_t pid = 0;
void get_ret_addr()
{
uint32_t ret_eip;
// get the return address from the stack.
asm volatile (" \n\
mov 20(%%esp), %0"
: "=r"(ret_eip));
// add 1 so it skips over the iret;
ret_eip++;
// don't pass in pcb pointers cause this should be easy to call from asm
pcb_t* pcb = (pcb_t*)(_8MB - _8KB - _8KB*pid);
pcb->execute_return = ret_eip;
}
pcb_t* get_pcb_ptr()
{
pcb_t* pcb;
/* get the PCB pointer passed from assembly */
asm volatile(" \n\
andl %%esp, %%eax \n\
movl %%eax, %0"
: "=r"(pcb)
: "a"(PCB_MASK)
);
return pcb;
}
int32_t _sys_exec(uint8_t* name)
{
printf("In _sys_exec\n");
......@@ -36,14 +72,54 @@ int32_t _sys_exec(uint8_t* name)
map_large(EXEC_BASE_ADDR, _8MB + pid * _4MB);
ext2_read_data(parsed_inode, 0, (uint16_t*)EXEC_LOAD_ADDR);
printf("Some code for you: ");
/* populate tss */
tss.ss0 = KERNEL_DS;
tss.esp0 = _8MB - _8KB*pid - 4;
pcb_t* pcb = (pcb_t*)(_8MB - _8KB - _8KB*pid);
pcb->pid = pid;
pcb->parent_pid = get_pcb_ptr()->pid;
/* increment pid and run process; decrement it after termination */
pid++;
uint32_t status;
printf("Some code for you: %x\n", header->e_entry);
int i;
for (i = 0; i < 16; i++) {
printf("%x ", *((uint8_t*)(header->e_entry) + i));
}
printf("\n");
return 1;
printf("gonna execute now, wish me luck! %x\n", header->e_entry);
/* push use data segment, user stack pointer,
* flags, user code segment, entry point, then iret */
asm volatile(" \n\
movl %%ebp, %0 \n\
movl %%esp, %1 \n\
pushl $0x2B \n\
pushl $0x83ffffc \n\
pushfl \n\
pop %%eax \n\
orl $0x200, %%eax \n\
push %%eax \n\
pushl $0x23 \n\
pushl %3 \n\
call get_ret_addr \n\
iret \n\
pop %2"
: "=m"(pcb->parent_ebp), "=m"(pcb->parent_esp), "=m"(status)
: "r"(header->e_entry)
: "%eax"
);
pid--;
return status;
}
int32_t _sys_exit()
......
......@@ -49,7 +49,7 @@
#define EI_NIDENT 16
typedef struct {
typedef struct elf_header{
uint8_t e_ident[EI_NIDENT];
uint16_t e_type;
uint16_t e_machine;
......@@ -66,6 +66,14 @@ typedef struct {
uint16_t e_shstrndx;
} elf_header_t;
typedef struct pcb {
uint32_t pid;
uint32_t parent_pid;
uint32_t parent_ebp;
uint32_t parent_esp;
uint32_t execute_return;
} pcb_t;
int32_t _sys_exec(uint8_t* name);
int32_t _sys_exit();
......
......@@ -13,6 +13,10 @@ int32_t sys_read(int32_t fd, const void* buf, int32_t len)
int32_t sys_write(int32_t fd, const void* buf, int32_t len)
{
printf("syswrite\n");
int i;
for (i = 0; i < len; i++) {
putc(*((uint8_t*)buf + i));
}
return -1;
}
......
......@@ -103,8 +103,6 @@ void entry(unsigned long magic, unsigned long addr) {
init_paging();
kmap_large(fb_addr, fb_addr);
printf("Does this work?\n");
i8259_init();
interrupts_init();
i8259_enable_irq(1);
......@@ -119,7 +117,6 @@ void entry(unsigned long magic, unsigned long addr) {
ata_read_sectors(ATA_MASTER, 0, 1, mbr.data);
ext2_init(mbr.partitions[0].lba_start);
int32_t error = sys_exec((uint8_t*)"/Programs/bin/hello");
if (error) {
printf("Error executing file: %d\n", error);
......
......@@ -8,6 +8,7 @@
#define _4MB 0x400000
#define _8MB 0x800000
#define _8KB 0x2000
typedef struct bmp {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment