Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take good code #1

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a6f3711
HW2 solution
AntonKozlov Sep 21, 2021
0c87553
HW3
AntonKozlov Sep 22, 2021
a3a2754
Solved homework3
IAmFunkyFrog Sep 25, 2021
1b8dd7c
Little logical change
IAmFunkyFrog Sep 26, 2021
2cc5613
HW3 solution
AntonKozlov Sep 28, 2021
aa3d05b
HW4
AntonKozlov Sep 28, 2021
fafb6ee
Merge branch 'homework3'
IAmFunkyFrog Oct 2, 2021
d3f877c
HW4 solution
AntonKozlov Oct 12, 2021
5af8994
HW5
AntonKozlov Oct 13, 2021
f5c935c
Merge remote-tracking branch 'origin/master'
IAmFunkyFrog Oct 15, 2021
fd4005d
HW5 solution
AntonKozlov Oct 19, 2021
a42d8ce
HW6
AntonKozlov Oct 20, 2021
986ac21
Merge remote-tracking branch 'origin/master'
IAmFunkyFrog Oct 26, 2021
172e8a3
Improve sleep test
AntonKozlov Oct 27, 2021
4e5261c
HW7
AntonKozlov Oct 27, 2021
bd62e27
Fix clean in Makefile (#92)
TimPushkin Nov 1, 2021
5dfadf4
Merge remote-tracking branch 'origin/master'
IAmFunkyFrog Nov 2, 2021
c6a1eec
HW7: fix test
AntonKozlov Nov 2, 2021
190057d
Merge remote-tracking branch 'origin/master'
IAmFunkyFrog Nov 2, 2021
8c76429
HW7 solution
AntonKozlov Nov 3, 2021
056075a
HW8
AntonKozlov Nov 3, 2021
f8ac012
Merge remote-tracking branch 'origin/master'
IAmFunkyFrog Nov 7, 2021
45b10c2
HW8 solution
AntonKozlov Nov 7, 2021
9c3f8ed
HW9
AntonKozlov Nov 10, 2021
cbc1405
HW9 solution
AntonKozlov Nov 14, 2021
bf93d69
Merge remote-tracking branch 'origin/master'
IAmFunkyFrog Nov 16, 2021
1224652
Fix of "snprintf problem"
IAmFunkyFrog Nov 24, 2021
823b80d
HW10
AntonKozlov Nov 21, 2021
3321d70
Clean unused co-op apps
AntonKozlov Nov 21, 2021
dea4b07
New version of snprintf fix
IAmFunkyFrog Nov 29, 2021
5348ec6
Little improvement
IAmFunkyFrog Nov 29, 2021
ec68dcc
Added define for clarifying
IAmFunkyFrog Nov 29, 2021
4e29069
HW10 solution
AntonKozlov Nov 30, 2021
f9f187e
Added param to ctx_make(...) to support alignment
IAmFunkyFrog Dec 1, 2021
37793c0
Redundant define deletion
IAmFunkyFrog Dec 1, 2021
81e3ee7
Changed parameter to clarify meaning
IAmFunkyFrog Dec 1, 2021
204f9fe
HW11
AntonKozlov Dec 1, 2021
8add941
Merge commit 'refs/pull/132/head' of https://github.com/AntonKozlov/o…
AntonKozlov Dec 7, 2021
c53f960
HW12 test
IAmFunkyFrog Dec 14, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EditorConfig is awesome: http://EditorConfig.org

root = true

[*]
end_of_line = lf
# Do not trim everywhere: makefiles, configs are not usually follows this policy
#trim_trailing_whitespace = true

[c,h]
indent_style = tab
indent_size = 8
trim_trailing_whitespace = true

3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ jobs:
fail-fast: false
matrix:
test:
- batch
- pool
- multithread

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*.o
*.o.d
/main
/rootfs.cpio
*.app

# IDE and tooling
/tags
Expand Down
26 changes: 22 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
CFLAGS = -g
CFLAGS = -g -MMD -MT $@ -MF [email protected]
ASFLAGS = $(CFLAGS)

all : main
KERNEL_START := 0xf00000000
USERSPACE_START := 0x400000

main : $(patsubst %.c,%.o,$(wildcard *.c))
CFLAGS += -DIKERNEL_START=$(KERNEL_START) -DIUSERSPACE_START=$(USERSPACE_START)

all : main rootfs.cpio

main : $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(filter-out %.app.c,$(wildcard *.[cS]))))
$(CC) $(LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@

APPS = $(patsubst %.app.c,%.app,$(wildcard *.app.c))

$(APPS) : %.app : %.app.c
$(CC) -fno-stack-protector -fno-pic -Wl,-Ttext-segment=$(USERSPACE_START) -nostdlib -e main -static -x c $< -o $@

rootfs.cpio : $(APPS)
ls -1 $^ | cpio -o -H bin > $@

clean :
rm -f *.o main
rm -f *.[od] main rootfs.cpio *.app

-include $(wildcard *.d)

49 changes: 20 additions & 29 deletions main.c → apps.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@

#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <time.h>
#include <sys/time.h>

#include "sched.h"
#include "usyscall.h"
#include "pool.h"

static int g_retcode;

#define APPS_X(X) \
X(echo) \
X(retcode) \
X(pooltest) \


#define DECLARE(X) static int X(int, char *[]);
APPS_X(DECLARE)
Expand All @@ -27,15 +32,26 @@ static const struct app {
#undef ELEM
};

static int os_printf(const char *fmt, ...) {
char buf[128];
va_list ap;
va_start(ap, fmt);
int ret = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
return os_write(1, buf, ret);
}

static int echo(int argc, char *argv[]) {
for (int i = 1; i < argc; ++i) {
printf("%s%c", argv[i], i == argc - 1 ? '\n' : ' ');
}
fflush(stdout);
return argc - 1;
}

static int retcode(int argc, char *argv[]) {
printf("%d\n", g_retcode);
fflush(stdout);
return 0;
}

Expand All @@ -57,39 +73,19 @@ static int exec(int argc, char *argv[]) {
return g_retcode;
}

static int pooltest(int argc, char *argv[]) {
struct obj {
void *field1;
void *field2;
};
static struct obj objmem[4];
static struct pool objpool = POOL_INITIALIZER_ARRAY(objmem);

if (!strcmp(argv[1], "alloc")) {
struct obj *o = pool_alloc(&objpool);
printf("alloc %d\n", o ? (o - objmem) : -1);
return 0;
} else if (!strcmp(argv[1], "free")) {
int iobj = atoi(argv[2]);
printf("free %d\n", iobj);
pool_free(&objpool, objmem + iobj);
return 0;
}
}

int shell(int argc, char *argv[]) {
char line[256];
while (fgets(line, sizeof(line), stdin)) {
const char *comsep = "\n;";
char *stcmd;
char *cmd = strtok_r(line, comsep, &stcmd);
while (cmd) {
const char *argsep = " ";
const char *argsep = " \t";
char *starg;
char *arg = strtok_r(cmd, argsep, &starg);
char *argv[256];
int argc = 0;
while (arg) {
while (arg && arg[0] != '#') {
argv[argc++] = arg;
arg = strtok_r(NULL, argsep, &starg);
}
Expand All @@ -106,8 +102,3 @@ int shell(int argc, char *argv[]) {
}
return 0;
}


int main(int argc, char *argv[]) {
shell(0, NULL);
}
13 changes: 13 additions & 0 deletions ctx.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <string.h>

#include "ctx.h"

void ctx_make(struct ctx *ctx, void *entry, void *stack, int alignment) {
memset(ctx, 0, sizeof(*ctx));

if(alignment == STANDARD) stack = (void*)(((unsigned long)stack & ~0xf) - 8);
ctx->rsp = (unsigned long) stack;
ctx->rsp -= 8;
*(unsigned long *)ctx->rsp = (unsigned long) entry;
}

23 changes: 23 additions & 0 deletions ctx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

enum Alignment {
NONE,
STANDARD
};


struct ctx {
unsigned long rbx;
unsigned long r12;
unsigned long r13;
unsigned long r14;
unsigned long r15;
unsigned long rsp;
unsigned long rbp;
unsigned long rip;
};

extern void ctx_make(struct ctx *ctx, void *entry, void *stack, int alignment);

extern void ctx_switch(struct ctx *old, struct ctx *new);

21 changes: 21 additions & 0 deletions ctx_switch.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

.text
.global ctx_switch
ctx_switch:
mov %rbx, 0*8(%rdi)
mov %r12, 1*8(%rdi)
mov %r13, 2*8(%rdi)
mov %r14, 3*8(%rdi)
mov %r15, 4*8(%rdi)
mov %rsp, 5*8(%rdi)
mov %rbp, 6*8(%rdi)

mov 0*8(%rsi), %rbx
mov 1*8(%rsi), %r12
mov 2*8(%rsi), %r13
mov 3*8(%rsi), %r14
mov 4*8(%rsi), %r15
mov 5*8(%rsi), %rsp
mov 6*8(%rsi), %rbp

ret
62 changes: 62 additions & 0 deletions grep.app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "usyscall.h"

char *strstr(const char *where, const char *what) {
int a = 0;
int b;

do {
b = 0;
while (what[b] && what[b] == where[a + b]) {
++b;
}
if (!what[b]) {
return (char*)(where + a);
}
} while (where[a++ + b]);
return (char*)0;
}

void *memchr(const void *str, int c, long unsigned n) {
const char *s = str;
const char *e = str + n;
while (s < e) {
if (*s == c) {
return (void*)s;
}
++s;
}
return (void*)0;
}

void *memmove(void *dst, const void *src, long unsigned n) {
const char *f = src;
char *t = dst;
while (n--) {
*t++ = *f++;
}
return dst;
}

int main(int argc, char* argv[]) {
char buf[5];
int len;
int o = 0;
while (0 < (len = os_read(0, buf + o, sizeof(buf) - o))) {
len += o;
char *p = buf;
char *p2 = memchr(p, '\n', len);
while (p2) {
*p2 = '\0';
if (strstr(p, argv[1])) {
os_write(1, p, p2 - p);
os_write(1, "\n", 1);
}
p = p2 + 1;
p2 = memchr(p, '\n', len - (p - buf));
}
o = len - (p - buf);
memmove(buf, p, o);
}

os_exit(0);
}
40 changes: 40 additions & 0 deletions init.app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "usyscall.h"

long unsigned strlen(const char *str) {
const char *e = str;
while (*e) {
++e;
}
return e - str;
}

int os_print(int fd, const char *str) {
int len = strlen(str);
return os_write(fd, str, len);
}

int main(int argc, char* argv[]) {
os_write(1, "start\n", 6);

int pipe[2];
int ret = os_pipe(pipe);
if (ret < 0) {
os_print(2, "cannot create pipe\n");
os_exit(1);
}
int pid = os_fork();
if (pid) {
os_close(1);
os_dup(pipe[1]);
const char *arg[] = { "seq", "100", (char*)0 };
os_exec("seq", (char**)arg);
} else {
os_close(0);
os_dup(pipe[0]);
const char *arg[] = { "grep", "2", (char*)0 };
os_exec("grep", (char**)arg);
}

os_print(2, "should not reach here\n");
os_exit(1);
}
20 changes: 20 additions & 0 deletions pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@
#include "pool.h"

void pool_init(struct pool *p, void *mem, unsigned long nmemb, unsigned long membsz) {
p->mem = mem;
p->membsz = membsz;
p->freestart = mem;
p->freeend = p->freestart + nmemb * membsz;
p->freehead = NULL;
}

void *pool_alloc(struct pool *p) {
if (p->freestart < p->freeend) {
void *r = p->freestart;
p->freestart += p->membsz;
return r;
}

struct pool_free_block *fb = p->freehead;
if (fb) {
p->freehead = fb->next;
return fb;
}

return NULL;
}

void pool_free(struct pool *p, void *ptr) {
struct pool_free_block *fb = ptr;
fb->next = p->freehead;
p->freehead = fb;
}
15 changes: 14 additions & 1 deletion pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@

#include "util.h"

struct pool {
struct pool_free_block {
struct pool_free_block *next;
};

struct pool {
char *mem;
unsigned long membsz;
char *freestart;
char *freeend;
struct pool_free_block *freehead;
};

#define POOL_INITIALIZER(_mem, _nmemb, _membsz) { \
.mem = (char*)(_mem), \
.membsz = (_membsz), \
.freehead = NULL, \
.freestart = (char*)(_mem), \
.freeend = (char*)(_mem) + (_nmemb) * (_membsz), \
}

#define POOL_INITIALIZER_ARRAY(_array) \
Expand Down
Loading