> compile NEMU

221220000 张三
Linux zzy 5.15.146.1-microsoft-standard-WSL2 #1 SMP Thu Jan 11 04:09:03 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
 17:16:20 up 7 days,  2:13,  1 user,  load average: 0.74, 0.65, 0.61
This commit is contained in:
tracer-ics2023
2024-09-24 17:16:23 +08:00
committed by zzy
parent af3ffc7091
commit c6a6cc13d7
10 changed files with 64 additions and 35 deletions

View File

@ -0,0 +1,8 @@
#ifndef __DEVIDE_H__
#define __DEVIDE_H__
#include <common.h>
size_t serial_write(const void *buf, size_t offset, size_t len);
#endif

View File

@ -7,10 +7,10 @@
enum {SEEK_SET, SEEK_CUR, SEEK_END};
#endif
int _fs_open(const char *pathname, int flags, int mode);
size_t _fs_read(int fd, void *buf, size_t len);
size_t _fs_write(int fd, const void *buf, size_t len);
size_t _fs_lseek(int fd, size_t offset, int whence);
int _fs_close(int fd);
int fs_open(const char *pathname, int flags, int mode);
size_t fs_read(int fd, void *buf, size_t len);
size_t fs_write(int fd, const void *buf, size_t len);
size_t fs_lseek(int fd, size_t offset, int whence);
int fs_close(int fd);
#endif

View File

@ -1,4 +1,4 @@
#include <common.h>
#include <device.h>
#if defined(MULTIPROGRAM) && !defined(TIME_SHARING)
# define MULTIPROGRAM_YIELD() yield()
@ -15,7 +15,10 @@ static const char *keyname[256] __attribute__((used)) = {
};
size_t serial_write(const void *buf, size_t offset, size_t len) {
return 0;
for (size_t i = 0; i < len; i++) {
putch(((const char*)buf)[i]);
}
return len;
}
size_t events_read(void *buf, size_t offset, size_t len) {

View File

@ -1,4 +1,5 @@
#include <fs.h>
#include <device.h>
#include <ramdisk.h>
typedef size_t (*ReadFn) (void *buf, size_t offset, size_t len);
@ -25,22 +26,15 @@ size_t invalid_write(const void *buf, size_t offset, size_t len) {
return 0;
}
size_t _write(const void *buf, size_t offset, size_t count) {
for (size_t i = 0; i < count; i++) {
putch(((const char*)buf)[i]);
}
return count;
}
/* This is the information about all files in disk. */
static Finfo file_table[] __attribute__((used)) = {
[FD_STDIN] = {"stdin", 0, 0, invalid_read, invalid_write},
[FD_STDOUT] = {"stdout", 0, 0, invalid_read, _write},
[FD_STDERR] = {"stderr", 0, 0, invalid_read, _write},
[FD_STDOUT] = {"stdout", 0, 0, invalid_read, serial_write},
[FD_STDERR] = {"stderr", 0, 0, invalid_read, serial_write},
#include "files.h"
};
int _fs_open(const char *pathname, int flags, int mode) {
int fs_open(const char *pathname, int flags, int mode) {
int fd;
for (fd = 0; fd < LENGTH(file_table); fd ++) {
if (strcmp(file_table[fd].name, pathname) == 0) {
@ -52,7 +46,7 @@ int _fs_open(const char *pathname, int flags, int mode) {
return -1;
}
size_t _fs_read(int fd, void *buf, size_t len) {
size_t fs_read(int fd, void *buf, size_t len) {
assert(fd >= 0 && fd < LENGTH(file_table));
if (file_table[fd].read != NULL) {
return file_table[fd].read(buf, file_table[fd].open_offset, len);
@ -66,7 +60,7 @@ size_t _fs_read(int fd, void *buf, size_t len) {
return ret;
}
size_t _fs_write(int fd, const void *buf, size_t len) {
size_t fs_write(int fd, const void *buf, size_t len) {
assert(fd >= 0 && fd < LENGTH(file_table));
if (file_table[fd].write != NULL) {
return file_table[fd].write(buf, file_table[fd].open_offset, len);
@ -80,7 +74,7 @@ size_t _fs_write(int fd, const void *buf, size_t len) {
return ret;
}
size_t _fs_lseek(int fd, size_t offset, int whence) {
size_t fs_lseek(int fd, size_t offset, int whence) {
assert(fd >= 0 && fd < LENGTH(file_table));
switch (whence)
{
@ -102,7 +96,7 @@ size_t _fs_lseek(int fd, size_t offset, int whence) {
return file_table[fd].open_offset;
}
int _fs_close(int fd) {
int fs_close(int fd) {
assert(fd >= 0 && fd < LENGTH(file_table));
return 0;
}

View File

@ -15,25 +15,25 @@ static uintptr_t loader(PCB *pcb, const char *filename) {
Elf_Ehdr ehdr;
size_t ret;
int fd = _fs_open(filename, 0, 0);
int fd = fs_open(filename, 0, 0);
assert(fd != -1);
ret = _fs_read(fd, &ehdr, sizeof(ehdr));
ret = fs_read(fd, &ehdr, sizeof(ehdr));
assert(ret == sizeof(ehdr));
assert(ehdr.e_ident[0] == 0x7f && ehdr.e_ident[1] == 'E' && ehdr.e_ident[2] == 'L' && ehdr.e_ident[3] == 'F');
// assert(memcpy(ehdr.e_ident, ELFMAG, SELFMAG) == 0);
// assert(*(uint32_t *)ehdr.e_ident == 0x7f454746);
Elf_Phdr phdr[ehdr.e_phnum];
assert(_fs_lseek(fd, ehdr.e_phoff, SEEK_SET) != -1);
ret = _fs_read(fd, phdr, sizeof(phdr));
assert(fs_lseek(fd, ehdr.e_phoff, SEEK_SET) != -1);
ret = fs_read(fd, phdr, sizeof(phdr));
assert(ret == ehdr.e_phentsize * ehdr.e_phnum);
for (int i = 0; i < ehdr.e_phnum; i ++) {
if (phdr[i].p_type != PT_LOAD) {
continue;
}
assert(_fs_lseek(fd, phdr[i].p_offset, SEEK_SET) != -1);
ret = _fs_read(fd, (void*)phdr[i].p_vaddr, phdr[i].p_filesz);
assert(fs_lseek(fd, phdr[i].p_offset, SEEK_SET) != -1);
ret = fs_read(fd, (void*)phdr[i].p_vaddr, phdr[i].p_filesz);
assert(ret == phdr[i].p_filesz);
memset((uint8_t*)phdr[i].p_vaddr + phdr[i].p_filesz, 0, phdr[i].p_memsz - phdr[i].p_filesz);
}

View File

@ -1,11 +1,20 @@
#include <common.h>
#include <syscall.h>
#include <fs.h>
#include <sys/time.h>
int _brk(void* addr) {
return 0;
}
int _gettimeofday(struct timeval *tv, struct timezone *tz) {
AM_TIMER_UPTIME_T time;
time = io_read(AM_TIMER_UPTIME);
tv->tv_sec = time.us / 1000000;
tv->tv_usec = time.us;
return 0;
}
#define CASE(syscall, ...) case _SYSCALL_NAME(syscall): __VA_ARGS__; break;
void do_syscall(Context *c) {
uintptr_t a[4];
@ -18,11 +27,13 @@ void do_syscall(Context *c) {
switch (a[0]) {
CASE(yield, yield())
CASE(open, c->GPRx = _fs_open((const char*)a[1], (int)a[2], (int)a[3]))
CASE(read, c->GPRx = _fs_read((int)a[1], (void*)a[2], (size_t)a[3]))
CASE(write, c->GPRx = _fs_write((int)a[1], (const char*)a[2], (size_t)a[3]))
CASE(lseek, c->GPRx = _fs_lseek((int)a[1], (size_t)a[2], (int)a[3]))
CASE(close, c->GPRx = _fs_close((int)a[1]))
CASE(open, c->GPRx = fs_open((const char*)a[1], (int)a[2], (int)a[3]))
CASE(read, c->GPRx = fs_read((int)a[1], (void*)a[2], (size_t)a[3]))
CASE(write, c->GPRx = fs_write((int)a[1], (const char*)a[2], (size_t)a[3]))
CASE(lseek, c->GPRx = fs_lseek((int)a[1], (size_t)a[2], (int)a[3]))
CASE(close, c->GPRx = fs_close((int)a[1]))
CASE(gettimeofday, c->GPRx = io_read(AM_TIMER_UPTIME).us)
CASE(brk, c->GPRx = _brk((void*)a[1]))
CASE(exit, halt(0))

View File

@ -167,7 +167,7 @@ $(CLEAN_ALL):
### Build fsimg and ramdisk for Nanos-lite
APPS =
TESTS = dummy hello file-test
TESTS = dummy hello file-test timer-test
fsimg: $(addprefix apps/, $(APPS)) $(addprefix tests/, $(TESTS))
-for t in $^; do $(MAKE) -s -C $(NAVY_HOME)/$$t install; done

View File

@ -94,8 +94,7 @@ off_t _lseek(int fd, off_t offset, int whence) {
}
int _gettimeofday(struct timeval *tv, struct timezone *tz) {
_exit(SYS_gettimeofday);
return 0;
return _syscall_(SYS_gettimeofday, (intptr_t)tv, (intptr_t)tz, 0);
}
int _execve(const char *fname, char * const argv[], char *const envp[]) {

View File

@ -0,0 +1,3 @@
NAME = timer-test
SRCS = timer.c
include $(NAVY_HOME)/Makefile

View File

@ -0,0 +1,11 @@
#include <unistd.h>
#include <stdio.h>
int main(void) {
struct timeval t;
while(1) {
gettimeofday(&t, NULL);
if (t.tv_sec % 2 == 0)
printf("Hello World\n");
}
}