Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "shared"]
path = shared
url = https://github.com/differrari/redlib
url = https://github.com/differrari/redlib.git
15 changes: 10 additions & 5 deletions bin/read/linker.ld
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
ENTRY(main)
PHDRS {
text PT_LOAD FLAGS(5);
data PT_LOAD FLAGS(6);
}

SECTIONS {
. = 0x20000;

.text : {
*(.text .text.*)
}
}:text

.rodata : {
*(.rodata .rodata.*)
}
} :text
. = ALIGN(0x1000);

.data : {
*(.data .data.*)
}
} :data

.bss : {
.bss (NOLOAD) : {
__bss_start = .;
*(.bss .bss.* COMMON)
__bss_end = .;
}
} :data

/DISCARD/ : {
*(.comment .note .eh_frame)
Expand Down
15 changes: 10 additions & 5 deletions bin/test/linker.ld
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
ENTRY(main)
PHDRS {
text PT_LOAD FLAGS(5);
data PT_LOAD FLAGS(6);
}

SECTIONS {
. = 0x20000;

.text : {
*(.text .text.*)
}
}:text

.rodata : {
*(.rodata .rodata.*)
}
} :text
. = ALIGN(0x1000);

.data : {
*(.data .data.*)
}
} :data

.bss : {
.bss (NOLOAD) : {
__bss_start = .;
*(.bss .bss.* COMMON)
__bss_end = .;
}
} :data

/DISCARD/ : {
*(.comment .note .eh_frame)
Expand Down
2 changes: 1 addition & 1 deletion common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BUILD_DIR := ./build
COMMON_FLAGS ?= -ffreestanding -nostdlib -fno-exceptions -fno-unwind-tables \
-fno-asynchronous-unwind-tables -g -O0 -Wall -Wextra \
-Wno-unused-parameter -Wno-address-of-packed-member \
-Werror \
#-Werror \
-Wno-unused-function

ifeq ($(ARCH), aarch64-none-elf-)
Expand Down
1 change: 1 addition & 0 deletions kernel/audio/OutputAudioDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ void OutputAudioDevice::populate(){
}

sizedptr OutputAudioDevice::request_buffer(){
*(uint32_t*)(buffer + write_ptr) = stream_id;
sizedptr ptr = (sizedptr){buffer + write_ptr + header_size, buf_size};
write_ptr += packet_size;
if (write_ptr + packet_size >= BUF_SIZE) write_ptr = 0;
Expand Down
134 changes: 83 additions & 51 deletions kernel/bin/bin_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
#include "console/kio.h"
#include "process/loading/elf_file.h"
#include "memory/page_allocator.h"
#include "alloc/allocate.h"
#include "std/memory.h"
#include "sysregs.h"
#include "memory/addr.h"

bool init_bin(){
return true;
Expand All @@ -29,64 +31,94 @@ open_bin_ref available_cmds[] = {
};

process_t* execute(const char* prog_name, int argc, const char* argv[]){
size_t listsize = 0x1000;
void *listptr = malloc(listsize);
if (list_directory_contents("/boot/redos/bin/", listptr, listsize, 0)){
size_t name_len = strlen(prog_name) + 4;
char *full_name = strcat_new(prog_name, ".elf");
string_list *list = (string_list*)listptr;
char* reader = (char*)list->array;
kprintf("Directory contains %i files",list->count);
for (uint32_t i = 0; i < list->count; i++){
char *f = reader;
if (*f){
if (strcmp_case(f, full_name,true) == 0){
string path = string_format("/boot/redos/bin/%s",full_name);
file fd = {};
FS_RESULT op = openf(path.data, &fd);
if (op != FS_RESULT_SUCCESS){
kprintf("Failed to open file %s",path.data);
return 0;
if (!prog_name || !*prog_name) return 0;

char *full_name = 0;
full_name = (strend_case(prog_name, ".elf", true) == 0) ? string_from_literal(prog_name).data : strcat_new(prog_name, ".elf");
if (full_name) {
char pathbuf[1024];
size_t pathlen = string_format_buf(pathbuf, sizeof(pathbuf), "/boot/redos/bin/%s",full_name);
if (pathlen >= sizeof(pathbuf) - 1) {
release(full_name);
return 0;
}

file fd = {};
FS_RESULT op = openf(pathbuf, &fd);
if (op == FS_RESULT_SUCCESS){
char *program = zalloc(fd.size);
if (!program){
kprintf("Failed to read file %s", pathbuf);
closef(&fd);
release(full_name);
return 0;
}
if (readf(&fd, program, fd.size) != fd.size){
kprintf("Failed to read file %s", pathbuf);
closef(&fd);
release(program);
release(full_name);
return 0;
}
process_t *proc = load_elf_file(prog_name, 0, program, fd.size);
closef(&fd);
release(program);
if (!proc){
kprintf("Failed to create process for %s",prog_name);
release(full_name);
return 0;
}
proc->PROC_X0 = argc;
proc->PROC_X1 = 0;
if (argc > 0 && argv) {
bool args_ok = true;
size_t total_str = 0;
for (int j = 0; j < argc; j++) {
const char *s = argv[j];
if (!s) {
args_ok = false;
break;
}
char *program = malloc(fd.size);
if (readf(&fd, program, fd.size) != fd.size){
kprintf("Failed to read file %s", path.data);
size_t l = strlen(s);
if (l > proc->stack_size) {
args_ok = false;
break;
}
process_t *proc = load_elf_file(prog_name, 0, program, fd.size);
string_free(path);
free_sized(full_name, name_len);
closef(&fd);
if (!proc){
kprintf("Failed to create process for %s",prog_name);
if (total_str > proc->stack_size - (l + 1)) {
args_ok = false;
break;
}
proc->PROC_X0 = argc;
if (argc > 0){
uintptr_t start = (uintptr_t)argv[0];
uintptr_t end = (uintptr_t)argv;
size_t total = end-start;
size_t argvs = argc * sizeof(uintptr_t);
char *nargvals = (char*)(PHYS_TO_VIRT_P(proc->stack_phys)-total-argvs);
char *vnargvals = (char*)(proc->stack-total-argvs);
char** nargv = (char**)(PHYS_TO_VIRT_P(proc->stack_phys)-argvs);
uintptr_t strptr = 0;
for (int i = 0; i < argc; i++){
size_t strsize = strlen(argv[i]);
memcpy(nargvals + strptr, argv[i], strsize);
*(char*)(nargvals + strptr + strsize++) = 0;
nargv[i] = vnargvals + strptr;
strptr += strsize;
}
proc->PROC_X1 = (uintptr_t)proc->stack-argvs;
proc->sp -= total+argvs;
total_str += l + 1;
}

size_t argvs = (size_t)argc * sizeof(uintptr_t);
if (argvs > proc->stack_size) args_ok = false;
if (total_str> proc->stack_size - argvs) args_ok = false;
size_t total = total_str + argvs;
if (total + 16 > proc->stack_size) args_ok = false;
if (args_ok) {
char *nargvals = (char*)((void*)dmap_pa_to_kva(proc->stack_phys)-total);
char *vnargvals = (char*)(proc->stack-total);
char** nargv = (char**)((void*)dmap_pa_to_kva(proc->stack_phys)-argvs);
size_t strptr = 0;
for (int j = 0; j < argc; j++){
size_t strsize = strlen(argv[j]);
memcpy(nargvals + strptr, argv[j], strsize);
nargvals[strptr + strsize] = 0;
nargv[j] = vnargvals + strptr;
strptr += strsize+1;
}
proc->state = READY;
return proc;
proc->PROC_X1 = (uintptr_t)proc->stack-argvs;
proc->sp = (proc->stack - total) & ~0xFULL;
} else {
proc->PROC_X0 = 0;
}
while (*reader) reader++;
reader++;
}
proc->state = READY;
release(full_name);
return proc;
}
free_sized(full_name,name_len);
release(full_name);
}

for (uint32_t i = 0; i < N_ARR(available_cmds); i++){
Expand Down
10 changes: 5 additions & 5 deletions kernel/bin/monitor_processes.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void draw_memory(char *name,int x, int y, int width, int full_height, int used,

string str = string_format("%s\n%x",(uintptr_t)name, used);
fb_draw_string(&ctx,str.data, stack_top.x, stack_top.y + height + 5, 2, system_theme.bg_color);
free_sized(str.data,str.mem_length);
string_free(str);
}

void draw_process_view(){
Expand Down Expand Up @@ -130,7 +130,7 @@ void draw_process_view(){

string pc = string_from_hex(proc->pc);
fb_draw_string(&ctx,pc.data, xo, pc_y, scale, system_theme.bg_color);
free_sized(pc.data, pc.mem_length);
string_free(pc);

draw_memory("Stack", xo, stack_y, stack_width, stack_height, proc->stack - proc->sp, proc->stack_size);
uint64_t heap = calc_heap(proc->heap_phys);
Expand All @@ -139,9 +139,9 @@ void draw_process_view(){

string flags = string_format("Flags: %x", proc->spsr);
fb_draw_string(&ctx, flags.data, xo, flags_y, scale, system_theme.bg_color);
free_sized(name.data, name.mem_length);
free_sized(state.data, state.mem_length);
free_sized(flags.data, flags.mem_length);
string_free(name);
string_free(state);
string_free(flags);

}
commit_draw_ctx(&ctx);
Expand Down
24 changes: 12 additions & 12 deletions kernel/bin/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static int ping_v4(file *fd, const ping_opts_t *o) {
string m = string_format("ping: dns lookup failed (%d) for '%s'", (int)dr, host);
write_file(fd, m.data, m.length);
write_file(fd, "\n", 1);
free_sized(m.data, m.mem_length);
string_free(m);
return 2;
}
dst_ip_be = r;
Expand Down Expand Up @@ -156,7 +156,7 @@ static int ping_v4(file *fd, const ping_opts_t *o) {
string ln = string_format("Reply from %s: bytes=32 time=%ums", ipstr, (uint32_t)rtt);
write_file(fd, ln.data, ln.length);
write_file(fd, "\n", 1);
free_sized(ln.data, ln.mem_length);
string_free(ln);
} else {
const char *msg = status_to_msg(res.status);
write_file(fd, msg, strlen(msg));
Expand All @@ -171,23 +171,23 @@ static int ping_v4(file *fd, const ping_opts_t *o) {
string h = string_format("--- %s ping statistics ---", host);
write_file(fd, h.data, h.length);
write_file(fd, "\n", 1);
free_sized(h.data, h.mem_length);
string_free(h);

uint32_t loss = (sent == 0) ? 0 : (uint32_t)((((uint64_t)(sent - received)) * 100) / sent);
uint32_t total_time = (o->count > 0) ? (o->count - 1) * o->interval_ms : 0;

string s = string_format("%u packets transmitted, %u received, %u%% packet loss, time %ums", sent, received, loss, total_time);
write_file(fd, s.data, s.length);
write_file(fd, "\n", 1);
free_sized(s.data, s.mem_length);
string_free(s);

if (received > 0) {
uint32_t avg = (uint32_t)(sum_ms / received);
if (min_ms == UINT32_MAX) min_ms = avg;
string r = string_format("rtt min/avg/max = %u/%u/%u ms", min_ms, avg, max_ms);
write_file(fd, r.data, r.length);
write_file(fd, "\n", 1);
free_sized(r.data, r.mem_length);
string_free(r);
}

return (received > 0) ? 0 : 1;
Expand All @@ -204,7 +204,7 @@ static int ping_v6(file *fd, const ping_opts_t *o) {
string m = string_format("ping: dns lookup failed (%d) for '%s'",(int)dr, host);
write_file(fd, m.data, m.length);
write_file(fd, "\n", 1);
free_sized(m.data, m.mem_length);
string_free(m);
return 2;
}
}
Expand Down Expand Up @@ -241,7 +241,7 @@ static int ping_v6(file *fd, const ping_opts_t *o) {
string ln = string_format("Reply from %s: bytes=32 time=%ums", ipstr, (uint32_t)rtt);
write_file(fd, ln.data, ln.length);
write_file(fd, "\n", 1);
free_sized(ln.data, ln.mem_length);
string_free(ln);
} else {
const char *msg = status_to_msg(res.status);
write_file(fd, msg, strlen(msg));
Expand All @@ -256,7 +256,7 @@ static int ping_v6(file *fd, const ping_opts_t *o) {
string h = string_format("--- %s ping statistics ---", host);
write_file(fd, h.data, h.length);
write_file(fd, "\n", 1);
free_sized(h.data, h.mem_length);
string_free(h);


uint32_t loss = (sent == 0) ? 0 : (uint32_t)((((uint64_t)(sent - received)) * 100) / sent);
Expand All @@ -265,15 +265,15 @@ static int ping_v6(file *fd, const ping_opts_t *o) {
string s = string_format("%u packets transmitted, %u received, %u%% packet loss, time %ums", sent, received, loss, total_time);
write_file(fd, s.data, s.length);
write_file(fd, "\n", 1);
free_sized(s.data, s.mem_length);
string_free(s);

if (received > 0) {
uint32_t avg = (uint32_t)(sum_ms / received);
if (min_ms == UINT32_MAX) min_ms = avg;
string r = string_format("rtt min/avg/max = %u/%u/%u ms", min_ms, avg, max_ms);
write_file(fd, r.data, r.length);
write_file(fd, "\n", 1);
free_sized(r.data, r.mem_length);
string_free(r);
}

return (received > 0) ? 0 : 1;
Expand All @@ -284,7 +284,7 @@ int run_ping(int argc, char *argv[]) {
string p = string_format("/proc/%u/out", pid);
file fd = {0};
open_file(p.data, &fd);
free_sized(p.data, p.mem_length);
string_free(p);

ping_opts_t opts;
if (!parse_args(argc, argv, &opts)) {
Expand All @@ -308,7 +308,7 @@ int run_ping(int argc, char *argv[]) {
string em = string_format("ping: invalid source %s (no local ip match)", ssrc);
write_file(&fd, em.data, em.length);
write_file(&fd, "\n", 1);
free_sized(em.data, em.mem_length);
string_free(em);
close_file(&fd);
return 2;
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/bin/shutdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int run_shutdown(int argc, char* argv[]){
string p = string_format("/proc/%i/out", pid);
file out;
FS_RESULT r = open_file(p.data, &out);
free_sized(p.data, p.mem_length);
string_free(p);

if (r != FS_RESULT_SUCCESS){
return 2;
Expand Down
Loading