Skip to content

Commit

Permalink
Restart syscall in vm_syscall_block() on snap/reboot
Browse files Browse the repository at this point in the history
Killed shortcut opcodes for sys_0...sys_15 to make it possible.
  • Loading branch information
dzavalishin committed Sep 27, 2019
1 parent 59d142c commit 6ea0dc1
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 116 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,5 +1,5 @@
include config.mk
MAKEFLAGS += --print-directory
MAKEFLAGS += --no-print-directory


default::
Expand Down
38 changes: 33 additions & 5 deletions oldtree/kernel/phantom/snap_sync.c
Expand Up @@ -202,9 +202,13 @@ void phantom_snapper_reenable_threads( void )
#include <vm/syscall.h>
#include <vm/alloc.h>

#define TMP_NO_UNLOCK 1
#define TMP_RESTART_CALL 1
#define TMP_NO_UNLOCK 0
//#define MAX_SYS_ARG 16

#define VM_SYSCALL_INSTR_SIZE 2


// interlock code of VM blocking syscall (part of .internal.connection class) implementation
// called from si_connection_13_blocking, calls passed syscall worker implemented in cn_*.c in kernel
int vm_syscall_block( pvm_object_t this, struct data_area_4_thread *tc, pvm_object_t (*syscall_worker)( pvm_object_t , struct data_area_4_thread *, int nmethod, pvm_object_t arg ) )
Expand All @@ -217,13 +221,29 @@ int vm_syscall_block( pvm_object_t this, struct data_area_4_thread *tc, pvm_obje

//if( n_param < 1 ) SYSCALL_THROW(pvm_create_string_object( "blocking: need at least 1 parameter" ));

int nmethod = POP_INT();
#if TMP_RESTART_CALL

pvm_object_t __ival = POP_ARG;
ASSERT_INT(__ival);
//SYS_FREE_O(__ival); // no - after syscall is done
int nmethod = pvm_get_int(__ival);

pvm_object_t arg = POP_ARG;

// push zero to obj stack - fake return code if we are snapped during syscall
// Attempt to restart us if snap/restart happened

pvm_ostack_push( tc->_ostack, pvm_create_null_object() );
// Restore stack state and move back IP
pvm_istack_push( tc->_istack, n_param ); // arg count
pvm_ostack_push( tc->_ostack, arg );
pvm_ostack_push( tc->_ostack, __ival );

tc->code.IP -= VM_SYSCALL_INSTR_SIZE;
#else
int nmethod = POP_INT();
pvm_object_t arg = POP_ARG;
// push zero to obj stack - fake return code if we are snapped during syscall
pvm_ostack_push( tc->_ostack, pvm_create_null_object() );
#endif
pvm_exec_save_fast_acc(tc); // Before snap

if(phantom_virtual_machine_stop_request)
Expand All @@ -234,6 +254,7 @@ int vm_syscall_block( pvm_object_t this, struct data_area_4_thread *tc, pvm_obje
// JUST FOR TEST - run blocking syscalls with pers mem locked
// test if it is a cause for VM crash
#if TMP_NO_UNLOCK
#warning FIX ME
pvm_object_t ret = syscall_worker( this, tc, nmethod, arg );
#else
vm_unlock_persistent_memory();
Expand All @@ -249,8 +270,15 @@ int vm_syscall_block( pvm_object_t this, struct data_area_4_thread *tc, pvm_obje

// pop zero from obj stack
// push ret val to obj stack

#if TMP_RESTART_CALL
// We did it, now clean up args from stack
pvm_ostack_pop( tc->_ostack );
pvm_ostack_pop( tc->_ostack );
pvm_istack_pop( tc->_istack ); // arg count
tc->code.IP += VM_SYSCALL_INSTR_SIZE;
#else
pvm_ostack_pop( tc->_ostack );
#endif
pvm_ostack_push( tc->_ostack, ret );

return 1; // not throw
Expand Down
13 changes: 9 additions & 4 deletions oldtree/kernel/phantom/time.c
Expand Up @@ -18,6 +18,7 @@
#include <string.h>

#include <kernel/timedcall.h>
#include <kernel/atomic.h>


static void update_tm();
Expand Down Expand Up @@ -108,11 +109,15 @@ void hal_time_tick(int tick_rate)
diff -= shift;
}

#if 1
ATOMIC_ADD_AND_FETCH( &sys_time, tick_rate );
#else
int ei = hal_save_cli();
hal_spin_lock(&sys_time_spinlock);
sys_time += tick_rate;
hal_spin_unlock(&sys_time_spinlock);
if (ei) hal_sti();
#endif

msecDivider += tick_rate;
while(msecDivider > 1000)
Expand Down Expand Up @@ -185,11 +190,11 @@ bigtime_t hal_system_time_lores(void)
{
bigtime_t val;

int ei = hal_save_cli();
hal_spin_lock(&sys_time_spinlock);
//int ei = hal_save_cli();
//hal_spin_lock(&sys_time_spinlock);
val = sys_time;
hal_spin_unlock(&sys_time_spinlock);
if (ei) hal_sti();
//hal_spin_unlock(&sys_time_spinlock);
//if (ei) hal_sti();

return val;
}
Expand Down
14 changes: 12 additions & 2 deletions phantom/threads/t_schedule.c
Expand Up @@ -100,11 +100,15 @@ void phantom_scheduler_yield_locked( hal_spinlock_t *lock )
}




// Does not speed kernel up.
#define TMP_SKIP_SOFTINT 0

static volatile int phantom_scheduler_soft_interrupt_reenter = 0;

#if TMP_SKIP_SOFTINT
static int thread_switch_skip_count = 0; // TEMP TEST if we spend too much CPU here
#endif

void phantom_scheduler_soft_interrupt(void)
{
if(panic_reenter)
Expand Down Expand Up @@ -135,6 +139,12 @@ void phantom_scheduler_soft_interrupt(void)
return;
#endif

#if TMP_SKIP_SOFTINT
thread_switch_skip_count++;
if( (GET_CURRENT_THREAD()->sleep_flags == 0) && (thread_switch_skip_count < 10) )
return;
#endif

//if(phantom_scheduler_soft_interrupt_reenter) panic("phantom_scheduler_soft_interrupt_reenter");

phantom_scheduler_soft_interrupt_reenter++;
Expand Down
3 changes: 2 additions & 1 deletion phantom/vm/create.c
Expand Up @@ -1232,7 +1232,7 @@ void pvm_fill_syscall_interface( pvm_object_t iface, int syscall_count )
// syscall code segment generator
static pvm_object_t pvm_create_syscall_code( int sys_num )
{

/*
if( sys_num <= 15 )
{
char code[2];
Expand All @@ -1241,6 +1241,7 @@ static pvm_object_t pvm_create_syscall_code( int sys_num )
return pvm_create_code_object( sizeof(code), code );
}
else
*/
{
char code[3];
code[0] = opcode_sys_8bit;
Expand Down
23 changes: 12 additions & 11 deletions phantom/vm/exec.c
Expand Up @@ -77,10 +77,10 @@ int debug_trace = 0;



#define os_push( o ) pvm_ostack_push( da->_ostack, o )
#define os_pop() pvm_ostack_pop( da->_ostack )
#define os_top() pvm_ostack_top( da->_ostack )
#define os_empty() pvm_ostack_empty( da->_ostack )
#define os_push( o ) pvm_ostack_push( da->_ostack, o )
#define os_pop() pvm_ostack_pop( da->_ostack )
#define os_top() pvm_ostack_top( da->_ostack )
#define os_empty() pvm_ostack_empty( da->_ostack )

#define os_pull( pos ) pvm_ostack_pull( da->_ostack, pos )

Expand Down Expand Up @@ -2229,9 +2229,17 @@ static void do_pvm_exec(pvm_object_t current_thread)
break;

default:
#if 0 // turned off because of syscall restart mechanism needs fixed syscall instr size
if( (instruction & 0xF0 ) == opcode_sys_0 )
{
pvm_exec_sys(da,instruction & 0x0F);
goto sys_sleep;
//break;
}
#endif
if( instruction == opcode_sys_8bit )
{
pvm_exec_sys(da,pvm_code_get_byte(&(da->code))); //cf->cs.get_byte( cf->IP ));
sys_sleep:
//#if OLD_VM_SLEEP
// Only sys can put thread asleep
Expand All @@ -2247,13 +2255,6 @@ static void do_pvm_exec(pvm_object_t current_thread)
break;
}

if( instruction == opcode_sys_8bit )
{
pvm_exec_sys(da,pvm_code_get_byte(&(da->code))); //cf->cs.get_byte( cf->IP ));
goto sys_sleep;
//break;
}

if( (instruction & 0xE0 ) == opcode_call_00 )
{
unsigned n_param = pvm_code_get_byte(&(da->code));
Expand Down
34 changes: 17 additions & 17 deletions phantom/vm/ids/opcode_ids.definitions
Expand Up @@ -162,23 +162,23 @@ id(opcode_fromd,0x77) // cast from double



// TODO kill shortcuts for we will have JIT anyway and bytecode size does not matter
id(opcode_sys_0,0x80) // shortcut for syscall 0
id(opcode_sys_1,0x81)
id(opcode_sys_2,0x82)
id(opcode_sys_3,0x83)
id(opcode_sys_4,0x84)
id(opcode_sys_5,0x85)
id(opcode_sys_6,0x86)
id(opcode_sys_7,0x87)
id(opcode_sys_8,0x88)
id(opcode_sys_9,0x89)
id(opcode_sys_A,0x8A)
id(opcode_sys_B,0x8B)
id(opcode_sys_C,0x8C)
id(opcode_sys_D,0x8D)
id(opcode_sys_E,0x8E)
id(opcode_sys_F,0x8F)
// We can't have sys shortcuts because of syscall restart needs fixed sycall instr size for restart
//id(opcode_sys_0,0x80) // shortcut for syscall 0
//id(opcode_sys_1,0x81)
//id(opcode_sys_2,0x82)
//id(opcode_sys_3,0x83)
//id(opcode_sys_4,0x84)
//id(opcode_sys_5,0x85)
//id(opcode_sys_6,0x86)
//id(opcode_sys_7,0x87)
//id(opcode_sys_8,0x88)
//id(opcode_sys_9,0x89)
//id(opcode_sys_A,0x8A)
//id(opcode_sys_B,0x8B)
//id(opcode_sys_C,0x8C)
//id(opcode_sys_D,0x8D)
//id(opcode_sys_E,0x8E)
//id(opcode_sys_F,0x8F)


id(opcode_call_00,0xA0) // shortcut for call 0
Expand Down
34 changes: 17 additions & 17 deletions phantom/vm/ids/opcode_ids.h
Expand Up @@ -112,23 +112,23 @@
#define opcode_fromf 0x76 // cast from float
#define opcode_fromd 0x77 // cast from double
// 73-7f
// TODO kill shortcuts for we will have JIT anyway and bytecode size does not matter
#define opcode_sys_0 0x80 // shortcut for syscall 0
#define opcode_sys_1 0x81
#define opcode_sys_2 0x82
#define opcode_sys_3 0x83
#define opcode_sys_4 0x84
#define opcode_sys_5 0x85
#define opcode_sys_6 0x86
#define opcode_sys_7 0x87
#define opcode_sys_8 0x88
#define opcode_sys_9 0x89
#define opcode_sys_A 0x8A
#define opcode_sys_B 0x8B
#define opcode_sys_C 0x8C
#define opcode_sys_D 0x8D
#define opcode_sys_E 0x8E
#define opcode_sys_F 0x8F
// We can't have sys shortcuts because of syscall restart needs fixed sycall instr size for restart
//id(opcode_sys_0,0x80) // shortcut for syscall 0
//id(opcode_sys_1,0x81)
//id(opcode_sys_2,0x82)
//id(opcode_sys_3,0x83)
//id(opcode_sys_4,0x84)
//id(opcode_sys_5,0x85)
//id(opcode_sys_6,0x86)
//id(opcode_sys_7,0x87)
//id(opcode_sys_8,0x88)
//id(opcode_sys_9,0x89)
//id(opcode_sys_A,0x8A)
//id(opcode_sys_B,0x8B)
//id(opcode_sys_C,0x8C)
//id(opcode_sys_D,0x8D)
//id(opcode_sys_E,0x8E)
//id(opcode_sys_F,0x8F)
#define opcode_call_00 0xA0 // shortcut for call 0
#define opcode_call_01 0xA1
#define opcode_call_02 0xA2
Expand Down
34 changes: 17 additions & 17 deletions phantom/vm/ids/opcode_ids.java
Expand Up @@ -114,23 +114,23 @@ public class opcode_ids {
protected static final byte opcode_fromf = (byte)0x76; // cast from float
protected static final byte opcode_fromd = (byte)0x77; // cast from double
// 73-7f
// TODO kill shortcuts for we will have JIT anyway and bytecode size does not matter
protected static final byte opcode_sys_0 = (byte)0x80; // shortcut for syscall 0
protected static final byte opcode_sys_1 = (byte)0x81;
protected static final byte opcode_sys_2 = (byte)0x82;
protected static final byte opcode_sys_3 = (byte)0x83;
protected static final byte opcode_sys_4 = (byte)0x84;
protected static final byte opcode_sys_5 = (byte)0x85;
protected static final byte opcode_sys_6 = (byte)0x86;
protected static final byte opcode_sys_7 = (byte)0x87;
protected static final byte opcode_sys_8 = (byte)0x88;
protected static final byte opcode_sys_9 = (byte)0x89;
protected static final byte opcode_sys_A = (byte)0x8A;
protected static final byte opcode_sys_B = (byte)0x8B;
protected static final byte opcode_sys_C = (byte)0x8C;
protected static final byte opcode_sys_D = (byte)0x8D;
protected static final byte opcode_sys_E = (byte)0x8E;
protected static final byte opcode_sys_F = (byte)0x8F;
// We can't have sys shortcuts because of syscall restart needs fixed sycall instr size for restart
//id(opcode_sys_0,0x80) // shortcut for syscall 0
//id(opcode_sys_1,0x81)
//id(opcode_sys_2,0x82)
//id(opcode_sys_3,0x83)
//id(opcode_sys_4,0x84)
//id(opcode_sys_5,0x85)
//id(opcode_sys_6,0x86)
//id(opcode_sys_7,0x87)
//id(opcode_sys_8,0x88)
//id(opcode_sys_9,0x89)
//id(opcode_sys_A,0x8A)
//id(opcode_sys_B,0x8B)
//id(opcode_sys_C,0x8C)
//id(opcode_sys_D,0x8D)
//id(opcode_sys_E,0x8E)
//id(opcode_sys_F,0x8F)
protected static final byte opcode_call_00 = (byte)0xA0; // shortcut for call 0
protected static final byte opcode_call_01 = (byte)0xA1;
protected static final byte opcode_call_02 = (byte)0xA2;
Expand Down
3 changes: 3 additions & 0 deletions phantom/vm/syscall.c
Expand Up @@ -5,6 +5,9 @@
* Copyright (C) 2005-2011 Dmitry Zavalishin, dz@dz.ru
*
* Internal (native) classes implementation.
*
* See <https://github.com/dzavalishin/phantomuserland/wiki/InternalClasses>
* See <https://github.com/dzavalishin/phantomuserland/wiki/InternalMethodWritingGuide>
*
**/

Expand Down
3 changes: 1 addition & 2 deletions tools/big_gc/src/phantom/gg/ObjectType.java
@@ -1,6 +1,5 @@
package phantom.gg;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

/**
* @author Anton Zuev(anzuev@bk.ru) on 23/06/2017.
Expand Down Expand Up @@ -51,7 +50,7 @@ enum InternalObjectType{

public int getSize() {
if(size < 0)
throw new NotImplementedException();
throw new RuntimeException("getSize");
return size;
}
}
Expand Down
4 changes: 1 addition & 3 deletions tools/big_gc/src/phantom/gg/PhantomObjectsBuilder.java
Expand Up @@ -3,8 +3,6 @@
import phantom.data.AllocHeader;
import phantom.data.ObjectFlags;
import phantom.data.ObjectHeader;
import phantom.data.ObjectRef;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.nio.ByteBuffer;

Expand Down Expand Up @@ -139,7 +137,7 @@ public static ObjectHeader buildInternalObject(InternalObjectType type){
objectHeader.setDataArea(ByteBuffer.allocate(type.getSize()));
break;
default:
throw new NotImplementedException();
throw new RuntimeException("default");
}

objectHeader.setExactSize(
Expand Down

0 comments on commit 6ea0dc1

Please sign in to comment.