Skip to content

Commit 920f09b

Browse files
committed
upd: Intermediate save
1 parent a69fe1b commit 920f09b

178 files changed

Lines changed: 68473 additions & 141 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ZenOS.vhd

2 MB
Binary file not shown.

docs/CREDITS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@
3030
- [FIGlet](https://github.com/cmatsuoka/figlet) — Large ASCII text renderer
3131
- [BusyBox](https://busybox.net/) — The Swiss Army Knife of Embedded Linux
3232
- [DOOM](https://github.com/id-Software/DOOM), and the [generic platform layer](https://github.com/ozkl/doomgeneric) — 3D FPS action game.
33+
- [ClassiCube](https://classicube.net) — A clean-room remake of 2009 Minecraft Alpha.

docs/ISSUES.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
## Current issues in Zen:
22

3-
> No known issues as of now! Wanna go find out one?
3+
> ClassiCube closes upon clicking Singleplayer button in menu. Most probably the missing functionality thats needed to open another game window.
4+
5+
> Providing an argument to ClassiCube skips menu and starts a world with that name. But mostly page faults, and rarely does work but the entire rendering engine looks messed up. Read following issues to get an idea why its messed up.
6+
7+
> ClassiCube doesn't even launch from other real shells like ash and sh (BusyBox), other than ZenOS's shell, which works because it uses a custom spawn() instead of fork + exec. Infact launching classicube from one of these shells freezes the system.
8+
9+
> All of this points to core memory issues. Other big clue of this is that launching nk_widgets, or classicube, or imgview, or other big apps (i.e big binary, or an app that loads a lot of memory at runtime), actually can mess up their own window buffer (this does not sound serious as obviously apps can access window buffer but its dangerous) but they can ALSO CORRUPT the window titlebars background and colors, and harp's bottom dock islands' blur and background with corrupt ahh pixel garbage.
10+
11+
> imgview app almost never works correctly and always has some garbage pixel lines and artifacts in it, but imgview app itself is damn simple... its memory corruption too.
12+
13+
---
14+
15+
### What to do:
16+
17+
> This is definitely a kernel-side or core userspace-side error.
18+
19+
> Completely debug out, rewrite, and fix fork, and also check spawn() in the meanwhile.
20+
21+
> Check and debug the core memory manager, PMM and VMM
22+
23+
> Also need to consider edge cases of memory running out, heap, etc.
24+
25+
> Shift the custom shell to use fork() again, and test ALL apps.
26+
27+
> Make the fork, spawn, exec stuff better, idk atp

docs/TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## What I have planned next for Zen:
22

3+
> Finish ISSUES.md. Huge bugs on the way.
4+
35
> Getting more BusyBox applets.
46
57
> Self-hosting, perhaps implies GCC porting

mlibc/sysdeps/zenos/generic/generic.cpp

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <sys/utsname.h>
2121
#include <termios.h>
2222
#include <unistd.h>
23+
#include <netinet/in.h>
2324

2425
#include <frg/manual_box.hpp>
2526
#include <mlibc/all-sysdeps.hpp>
@@ -30,6 +31,17 @@
3031

3132
namespace {
3233

34+
static bool inet_sockets[64];
35+
36+
static bool is_inet_socket(int fd) {
37+
return fd >= 0 && static_cast<size_t>(fd) < (sizeof(inet_sockets) / sizeof(inet_sockets[0])) && inet_sockets[fd];
38+
}
39+
40+
static void set_inet_socket(int fd, bool value) {
41+
if(fd >= 0 && static_cast<size_t>(fd) < (sizeof(inet_sockets) / sizeof(inet_sockets[0])))
42+
inet_sockets[fd] = value;
43+
}
44+
3345
static int sc_error(long ret) {
3446
return ret < 0 ? static_cast<int>(-ret) : 0;
3547
}
@@ -221,6 +233,14 @@ int sys_read_entries(int handle, void *buffer, size_t max_size, size_t *bytes_re
221233
}
222234

223235
int sys_read(int fd, void *buf, size_t count, ssize_t *bytes_read) {
236+
if(is_inet_socket(fd)) {
237+
long ret = zenos_do_syscall4(ZENOS_SYSCALL_RECV, fd, reinterpret_cast<long>(buf), count, 0);
238+
int e = sc_error(ret);
239+
if(e)
240+
return e;
241+
*bytes_read = ret;
242+
return 0;
243+
}
224244
long ret = zenos_do_syscall3(ZENOS_SYSCALL_READ, fd, reinterpret_cast<long>(buf), count);
225245
int e = sc_error(ret);
226246
if(e)
@@ -230,6 +250,15 @@ int sys_read(int fd, void *buf, size_t count, ssize_t *bytes_read) {
230250
}
231251

232252
int sys_write(int fd, const void *buf, size_t count, ssize_t *bytes_written) {
253+
if(is_inet_socket(fd)) {
254+
long ret = zenos_do_syscall4(ZENOS_SYSCALL_SEND, fd, reinterpret_cast<long>(buf), count, 0);
255+
int e = sc_error(ret);
256+
if(e)
257+
return e;
258+
if(bytes_written)
259+
*bytes_written = ret;
260+
return 0;
261+
}
233262
long ret = zenos_do_syscall3(ZENOS_SYSCALL_WRITE, fd, reinterpret_cast<long>(buf), count);
234263
int e = sc_error(ret);
235264
if(e)
@@ -249,6 +278,11 @@ int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) {
249278
}
250279

251280
int sys_close(int fd) {
281+
if(is_inet_socket(fd)) {
282+
long ret = zenos_do_syscall1(ZENOS_SYSCALL_CLOSESOCKET, fd);
283+
set_inet_socket(fd, false);
284+
return sc_error(ret);
285+
}
252286
long ret = zenos_do_syscall1(ZENOS_SYSCALL_CLOSE, fd);
253287
return sc_error(ret);
254288
}
@@ -792,7 +826,18 @@ int sys_clone(void *tcb, pid_t *pid_out, void *stack) {
792826
}
793827

794828
int sys_socket(int domain, int type, int protocol, int *fd) {
795-
(void)protocol;
829+
int base_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
830+
if(domain == AF_INET) {
831+
if(base_type != SOCK_STREAM)
832+
return EPROTONOSUPPORT;
833+
if(protocol != 0 && protocol != IPPROTO_TCP)
834+
return EPROTONOSUPPORT;
835+
long ret = zenos_do_syscall3(ZENOS_SYSCALL_SOCKET, domain, base_type, protocol);
836+
int e = finish_fd(ret, fd);
837+
if(!e)
838+
set_inet_socket(*fd, true);
839+
return e;
840+
}
796841
long ret = zenos_do_syscall2(ZENOS_SYSCALL_UNIX_SOCKET, domain, type);
797842
return finish_fd(ret, fd);
798843
}
@@ -816,17 +861,42 @@ int sys_accept(int fd, int *newfd, struct sockaddr *addr, socklen_t *addrlen, in
816861
}
817862

818863
int sys_connect(int fd, const struct sockaddr *addr, socklen_t addrlen) {
819-
(void)addrlen;
864+
if((addr && addr->sa_family == AF_INET) || is_inet_socket(fd)) {
865+
if(!addr || addrlen < sizeof(struct sockaddr_in))
866+
return EINVAL;
867+
long ret = zenos_do_syscall3(ZENOS_SYSCALL_CONNECT,
868+
fd, reinterpret_cast<long>(addr), addrlen);
869+
int e = sc_error(ret);
870+
if(!e)
871+
set_inet_socket(fd, true);
872+
return e;
873+
}
820874
long ret = zenos_do_syscall2(ZENOS_SYSCALL_UNIX_CONNECT,
821875
fd, reinterpret_cast<long>(addr));
822876
return sc_error(ret);
823877
}
824878

825879
int sys_msg_send(int fd, const struct msghdr *msg, int flags, ssize_t *bytes_written) {
826-
(void)flags;
827880
if (!msg || !msg->msg_iov || msg->msg_iovlen == 0)
828881
return EINVAL;
829882
ssize_t total = 0;
883+
if(is_inet_socket(fd)) {
884+
for (size_t i = 0; i < msg->msg_iovlen; i++) {
885+
long ret = zenos_do_syscall4(ZENOS_SYSCALL_SEND,
886+
fd,
887+
reinterpret_cast<long>(msg->msg_iov[i].iov_base),
888+
msg->msg_iov[i].iov_len,
889+
flags);
890+
int e = sc_error(ret);
891+
if(e) return e;
892+
total += ret;
893+
if((size_t)ret < msg->msg_iov[i].iov_len)
894+
break;
895+
}
896+
*bytes_written = total;
897+
return 0;
898+
}
899+
(void)flags;
830900
for (size_t i = 0; i < msg->msg_iovlen; i++) {
831901
long ret = zenos_do_syscall4(ZENOS_SYSCALL_UNIX_SEND,
832902
fd,
@@ -842,10 +912,26 @@ int sys_msg_send(int fd, const struct msghdr *msg, int flags, ssize_t *bytes_wri
842912
}
843913

844914
int sys_msg_recv(int fd, struct msghdr *msg, int flags, ssize_t *bytes_read) {
845-
(void)flags;
846915
if (!msg || !msg->msg_iov || msg->msg_iovlen == 0)
847916
return EINVAL;
848917
ssize_t total = 0;
918+
if(is_inet_socket(fd)) {
919+
for (size_t i = 0; i < msg->msg_iovlen; i++) {
920+
long ret = zenos_do_syscall4(ZENOS_SYSCALL_RECV,
921+
fd,
922+
reinterpret_cast<long>(msg->msg_iov[i].iov_base),
923+
msg->msg_iov[i].iov_len,
924+
flags);
925+
int e = sc_error(ret);
926+
if(e) return e;
927+
total += ret;
928+
if((size_t)ret < msg->msg_iov[i].iov_len)
929+
break;
930+
}
931+
*bytes_read = total;
932+
return 0;
933+
}
934+
(void)flags;
849935
for (size_t i = 0; i < msg->msg_iovlen; i++) {
850936
long ret = zenos_do_syscall5(ZENOS_SYSCALL_UNIX_RECV,
851937
fd,
@@ -883,6 +969,10 @@ int sys_setsockopt(int fd, int layer, int number, const void *buffer, socklen_t
883969
}
884970

885971
int sys_shutdown(int fd, int how) {
972+
if(is_inet_socket(fd)) {
973+
long ret = zenos_do_syscall2(ZENOS_SYSCALL_CLOSESOCKET, fd, how);
974+
return sc_error(ret);
975+
}
886976
long ret = zenos_do_syscall2(ZENOS_SYSCALL_UNIX_SHUTDOWN, fd, how);
887977
return sc_error(ret);
888978
}

mlibc/sysdeps/zenos/generic/syscall.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
#define ZENOS_SYSCALL_SIGACTION 58
3939
#define ZENOS_SYSCALL_SIGRETURN 59
4040
#define ZENOS_SYSCALL_SIGPROCMASK 60
41+
#define ZENOS_SYSCALL_SOCKET 61
42+
#define ZENOS_SYSCALL_CONNECT 62
43+
#define ZENOS_SYSCALL_SEND 63
44+
#define ZENOS_SYSCALL_RECV 64
45+
#define ZENOS_SYSCALL_CLOSESOCKET 65
46+
#define ZENOS_SYSCALL_GETHOSTBYNAME 66
4147
#define ZENOS_SYSCALL_FUTEX 67
4248
#define ZENOS_SYSCALL_IOCTL 69
4349
#define ZENOS_SYSCALL_ARCH_PRCTL 74

src/cpu/isr.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "../libk/ports.h"
2020
#include "../libk/string.h"
2121
#include "../libk/debug/log.h"
22+
#include "../libk/debug/serial.h"
2223
#include "../drv/vga.h"
2324
#include "../kernel/sched.h"
2425
#include <stdint.h>
@@ -198,30 +199,31 @@ static void userspace_exception_report(registers_t *regs)
198199
task->userspace_faults++;
199200
task->last_userspace_fault = regs->int_no;
200201

202+
uint64_t cr2 = 0;
203+
if (regs->int_no == PAGE_FAULT)
204+
asm volatile("mov %%cr2, %0" : "=r"(cr2));
205+
206+
char msg[768];
207+
if (regs->int_no == PAGE_FAULT) {
208+
snprintf(msg, sizeof(msg),
209+
"\n%s: %s in %s (pid %d)\n"
210+
" fault=0x%016lx rip=0x%016lx rsp=0x%016lx error=0x%016lx\n",
211+
"ZenOS", exception_title(regs->int_no), task->name, task->pid,
212+
cr2, regs->rip, regs->userrsp, regs->err_code);
213+
} else {
214+
snprintf(msg, sizeof(msg),
215+
"\n%s: %s in %s (pid %d)\n"
216+
" rip=0x%016lx rsp=0x%016lx error=0x%016lx\n",
217+
"ZenOS", exception_title(regs->int_no), task->name, task->pid,
218+
regs->rip, regs->userrsp, regs->err_code);
219+
}
220+
serial_write_string(msg);
221+
201222
fd_entry_t *err = NULL;
202223
if (task->fd_table && task->fd_table->entries[2].used)
203224
err = &task->fd_table->entries[2];
204225

205226
if (err && err->type == FD_PTY_SLAVE && err->pty) {
206-
uint64_t cr2 = 0;
207-
if (regs->int_no == PAGE_FAULT)
208-
asm volatile("mov %%cr2, %0" : "=r"(cr2));
209-
210-
char msg[768];
211-
if (regs->int_no == PAGE_FAULT) {
212-
snprintf(msg, sizeof(msg),
213-
"\n%s: %s in %s (pid %d)\n"
214-
" fault=0x%016lx rip=0x%016lx rsp=0x%016lx error=0x%016lx\n",
215-
"ZenOS", exception_title(regs->int_no), task->name, task->pid,
216-
cr2, regs->rip, regs->userrsp, regs->err_code);
217-
} else {
218-
snprintf(msg, sizeof(msg),
219-
"\n%s: %s in %s (pid %d)\n"
220-
" rip=0x%016lx rsp=0x%016lx error=0x%016lx\n",
221-
"ZenOS", exception_title(regs->int_no), task->name, task->pid,
222-
regs->rip, regs->userrsp, regs->err_code);
223-
}
224-
log(msg, 3, 0);
225227
pty_fault_write(err->pty, msg);
226228
}
227229

src/cpu/smp.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ void ap_entry(struct limine_smp_info *info) {
5353
void smp_prepare(void)
5454
{
5555
struct limine_smp_response *smp = smp_request.response;
56-
if (smp == NULL) {
56+
if (smp == NULL) { // Limine fetch failed
5757
clr();
5858
log("Failed to fetch SMP (CPU) info.", 0, 1);
5959
}
60-
if(smp->cpu_count > MAX_CPUS) {
60+
if(smp->cpu_count > MAX_CPUS) { // Very unstable beyond 16.
6161
clr();
62-
log("\n\nThis system cannot run ZenOS\n\n - This system does not meet the requirement of maximum 8 CPUs.\n\nConsider downgrading your CPU.\nDecrease CPUs available if on a VM.\n ", 0, 1);
62+
log("\n\nThis system cannot run ZenOS\n\n - This system does not meet the requirement of maximum 16 CPUs.\n\nConsider downgrading your CPU.\nDecrease CPUs available if on a VM.\n ", 0, 1);
6363
}
6464

6565
g_cpu_count = (uint32_t)smp->cpu_count;
@@ -83,7 +83,7 @@ void init_smp() {
8383
log("Starting CPU %lu (LAPIC ID %d)", 1, 0,
8484
i, smp->cpus[i]->lapic_id);
8585
smp->cpus[i]->extra_argument = i;
86-
smp->cpus[i]->goto_address = ap_entry;
86+
smp->cpus[i]->goto_address = ap_entry; // CPU cores now end up at ap_entry, lets catch them there
8787
}
8888
}
8989
while (g_activeCpuCount < smp->cpu_count) {

src/cpu/smp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "stdint.h"
1919
#include "acpi/acpi.h"
2020

21-
#define MAX_CPUS 8
21+
#define MAX_CPUS 16
2222

2323
typedef struct
2424
{

src/drv/net/net.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -705,20 +705,34 @@ void net_init(void)
705705

706706
int tcp_connect(const uint8_t ip[4], uint16_t port)
707707
{
708+
int id = tcp_socket();
709+
if (id < 0)
710+
return -1;
711+
if (tcp_connect_socket(id, ip, port) < 0)
712+
return -1;
713+
return id;
714+
}
708715

709-
int id = -1;
716+
int tcp_socket(void)
717+
{
710718
for (int i = 0; i < MAX_TCP_CONNS; i++)
711719
{
712720
if (conns[i].state == TCP_STATE_CLOSED)
713721
{
714-
id = i;
715-
break;
722+
memset(&conns[i], 0, sizeof(conns[i]));
723+
conns[i].state = TCP_STATE_SOCKET;
724+
return i;
716725
}
717726
}
718-
if (id < 0)
727+
return -1;
728+
}
729+
730+
int tcp_connect_socket(int id, const uint8_t ip[4], uint16_t port)
731+
{
732+
tcp_conn_t *c = conn_get(id);
733+
if (!c || c->state != TCP_STATE_SOCKET)
719734
return -1;
720735

721-
tcp_conn_t *c = &conns[id];
722736
memset(c, 0, sizeof(*c));
723737
memcpy(c->remote_ip, ip, 4);
724738
c->remote_port = port;

0 commit comments

Comments
 (0)