Skip to content

Commit

Permalink
Register networking syscalls
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Nov 6, 2023
1 parent 0dbed15 commit 90debac
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 13 deletions.
6 changes: 3 additions & 3 deletions api/syscalls.json
Original file line number Diff line number Diff line change
Expand Up @@ -795,12 +795,12 @@
]
],
"returns": [
"void",
""
"u64",
"num_bytes"
],
"permission": "net_io",
"const_idx": 24,
"description": "Write data to an open socket."
"description": "Write data to an open socket. This function will attempt to write the entire buffer and may block if the output buffer is full."
},
{
"name": "net_close",
Expand Down
6 changes: 4 additions & 2 deletions doc/syscalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,12 @@ Read data from a socket into a buffer with specified capacity. Data can only be
## net_write

```
void net_write(u64 socket_id, const u8* buf_ptr, u64 buf_len)
u64 net_write(u64 socket_id, const u8* buf_ptr, u64 buf_len)
```

Write data to an open socket.
**Returns:** `u64 num_bytes`

Write data to an open socket. This function will attempt to write the entire buffer and may block if the output buffer is full.

## net_close

Expand Down
6 changes: 3 additions & 3 deletions ncc/include/uvm/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@
// Read data from a socket into a buffer with specified capacity. Data can only be read if available.
#define net_read(__socket_id, __buf_ptr, __buf_len) asm (__socket_id, __buf_ptr, __buf_len) -> u64 { syscall net_read; }

// void net_write(u64 socket_id, const u8* buf_ptr, u64 buf_len)
// Write data to an open socket.
#define net_write(__socket_id, __buf_ptr, __buf_len) asm (__socket_id, __buf_ptr, __buf_len) -> void { syscall net_write; }
// u64 net_write(u64 socket_id, const u8* buf_ptr, u64 buf_len)
// Write data to an open socket. This function will attempt to write the entire buffer and may block if the output buffer is full.
#define net_write(__socket_id, __buf_ptr, __buf_len) asm (__socket_id, __buf_ptr, __buf_len) -> u64 { syscall net_write; }

// void net_close(u64 socket_id)
// Close an open socket.
Expand Down
2 changes: 1 addition & 1 deletion vm/src/sys/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub const SYSCALL_DESCS: [Option<SysCallDesc>; SYSCALL_TBL_LEN] = [
Some(SysCallDesc { name: "net_listen", const_idx: 21, argc: 2, has_ret: true }),
Some(SysCallDesc { name: "net_accept", const_idx: 22, argc: 4, has_ret: true }),
Some(SysCallDesc { name: "net_read", const_idx: 23, argc: 3, has_ret: true }),
Some(SysCallDesc { name: "net_write", const_idx: 24, argc: 3, has_ret: false }),
Some(SysCallDesc { name: "net_write", const_idx: 24, argc: 3, has_ret: true }),
Some(SysCallDesc { name: "net_close", const_idx: 25, argc: 1, has_ret: false }),
];

Expand Down
16 changes: 15 additions & 1 deletion vm/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ pub enum SysCallFn
{
Fn0_0(fn(&mut VM)),
Fn0_1(fn(&mut VM) -> Value),

Fn1_0(fn(&mut VM, a0: Value)),
Fn1_1(fn(&mut VM, a0: Value) -> Value),

Fn2_0(fn(&mut VM, a0: Value, a1: Value)),
Fn2_1(fn(&mut VM, a0: Value, a1: Value) -> Value),

Fn3_0(fn(&mut VM, a0: Value, a1: Value, a2: Value)),
Fn3_1(fn(&mut VM, a0: Value, a1: Value, a2: Value) -> Value),

Fn4_0(fn(&mut VM, a0: Value, a1: Value, a2: Value, a3: Value)),
Fn4_1(fn(&mut VM, a0: Value, a1: Value, a2: Value, a3: Value) -> Value),
}
Expand All @@ -43,7 +49,9 @@ impl SysCallFn
Self::Fn1_0(_) => 1,
Self::Fn1_1(_) => 1,
Self::Fn2_0(_) => 2,
Self::Fn2_1(_) => 2,
Self::Fn3_0(_) => 3,
Self::Fn3_1(_) => 3,
Self::Fn4_0(_) => 4,
Self::Fn4_1(_) => 4,
}
Expand All @@ -57,7 +65,9 @@ impl SysCallFn
Self::Fn1_0(_) => false,
Self::Fn1_1(_) => true,
Self::Fn2_0(_) => false,
Self::Fn2_1(_) => true,
Self::Fn3_0(_) => false,
Self::Fn3_1(_) => true,
Self::Fn4_0(_) => false,
Self::Fn4_1(_) => true,
}
Expand Down Expand Up @@ -185,7 +195,11 @@ impl SysState

self.reg_syscall(AUDIO_OPEN_OUTPUT, SysCallFn::Fn4_1(audio_open_output));

// TODO: NET_, networking syscalls
self.reg_syscall(NET_LISTEN, SysCallFn::Fn2_1(net_listen));
self.reg_syscall(NET_ACCEPT, SysCallFn::Fn4_1(net_accept));
self.reg_syscall(NET_READ, SysCallFn::Fn3_1(net_read));
self.reg_syscall(NET_WRITE, SysCallFn::Fn3_1(net_write));
self.reg_syscall(NET_CLOSE, SysCallFn::Fn1_0(net_close));
}
}

Expand Down
6 changes: 3 additions & 3 deletions vm/src/sys/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ pub fn net_read(
}

// Syscall to write data on a given socket
// void net_write(u64 socket_id, void* buf_ptr, u64 buf_len);
// u64 num_bytes = net_write(u64 socket_id, void* buf_ptr, u64 buf_len);
pub fn net_write(
vm: &mut VM,
socket_id: Value,
Expand All @@ -282,9 +282,9 @@ pub fn net_write(
let stream = socket.stream.as_mut().unwrap();

let mem_slice = unsafe { slice::from_raw_parts(buf_ptr, buf_len) };
stream.write(&mem_slice).unwrap();
stream.write_all(&mem_slice).unwrap();

Value::from(0)
Value::from(buf_len)
}
_ => panic!()
}
Expand Down
15 changes: 15 additions & 0 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,13 +1461,28 @@ impl VM
fun(self, a0, a1)
}

SysCallFn::Fn2_1(fun) => {
let a1 = self.pop();
let a0 = self.pop();
let v = fun(self, a0, a1);
self.push(v);
}

SysCallFn::Fn3_0(fun) => {
let a2 = self.pop();
let a1 = self.pop();
let a0 = self.pop();
fun(self, a0, a1, a2)
}

SysCallFn::Fn3_1(fun) => {
let a2 = self.pop();
let a1 = self.pop();
let a0 = self.pop();
let v = fun(self, a0, a1, a2);
self.push(v);
}

SysCallFn::Fn4_0(fun) => {
let a3 = self.pop();
let a2 = self.pop();
Expand Down

0 comments on commit 90debac

Please sign in to comment.