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

add file operations, clean up syscall and execute

parent 7d283ca3
No related branches found
No related tags found
No related merge requests found
#include "types.h"
extern int32_t sys_write(int32_t fd, const void* buf, int32_t len);
\ No newline at end of file
extern int32_t sys_write(int32_t fd, const void* buf, int32_t len);
extern int32_t sys_exec(uint8_t* name);
\ No newline at end of file
......@@ -76,7 +76,6 @@ uint32_t keyboard_handle_irq()
last_key = normal_map[scancode];
}
flag = 1;
printf("%c", last_key);
}
} else {
scancode &= ~0x80;
......
#include <types.h>
#include <lib.h>
#include "terminal.h"
int32_t terminal_read(int32_t fd, void* buf, int32_t nbytes){
return -1;
}
int32_t terminal_write(int32_t fd, const void* buf, int32_t nbytes)
{
int i;
for (i = 0; i < nbytes; i++) {
putc(*(uint8_t*)(buf + i));
}
return 0;
}
int32_t terminal_open(const uint8_t* filename)
{
return -1;
}
int32_t terminal_close(int32_t fd)
{
return -1;
}
#include <types.h>
#include <lib.h>
#include <x86_desc.h>
#include <Devices/Storage/ext2.h>
#include <paging.h>
#include <Devices/Storage/ext2.h>
#include <Devices/terminal.h>
#include "execute.h"
......@@ -11,6 +12,10 @@
// TEMPORARY!!!
static uint32_t pid = 0;
static fops_t stdin_ops = {terminal_open, terminal_close, terminal_read, NULL};
static fops_t stdout_ops = {terminal_open, terminal_close, NULL, terminal_write};
void get_ret_addr()
{
uint32_t ret_eip;
......@@ -46,9 +51,6 @@ pcb_t* get_pcb_ptr()
int32_t _sys_exec(uint8_t* name)
{
printf("In _sys_exec\n");
union {
uint16_t data16[512];
uint8_t data8[1024];
......@@ -80,21 +82,15 @@ int32_t _sys_exec(uint8_t* name)
pcb->pid = pid;
pcb->parent_pid = get_pcb_ptr()->pid;
pcb->file_desc_array[0].file_ops = &stdin_ops;
pcb->file_desc_array[1].file_ops = &stdout_ops;
/* 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");
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\
......@@ -122,8 +118,33 @@ int32_t _sys_exec(uint8_t* name)
return status;
}
int32_t _sys_exit()
int32_t _sys_exit(uint32_t status)
{
printf("in _sys_exit\n");
pcb_t* pcb = get_pcb_ptr();
/* restart the shell if the user quits the last layer */
if (!pcb->execute_return) {
if (pcb->pid == 0) {
pid = 0; // this feels like a hack...
_sys_exec((uint8_t*)"stop");
}
}
// unmap the current process memory before going back to the execute that called it
// remap original program memory
map_large(EXEC_BASE_ADDR, (_8MB + (get_pcb_ptr()->parent_pid)*_4MB));
tss.esp0 = pcb->parent_esp;
/* give the control back */
asm volatile(" \
mov %0, %%ebp \n\
mov %1, %%esp \n\
push %%ebx \n\
jmp *%%eax"
:
:"m"(pcb->parent_ebp), "m"(pcb->parent_esp), "b"(status), "a"(pcb->execute_return)
);
return -1;
}
......@@ -66,15 +66,36 @@ typedef struct elf_header{
uint16_t e_shstrndx;
} elf_header_t;
/* file operations struct */
typedef struct fops_t {
int32_t (*open)(const uint8_t* filename);
int32_t (*close)(int32_t fd);
int32_t (*read)(int32_t fd, void* buf, int32_t nbytes);
int32_t (*write)(int32_t fd, const void* buf, int32_t nbytes);
} fops_t;
/* file descriptors struct */
typedef struct file_desc_t {
fops_t* file_ops;
uint32_t inode;
uint32_t file_pos;
uint32_t flags;
} file_desc_t;
typedef struct pcb {
uint32_t pid;
uint32_t parent_pid;
uint32_t parent_ebp;
uint32_t parent_esp;
file_desc_t file_desc_array[8];
int32_t is_vidmapped;
uint8_t args[128];
uint32_t execute_return;
int32_t pid;
int32_t parent_pid;
uint32_t parent_esp;
uint32_t parent_ebp;
} pcb_t;
int32_t _sys_exec(uint8_t* name);
int32_t _sys_exit();
int32_t _sys_exit(uint32_t status);
pcb_t* get_pcb_ptr();
#endif
\ No newline at end of file
......@@ -4,20 +4,18 @@
#include "syscalls.h"
#include "execute.h"
int32_t sys_read(int32_t fd, const void* buf, int32_t len)
int32_t sys_read(int32_t fd, void* buf, int32_t len)
{
printf("sysread\n");
return -1;
pcb_t* pcb = get_pcb_ptr();
pcb->file_desc_array[fd].file_ops->read(fd, buf, len);
return 0;
}
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;
pcb_t* pcb = get_pcb_ptr();
pcb->file_desc_array[fd].file_ops->write(fd, buf, len);
return 0;
}
int32_t sys_open(uint8_t* name)
......@@ -35,6 +33,6 @@ int32_t sys_exec(uint8_t* name)
return _sys_exec(name);
}
int32_t sys_exit() {
return _sys_exit();
int32_t sys_exit(uint32_t status) {
return _sys_exit(status);
}
\ No newline at end of file
......@@ -11,11 +11,11 @@
#define SYS_EXEC 5
#define SYS_EXIT 6
int32_t sys_read(int32_t fd, const void* buf, int32_t len);
int32_t sys_read(int32_t fd, void* buf, int32_t len);
int32_t sys_write(int32_t fd, const void* buf, int32_t len);
int32_t sys_open(uint8_t* name);
int32_t sys_close(int32_t fd);
int32_t sys_exec(uint8_t* name);
int32_t sys_exit();
int32_t sys_exit(uint32_t status);
#endif
......@@ -117,7 +117,7 @@ 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");
int32_t error = sys_exec((uint8_t*)"/Programs/bin/exectest");
if (error) {
printf("Error executing file: %d\n", error);
}
......
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