Skip to content

Commit

Permalink
Fix all warnings, cleanup application interface code
Browse files Browse the repository at this point in the history
Signed-off-by: Mahavir Jain <[email protected]>
  • Loading branch information
mahavirj committed Mar 11, 2017
1 parent 9734aa1 commit 630e1c1
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 77 deletions.
19 changes: 14 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CC := gcc
AS := nasm
OBJDUMP := objdump
OBJCOPY := objcopy
AR := ar

# Tiny OS version
VERSION := 0.1
Expand Down Expand Up @@ -35,7 +36,12 @@ c_srcs := $(foreach dir, $(kernel_src_dir), $(wildcard $(dir)/*.c))
asm_srcs := $(foreach dir, $(boot_src_dir), $(wildcard $(dir)/*.s))
c_objs := $(c_srcs:%.c=$(objdir)/%.o)
asm_objs := $(asm_srcs:%.s=$(objdir)/%.o)
syscall_obj := $(objdir)/$(boot_src_dir)/syscall.o

app_lib_objs := $(objdir)/$(boot_src_dir)/syscall.o \
$(objdir)/stdlib/stdlib.o \
$(objdir)/stdlib/printf.o \

app_lib := $(objdir)/lib_helper.a

CFLAGS := -g -O2 -m32 -ffreestanding -Wall -Wextra -DVERSION=\"$(VERSION)\"
CFLAGS += -Iinclude/kernel \
Expand All @@ -44,7 +50,7 @@ CFLAGS += -Iinclude/kernel \
LDSCRIPT = -T ldscript/linker.ld
APP_LDSCRIPT = -T app/linker.ld
LDFLAGS = -nostdlib -Wl,--build-id=none
APP_CFLAGS := -g -O2 -m32 -static -fno-pic -Wall -Wextra -ffreestanding
APP_CFLAGS := -g -O2 -m32 -static -fno-pic -Wall -Wextra -ffreestanding -I app/
APP_LDFLAGS := -nostdlib -Ttext 0x100000 -e main -Wl,--build-id=none
ASFLAGS = -f elf

Expand All @@ -57,17 +63,20 @@ endef

all: pre-build $(kernel) $(os_image)

ramfs.obj: $(app_obj_dir)/init $(app_obj_dir)/shell
ramfs.obj: $(app_lib) $(app_obj_dir)/init $(app_obj_dir)/shell
$(V)cd $(app_obj_dir) && find . | cpio -o -H newc > ../ramfs.cpio
$(V)cd $(objdir) && $(OBJCOPY) -I binary -O elf32-i386 -B i386 ramfs.cpio $@

$(app_lib): $(app_lib_objs)
$(V)$(AR) cru $@ $^

$(app_obj_dir)/init: $(app_src_dir)/init.c
@echo " APP $<"
$(V)$(CC) $(APP_CFLAGS) $(APP_LDFLAGS) $< $(syscall_obj) -o $@
$(V)$(CC) $(APP_CFLAGS) $(APP_LDFLAGS) $< $(app_lib) -o $@

$(app_obj_dir)/shell: $(app_src_dir)/shell.c
@echo " APP $<"
$(V)$(CC) $(APP_CFLAGS) $(APP_LDFLAGS) $< $(syscall_obj) -o $@
$(V)$(CC) $(APP_CFLAGS) $(APP_LDFLAGS) $< $(app_lib) -o $@

pre-build:
@mkdir -p $(objdir)
Expand Down
10 changes: 7 additions & 3 deletions app/init.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#include <tinyos.h>
#include <unistd.h>

char a[] = "Hello World Parent!\n";
char b[] = "Hello World Child!\n";

int main(void)
{
write("User application started\n", 25);
char *argv = NULL;
printf("User application started\n");
int ret = fork();
if (ret == 0) {
exec("shell");
execve("shell", &argv, NULL);
} else {
for (;;) {
write(a, sizeof(a)-1);
printf("%s", a);
volatile int a = 1 << 23;
while (a--)
;
Expand Down
9 changes: 5 additions & 4 deletions app/shell.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <tinyos.h>

int a = 0xa5a5;
char buf[5000];
int main()
{
if (a = 0xa5a5 && !buf[4500])
write("I am shell\n", 11);
if (a == 0xa5a5)
printf("I am shell\n");
else
write("I lost you\n", 11);
printf("I lost you\n");
for(;;);
}
6 changes: 6 additions & 0 deletions app/tinyos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __TINYOS_H__
#define __TINYOS_H__

int printf(const char *fmt, ...);

#endif /* __TINYOS_H__ */
4 changes: 2 additions & 2 deletions boot/syscall.s
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ fork:
int 64
ret

[GLOBAL exec]
exec:
[GLOBAL execve]
execve:
mov eax, 2
int 64
ret
2 changes: 1 addition & 1 deletion include/drivers/vga.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
void init_vga();
void vga_write_char(char ch);
void vga_write_buf(char *buf, int len);
void printk(const char *fmt, ...);
int printk(const char *fmt, ...);

#endif /* __VGA_H__ */
5 changes: 3 additions & 2 deletions kernel/isr.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ static void syscall_handler(registers_t *r)
{
/* write system call */
if (r->eax == 0) {
/* First argument is fd, ignore for now */
/* buffer to print on VGA */
char *buf = (char *) (*(int *)(r->useresp + 4));
char *buf = (char *) (*(int *)(r->useresp + 8));
/* length of buffer */
int len = *(int *) (r->useresp + 8);
int len = *(int *) (r->useresp + 12);
vga_write_buf(buf, len);
/* fork system call */
} else if (r->eax == 1) {
Expand Down
1 change: 0 additions & 1 deletion kernel/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ static int load_elf(struct task *t, pd_t *pd, void *data)
t->irqf->useresp = STACK_ADDR - 16;

/* Set task entry point */
printk("Setting entry point %x\n", elf_hdr->e_entry);
t->irqf->eip = elf_hdr->e_entry;

return 0;
Expand Down
5 changes: 0 additions & 5 deletions ldscript/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ SECTIONS {
*(.bss*) /* all bss sections from all files */
}

.userapp ALIGN (0x1000) : /* align at 4 KB */
{
*(.userapp*)
}

. = ALIGN(4);
end = .;
}
61 changes: 61 additions & 0 deletions stdlib/printf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdarg.h>
#include <string.h>
#include <unistd.h>

extern int hex_to_str(const int num, char buf[]);
extern int dec_to_str(const int num, char buf[]);

int printf(const char *fmt, ...)
{
va_list ap;
int d, x;
char c, *s;
const char *p;
void *pi;
char buf[96];
int index = 0;
int ret;

va_start(ap, fmt);
for (p = fmt; *p; p++) {
if (*p != '%') {
buf[index++] = *p;
continue;
}
switch (*++p) {
case 's': /* string */
s = va_arg(ap, char *);
strncpy(&buf[index], s, strlen(s));
index += strlen(s);
break;
case 'd': /* int */
d = va_arg(ap, int);
ret = dec_to_str(d, &buf[index]);
index += ret;
break;
case 'x': /* hex */
x = va_arg(ap, int);
ret = hex_to_str(x, &buf[index]);
index += ret;
break;
case 'p': /* pointer */
pi = va_arg(ap, void *);
if (pi) {
ret = hex_to_str((int) pi, &buf[index]);
index += ret;
} else {
strncpy(&buf[index], "NULL", strlen("NULL"));
index += strlen("NULL");
}
break;
case 'c': /* char */
/* need a cast here since va_arg only
takes fully promoted types */
c = (char) va_arg(ap, int);
buf[index++] = c;
break;
}
}
va_end(ap);
return write(1, buf, index);
}
69 changes: 15 additions & 54 deletions stdlib/printk.c
Original file line number Diff line number Diff line change
@@ -1,67 +1,23 @@
#include <stdarg.h>
#include <vga.h>

extern int hex_to_str(const int num, char buf[]);
extern int dec_to_str(const int num, char buf[]);

static void write_str(const char *buf)
{
while (*buf)
vga_write_char(*buf++);
}

static void to_decimal(const int num)
{
int quot, i;
char rem;
char str[32];

quot = num;
i = 0;
do {
rem = (quot % 10) + '0';
quot /= 10;
str[i++] = rem;
} while (quot);

int start, end;
char temp;
for (end = i - 1, start = 0; start < end; end--, start++) {
temp = str[start];
str[start] = str[end];
str[end] = temp;
}

str[i] = '\0';
write_str(str);
}

static void to_hex(const int num)
{
int index = 32;
int val;

do {
index -= 4;
val = (num >> index) & 0xf;
} while (!val && index);

write_str("0x");
do {
if (val > 9)
val = 'A' + (val - 10);
else
val += '0';
vga_write_char(val);
index -= 4;
val = (num >> index) & 0xf;
} while (index >= 0);
}

void printk(const char *fmt, ...)
int printk(const char *fmt, ...)
{
va_list ap;
int d, x;
char c, *s;
const char *p;
void *pi;
char tmp_buf[16];

va_start(ap, fmt);
for (p = fmt; *p; p++) {
Expand All @@ -76,18 +32,22 @@ void printk(const char *fmt, ...)
break;
case 'd': /* int */
d = va_arg(ap, int);
to_decimal(d);
dec_to_str(d, tmp_buf);
write_str(tmp_buf);
break;
case 'x': /* hex */
x = va_arg(ap, int);
to_hex(x);
hex_to_str(x, tmp_buf);
write_str(tmp_buf);
break;
case 'p': /* pointer */
pi = va_arg(ap, void *);
if (pi)
to_hex((int) pi);
else
if (pi) {
hex_to_str((int) pi, tmp_buf);
write_str(tmp_buf);
} else {
write_str("NULL");
}
break;
case 'c': /* char */
/* need a cast here since va_arg only
Expand All @@ -98,4 +58,5 @@ void printk(const char *fmt, ...)
}
}
va_end(ap);
return 0;
}
52 changes: 52 additions & 0 deletions stdlib/string.c → stdlib/stdlib.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,58 @@
#include <stdint.h>
#include <stdlib.h>

int dec_to_str(const int num, char ostr[])
{
int quot, i;
char rem;

quot = num;
i = 0;
do {
rem = (quot % 10) + '0';
quot /= 10;
ostr[i++] = rem;
} while (quot);

int start, end;
char temp;
for (end = i - 1, start = 0; start < end; end--, start++) {
temp = ostr[start];
ostr[start] = ostr[end];
ostr[end] = temp;
}

ostr[i] = '\0';
return i;
}

int hex_to_str(const int num, char ostr[])
{
int index = 32;
int val;
int cnt = 0;

do {
index -= 4;
val = (num >> index) & 0xf;
} while (!val && index);

ostr[cnt++] = '0';
ostr[cnt++] = 'x';
do {
if (val > 9)
val = 'A' + (val - 10);
else
val += '0';
ostr[cnt++] = val;
index -= 4;
val = (num >> index) & 0xf;
} while (index >= 0);

ostr[cnt] = '\0';
return cnt;
}

size_t strlen(const char *s)
{
int i;
Expand Down

0 comments on commit 630e1c1

Please sign in to comment.