From 2d96f78cb16af6efdeee9624d8b26ff3ace64676 Mon Sep 17 00:00:00 2001 From: Pavel <68122101+red-prig@users.noreply.github.com> Date: Fri, 17 Nov 2023 14:45:02 +0300 Subject: [PATCH] + --- sys/dev/dev_tty.pas | 8 +++- sys/kern/kern_thread.pas | 61 ++++++++++++++++++++++++++---- sys/md/md_thread.pas | 12 ++++-- sys/md/md_timeout.pas | 1 - sys/md/md_tty.pas | 81 ++++++++++++++++++++++++++++++++++------ sys/sys_crt.pas | 69 ++++++++++++++++++++++++++++++++++ sys/sys_sysinit.pas | 1 - sys/sys_tty.pas | 4 ++ sys/test/project1.lpi | 4 ++ sys/test/project1.lpr | 8 +--- sys/vfs/vfs_mount.pas | 6 ++- 11 files changed, 221 insertions(+), 34 deletions(-) create mode 100644 sys/sys_crt.pas diff --git a/sys/dev/dev_tty.pas b/sys/dev/dev_tty.pas index 2bd1fe66..0c17acba 100644 --- a/sys/dev/dev_tty.pas +++ b/sys/dev/dev_tty.pas @@ -13,8 +13,6 @@ uses sys_tty; var - deci_tty:array[0..11] of t_tty; - dev_console:p_cdev; procedure ttyconsdev_init(); //SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, nil); @@ -375,6 +373,10 @@ end; procedure ttyconsdev_init(); begin + tty_init(@std_tty[ 0],'[Input]:' ,nil); + tty_init(@std_tty[ 1],'[Output]:',nil); + tty_init(@std_tty[ 2],'[Error]:' ,nil); + // tty_init(@deci_tty[ 0],'[stdin]:' ,nil); tty_init(@deci_tty[ 1],'[stdout]:',nil); tty_init(@deci_tty[ 2],'[stderr]:',nil); @@ -402,6 +404,8 @@ begin tty_makedev(@deci_tty[ 9],'deci_ttya0' ,[]); tty_makedev(@deci_tty[10],'deci_ttyb0' ,[]); tty_makedev(@deci_tty[11],'deci_ttyc0' ,[]); + // + md_init_tty; end; diff --git a/sys/kern/kern_thread.pas b/sys/kern/kern_thread.pas index 0617135a..0eeeca9f 100644 --- a/sys/kern/kern_thread.pas +++ b/sys/kern/kern_thread.pas @@ -60,6 +60,9 @@ procedure threadinit; //SYSINIT function kthread_add (func,arg:Pointer;newtdp:pp_kthread;name:PChar):Integer; procedure kthread_exit(); +var + init_tty_cb:Tprocedure; + implementation uses @@ -348,10 +351,35 @@ end; procedure before_start(td:p_kthread); begin - //init this - ipi_sigreturn; //switch + InitThread(td^.td_ustack.stack-td^.td_ustack.sttop); + + if (init_tty_cb<>nil) then + begin + init_tty_cb(); + end; + + //switch + ipi_sigreturn; end; +procedure before_start_kern(td:p_kthread); +type + t_cb=procedure(arg:QWORD); +begin + InitThread(td^.td_ustack.stack-td^.td_ustack.sttop); + + if (init_tty_cb<>nil) then + begin + init_tty_cb(); + end; + + //call + t_cb(td^.td_frame.tf_rip)(td^.td_frame.tf_rdi); + + kthread_exit(); +end; + + procedure thread0_param(td:p_kthread); begin td^.td_base_user_pri:=700; @@ -378,6 +406,8 @@ var newtd:p_kthread; stack:stack_t; + wrap:Pointer; + n:Integer; begin Result:=0; @@ -446,10 +476,16 @@ begin newtd^.td_ustack.sttop:=stack_base; //user stack + + //seh wrapper + wrap:=@before_start; + seh_wrapper_before(newtd,wrap); + //seh wrapper + n:=cpu_thread_create(newtd, stack_base, stack_size, - @before_start, + wrap, Pointer(newtd)); if (n<>0) then @@ -506,7 +542,8 @@ begin //jit wrapper //seh wrapper - seh_wrapper(newtd); + wrap:=@before_start; + seh_wrapper_after(newtd,wrap); //seh wrapper if (td<>nil) then @@ -555,6 +592,8 @@ var newtd:p_kthread; stack:stack_t; + wrap:Pointer; + n:Integer; begin Result:=0; @@ -582,11 +621,16 @@ begin newtd^.td_ustack.sttop:=newtd^.td_kstack.sttop; //user stack + //seh wrapper + wrap:=@before_start_kern; + seh_wrapper_before(newtd,wrap); + //seh wrapper + n:=cpu_thread_create(newtd, stack.ss_sp, stack.ss_size, - func, - arg); + wrap, + Pointer(newtd)); if (n<>0) then begin @@ -601,7 +645,8 @@ begin //jit wrapper //seh wrapper - seh_wrapper(newtd); + wrap:=@before_start_kern; + seh_wrapper_after(newtd,wrap); //seh wrapper if (td<>nil) then @@ -731,6 +776,8 @@ begin //free thread_dec_ref(td); + DoneThread; + cpu_sched_throw; end; diff --git a/sys/md/md_thread.pas b/sys/md/md_thread.pas index bf504720..c53ca7df 100644 --- a/sys/md/md_thread.pas +++ b/sys/md/md_thread.pas @@ -32,7 +32,8 @@ function cpu_thread_finished(td:p_kthread):Boolean; function cpuset_setaffinity(td:p_kthread;new:Ptruint):Integer; function cpu_set_priority(td:p_kthread;prio:Integer):Integer; -procedure seh_wrapper(td:p_kthread); +procedure seh_wrapper_before(td:p_kthread;var func:Pointer); +procedure seh_wrapper_after (td:p_kthread;func:Pointer); implementation @@ -332,12 +333,15 @@ asm .seh_handler __FPC_default_handler,@except,@unwind end; -procedure seh_wrapper(td:p_kthread); +procedure seh_wrapper_before(td:p_kthread;var func:Pointer); begin - td^.td_teb^.jitcall:=Pointer(td^.td_frame.tf_rip); - td^.td_frame.tf_rip:=QWORD(@main_wrapper); + func:=@main_wrapper; end; +procedure seh_wrapper_after(td:p_kthread;func:Pointer); +begin + td^.td_teb^.jitcall:=func; +end; end. diff --git a/sys/md/md_timeout.pas b/sys/md/md_timeout.pas index 922658e0..e4b10407 100644 --- a/sys/md/md_timeout.pas +++ b/sys/md/md_timeout.pas @@ -125,7 +125,6 @@ begin msleep_td(0); until false; - kthread_exit(); end; procedure md_callout_reset(c:p_callout); diff --git a/sys/md/md_tty.pas b/sys/md/md_tty.pas index 01615624..12342806 100644 --- a/sys/md/md_tty.pas +++ b/sys/md/md_tty.pas @@ -12,11 +12,15 @@ uses subr_uio, sys_tty; -function ttydisc_read_poll (tp:p_tty):QWORD; -function ttydisc_write_poll(tp:p_tty):QWORD; +function ttydisc_read_poll (tp:p_tty):QWORD; +function ttydisc_write_poll(tp:p_tty):QWORD; -function ttydisc_read (tp:p_tty;uio:p_uio;ioflag:Integer):Integer; -function ttydisc_write(tp:p_tty;uio:p_uio;ioflag:Integer):Integer; +function ttydisc_read (tp:p_tty;uio:p_uio;ioflag:Integer):Integer; +function ttydisc_write(tp:p_tty;uio:p_uio;ioflag:Integer):Integer; + +function ttycrt_write(tp:p_tty;iov_base:Pointer;iov_len:qword):Integer; + +procedure md_init_tty; implementation @@ -76,7 +80,7 @@ end; function ttydisc_write(tp:p_tty;uio:p_uio;ioflag:Integer):Integer; var - LEN:QWORD; + MAX,LEN,OFS:QWORD; BLK:IO_STATUS_BLOCK; OFFSET:Int64; PTR:Pointer; @@ -86,26 +90,81 @@ begin BLK:=Default(IO_STATUS_BLOCK); OFFSET:=Int64(FILE_WRITE_TO_END_OF_FILE_L); //tty name - LEN:=tp^.t_nlen; - Move(tp^.t_name^,BUF,LEN); - PTR:=@BUF[LEN]; - LEN:=uio^.uio_resid+LEN; + OFS:=tp^.t_nlen; + Move(tp^.t_name^,BUF,OFS); + PTR:=@BUF[OFS]; + MAX:=Length(BUF)-OFS; + LEN:=uio^.uio_resid+OFS; //text while (LEN<>0) do begin - if (len>Length(BUF)) then len:=Length(BUF); + if (LEN>MAX) then LEN:=MAX; // - Result:=uiomove(PTR, len, uio); + Result:=uiomove(PTR, LEN-OFS, uio); if (Result<>0) then Break; // NtWriteFile(tp^.t_wr_handle,0,nil,nil,@BLK,@BUF,LEN,@OFFSET,nil); // PTR:=@BUF[0]; + MAX:=Length(BUF); LEN:=uio^.uio_resid; + OFS:=0; end; end; +function ttycrt_write(tp:p_tty;iov_base:Pointer;iov_len:qword):Integer; +var + MAX,LEN,OFS:QWORD; + BLK:IO_STATUS_BLOCK; + OFFSET:Int64; + PTR:Pointer; + BUF:array[0..1023] of AnsiChar; +begin + Result:=0; + //init + BLK:=Default(IO_STATUS_BLOCK); + OFFSET:=Int64(FILE_WRITE_TO_END_OF_FILE_L); + //tty name + OFS:=tp^.t_nlen; + Move(tp^.t_name^,BUF,OFS); + PTR:=@BUF[OFS]; + MAX:=Length(BUF)-OFS; + LEN:=iov_len+OFS; + //text + while (LEN<>0) do + begin + if (LEN>MAX) then LEN:=MAX; + // + MAX:=LEN-OFS; + Move(iov_base^,PTR^,MAX); + Inc(iov_base,MAX); + Dec(iov_len ,MAX); + // + NtWriteFile(tp^.t_wr_handle,0,nil,nil,@BLK,@BUF,LEN,@OFFSET,nil); + // + PTR:=@BUF[0]; + MAX:=Length(BUF); + LEN:=iov_len; + OFS:=0; + end; +end; +procedure md_init_tty; +var + i:Integer; +begin + For i:=0 to High(std_tty) do + begin + std_tty[i].t_rd_handle:=GetStdHandle(STD_INPUT_HANDLE); + std_tty[i].t_wr_handle:=GetStdHandle(STD_OUTPUT_HANDLE); + end; + + For i:=0 to High(deci_tty) do + begin + deci_tty[i].t_rd_handle:=GetStdHandle(STD_INPUT_HANDLE); + deci_tty[i].t_wr_handle:=GetStdHandle(STD_OUTPUT_HANDLE); + end; +end; end. diff --git a/sys/sys_crt.pas b/sys/sys_crt.pas new file mode 100644 index 00000000..8faababa --- /dev/null +++ b/sys/sys_crt.pas @@ -0,0 +1,69 @@ +unit sys_crt; + +{$mode ObjFPC}{$H+} + +interface + +uses + vuio, + sys_tty; + +Procedure sys_tty_init; + +implementation + +uses + md_tty, + kern_thread; + +Procedure CrtOutWrite(var t:TextRec); +var + tp:p_tty; +Begin + tp:=PPointer(@t.UserData)^; + if (tp=nil) then Exit; + + ttycrt_write(tp,t.Bufptr,t.BufPos); + + t.BufPos:=0; +end; + +Procedure CrtClose(Var F:TextRec); +Begin + F.Mode:=fmClosed; +end; + +Procedure CrtOpenOut(Var F:TextRec); +Begin + F.InOutFunc:=@CrtOutWrite; + F.FlushFunc:=@CrtOutWrite; + F.CloseFunc:=@CrtClose; +end; + +procedure AssignTTY(var F:Text;tp:p_tty); +begin + Assign(F,''); + // + TextRec(F).OpenFunc :=@CrtOpenOut; + // + PPointer(@TextRec(F).UserData)^:=tp; +end; + +Procedure sys_tty_init; +begin + AssignTTY(Output ,@std_tty[ 1]); + AssignTTY(StdOut ,@std_tty[ 1]); + AssignTTY(ErrOutput,@std_tty[ 2]); + AssignTTY(StdErr ,@std_tty[ 2]); + // + Rewrite(Output); + Rewrite(StdOut); + Rewrite(ErrOutput); + Rewrite(StdErr); +end; + +initialization + init_tty_cb:=@sys_tty_init; + +end. + diff --git a/sys/sys_sysinit.pas b/sys/sys_sysinit.pas index 3b2c46a5..3fc382b7 100644 --- a/sys/sys_sysinit.pas +++ b/sys/sys_sysinit.pas @@ -59,7 +59,6 @@ begin vnlru_proc; pause('sys_daemon',hz); until false; - kthread_exit(); end; procedure sys_daemon_init; diff --git a/sys/sys_tty.pas b/sys/sys_tty.pas index 8fe1d446..2f5d4c4d 100644 --- a/sys/sys_tty.pas +++ b/sys/sys_tty.pas @@ -34,6 +34,10 @@ procedure tty_unlock(tp:p_tty); procedure tty_init(tp:p_tty;name:PChar;mutex:p_mtx); procedure tty_fini(tp:p_tty); +var + std_tty :array[0..2 ] of t_tty; + deci_tty:array[0..11] of t_tty; + implementation uses diff --git a/sys/test/project1.lpi b/sys/test/project1.lpi index a7251157..cdc1de9d 100644 --- a/sys/test/project1.lpi +++ b/sys/test/project1.lpi @@ -795,6 +795,10 @@ + + + + diff --git a/sys/test/project1.lpr b/sys/test/project1.lpr index 8c39a31d..ce5cb07c 100644 --- a/sys/test/project1.lpr +++ b/sys/test/project1.lpr @@ -105,6 +105,7 @@ uses md_exception, systm, dev_tty, + sys_crt, ps4_libSceSystemService, ps4_libSceIpmi, ps4_libSceDialogs, @@ -371,12 +372,6 @@ begin //readln; - For i:=0 to High(deci_tty) do - begin - deci_tty[i].t_rd_handle:=GetStdHandle(STD_INPUT_HANDLE); - deci_tty[i].t_wr_handle:=GetStdHandle(STD_OUTPUT_HANDLE); - end; - //fs guest host err:=vfs_mount_mkdir('ufs','/app0' ,'/' ,nil,0); err:=vfs_mount_mkdir('ufs','/system','/system',nil,0); @@ -537,7 +532,6 @@ var calloutp:p_callout; begin - writeln('Get_SEH:0x',HexStr(Get_SEH)); //writeln('copyin:',copyin(mem2+64*1024*4-(sizeof(ucontext_t) div 2),@uctx,sizeof(ucontext_t))); diff --git a/sys/vfs/vfs_mount.pas b/sys/vfs/vfs_mount.pas index 6df5dc24..b9382d54 100644 --- a/sys/vfs/vfs_mount.pas +++ b/sys/vfs/vfs_mount.pas @@ -1809,9 +1809,13 @@ begin Assert(ma^.v<>nil, 'kernel_mount nil ma^.v'); Assert((ma^.len and 1)=0, 'kernel_mount odd ma^.len (%d)'); - auio.uio_iov:=ma^.v; + auio.uio_iov :=ma^.v; auio.uio_iovcnt:=ma^.len; + auio.uio_offset:=0; + auio.uio_resid :=0; auio.uio_segflg:=UIO_SYSSPACE; + auio.uio_rw :=UIO_READ; + auio.uio_td :=nil; error:=ma^.error; if (error=0) then