Skip to content

Commit

Permalink
new code
Browse files Browse the repository at this point in the history
  • Loading branch information
szkabaroli committed Feb 3, 2023
1 parent 6739513 commit e8bbec9
Show file tree
Hide file tree
Showing 12 changed files with 495 additions and 63 deletions.
4 changes: 0 additions & 4 deletions .husky/pre-commit

This file was deleted.

36 changes: 16 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"./packages/*"
],
"scripts": {
"prepare": "husky install",
"test": "turbo run test",
"build": "turbo run build",
"start:dev": "turbo run start:dev --output-logs=new-only --no-cache --parallel",
Expand All @@ -17,7 +16,6 @@
"release": "turbo run build --filter=docs^... && changeset publish"
},
"devDependencies": {
"husky": "^7.0.4",
"jest": "^27.5.1",
"lint-staged": "^12.3.4",
"prettier": "^2.5.1",
Expand Down
26 changes: 23 additions & 3 deletions packages/fiber-kernel-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ export type fx_handle_t = number
export type fx_status_t = number
export type fx_vaddr_t = number
export type fx_rights_t = number
export type fx_koid_t = number
export type fx_txid_t = number
export type fx_signals_t = number
export type fx_policy_t = number

export type u32 = number
export type i64 = bigint

export const FX_INVALID_HANDLE = 0
export const FX_INVALID_KOID = 0
export const FX_KOID_INVALID = 0

export const FX_MAX_NAME_LEN = 100
export const FX_PROCESS_SHARED = 1

export const FX_RIGHT_NONE: fx_rights_t = 0
export const FX_RIGHT_DUPLICATE: fx_rights_t = 1 << 0
Expand All @@ -33,15 +40,28 @@ export const FX_RIGHT_APPLY_PROFILE: fx_rights_t = 1 << 19
export const FX_RIGHT_MANAGE_SOCKET: fx_rights_t = 1 << 20
export const FX_RIGHT_SAME_RIGHTS: fx_rights_t = 1 << 31


export const FX_POL_NEW_PROCESS = 1

export function todo() {
throw new Error('not implemented')
}

export enum Status {
OK = 1,
ERR_UNSUPPORTED = 2,
ERR_OUT_OF_RANGE = 3,
ERR_BAD_STATE = 4
ERR_CANCELED = 3,
ERR_OUT_OF_RANGE = 4,
ERR_BAD_STATE = 5,
ERR_BUFFER_TOO_SMALL = 6,
ERR_BAD_HANDLE = 7,
ERR_SHOULD_WAIT = 8,
ERR_PEER_CLOSED = 9,
ERR_INVALID_ARGS = 10
}

export enum Signal {
CHANNEL_READABLE = 1
}

export class Ref<T> {
Expand Down
3 changes: 2 additions & 1 deletion packages/fiber-kernel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"dependencies": {
"@meshx-org/fiber-kernel-types": "*",
"@meshx-org/fiber-sys": "*"
"@meshx-org/fiber-sys": "*",
"denque": "*"
},
"devDependencies": {
"@types/node": "^18",
Expand Down
156 changes: 138 additions & 18 deletions packages/fiber-kernel/src/fiber.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import { KernelHandle } from './object/handle'
import { JobDispatcher } from './object/job_dispatcher'
import { ProcessDispatcher, scope } from './object/process_dispatcher'
import { Ref, u32, i64, fx_status_t, fx_handle_t, fx_vaddr_t, Status, todo } from '@meshx-org/fiber-kernel-types'
import {
Ref,
u32,
i64,
fx_status_t,
fx_handle_t,
fx_vaddr_t,
Status,
todo,
FX_MAX_NAME_LEN,
FX_PROCESS_SHARED,
FX_POL_NEW_PROCESS,
FX_RIGHT_MANAGE_PROCESS,
FX_RIGHT_WRITE,
fx_rights_t,
FX_RIGHT_TRANSFER
} from '@meshx-org/fiber-kernel-types'
import { System } from '@meshx-org/fiber-sys'
import { Dispatcher } from './object/dispatcher'

export interface ILogger {
log: (...args: unknown[]) => void
trace: (...args: unknown[]) => void
}

function make_userspace_handle<T extends Dispatcher>(
handle: Ref<fx_handle_t>,
kernelHandle: KernelHandle<T>,
rgihts: fx_rights_t
): fx_status_t {
return Status.OK
}

export function createKernelLogger(): ILogger {
const logger = {
log: (...args: unknown[]) => console.log('[klog]:', ...args)
log: (...args: unknown[]) => console.log('[klog]:', ...args),
trace: (...args: unknown[]) => console.log('[klog]:', ...args)
}

return logger
Expand Down Expand Up @@ -76,38 +103,131 @@ export class Kernel implements System {
}

public sys_process_create(
parent: fx_handle_t,
name: string,
name_size: u32,
job_handle: fx_handle_t,
name: Uint8Array,
name_len: u32,
options: u32,
proc_handle_out: Ref<fx_handle_t>,
vmar_handle_out: Ref<fx_handle_t>
): fx_status_t {
this.klog.log('process_create', name)
this.klog.log(`job handle ${job_handle}, options ${options}`)
// currently, the only valid option values are 0 or ZX_PROCESS_SHARED
if (options != FX_PROCESS_SHARED && options != 0) {
return Status.ERR_INVALID_ARGS
}

proc_handle_out.value = 0
vmar_handle_out.value = 1
const up = ProcessDispatcher.getCurrent()

return Status.OK
}
// We check the policy against the process calling zx_process_create, which
// is the operative policy, rather than against |job_handle|. Access to
// |job_handle| is controlled by the rights associated with the handle.
let result: fx_status_t = up.enforceBasicPolicy(FX_POL_NEW_PROCESS)
if (result != Status.OK) {
return result
}

// copy out the name and silently truncate it.
let str = new TextDecoder().decode(name.buffer)
str = str.slice(FX_MAX_NAME_LEN)

if (result != Status.OK) {
return result
}

this.klog.trace('name ${buf}\n')

let [status, job] = up
.handleTable()
.getDispatcherWithRights<JobDispatcher>(up, job_handle, FX_RIGHT_MANAGE_PROCESS)

if (status != Status.OK) {
// Try again, but with the WRITE right.
// TODO(fxbug.dev/32803) Remove this when all callers are using MANAGE_PROCESS.
let [status2, job] = up.handleTable().getDispatcherWithRights<JobDispatcher>(up, job_handle, FX_RIGHT_WRITE)

public sys_process_start(handle: fx_handle_t, entry: fx_vaddr_t, arg1: fx_handle_t): fx_status_t {
let processDispatcher = ProcessDispatcher.create(this.rootJob.dispatcher(), '', 0)
if (status2 != Status.OK) {
return status2
}
}

let logs = []
// create a new process dispatcher
let res = ProcessDispatcher.create(job, 'TODO sp here', options)

const [process] = processDispatcher.unwrap()
if (res.isErr()) {
return res.unwrapErr()
}

scope({ dispatcher: process.dispatcher() }, () => {
this.config.onProcessStart(entry, 0).then(() => {
let [new_process_handle, proc_rights, new_vmar_handle, vmar_rights] = res.unwrap()

// const koid: u32 = new_process_handle.dispatcher().getKoid();
// fxt_kernel_object(TAG_PROC_NAME, /*always*/ false, koid, ZX_OBJ_TYPE_PROCESS, fxt::StringRef(buf));

result = make_userspace_handle(proc_handle_out, new_process_handle, proc_rights)

if (result == Status.OK) {
result = make_userspace_handle(vmar_handle_out, new_vmar_handle, vmar_rights)
return result
}

return Status.OK
}

public sys_process_start(process_handle: fx_handle_t, pc: fx_vaddr_t, arg1_handle_value: fx_handle_t): fx_status_t {
this.klog.trace(`phandle ${process_handle}, thandle %x, pc %#" PRIxPTR ", sp %#" PRIxPTR ", arg_handle %x, arg2 %#" PRIxPTR `, , thread_handle, pc, sp, arg_handle_value, arg2);

const up = ProcessDispatcher.getCurrent();

// get process dispatcher
let [status1, process] = up.handleTable().getDispatcherWithRights<ProcessDispatcher>(up, process_handle, FX_RIGHT_WRITE);
if (status1 != Status.OK) {
up.handleTable().removeHandle(up, arg1_handle_value);
return status1;
}

// get thread_dispatcher
let [status2, thread] = up.handleTable().getDispatcherWithRights(up, thread_handle, FX_RIGHT_WRITE);
if (status2 != Status.OK) {
up.handleTable().removeHandle(up, arg1_handle_value);
return status2;
}

let arg_handle: HandleOwner = up.handleTable().removeHandle(up, arg1_handle_value);

// test that the thread belongs to the starting process
if (thread.process() != process.get()) {
return ZX_ERR_ACCESS_DENIED;
}

let arg_nhv: fx_handle_t = FX_HANDLE_INVALID;
if (arg_handle) {
if (!arg_handle.hasRights(FX_RIGHT_TRANSFER)) {
return Status.ERR_ACCESS_DENIED;
}

arg_nhv = process.handleTable().mapHandleToValue(arg_handle);
process.handleTable().addHandle(arg_handle);
}

let status3 = thread.start(ThreadDispatcher.EntryState{pc, sp, arg_nhv, arg2}, /* ensure_initial_thread */ true);
if (status3 != Status.OK) {
// Remove |arg_handle| from the process that failed to start.
process.handleTable().removeHandle(process, arg_nhv);
return status3;
}



scope({ dispatcher: process }, () => {
this.config.onProcessStart(pc, 0).then(() => {
this.resolve()
})
})

return Status.OK
}

public sys_process_exit(retcode: i64): fx_status_t {
return Status.OK
public sys_process_exit(retcode: i64) {
this.klog.trace(`retcode ${retcode}`)
ProcessDispatcher.exitCurrent(retcode)
}
}
Loading

0 comments on commit e8bbec9

Please sign in to comment.