github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/runtime/gosecgc.go (about)

     1  package runtime
     2  
     3  import (
     4  	"runtime/internal/sys"
     5  	"unsafe"
     6  )
     7  
     8  /*
     9  * This file is here to help enclave GC.
    10  * It allocates the msgx + tls part of TCS in the data segment to facilitate
    11  * root marking in the GC.
    12  * Its keeps track of routines that cross the boundary.
    13  * We try to keep a reference to them to prevent garbage collection.
    14  * This should work from both the enclave and non-enclave domains.
    15  * However, for the moment, we try to insert it only in the enclave, as the
    16  * g can be found from scanning channels in non-enclave.
    17  * */
    18  
    19  // Check that this is the same as in isgx.go
    20  const (
    21  	_ptr_expected_size = 8 // we expect the size of a pointer to be 8.
    22  	_msgx_size         = _psize
    23  	_tls_size          = _psize
    24  	_tls_reserved_size = _msgx_size + _tls_size
    25  	_padding_alignment = 2 * _psize
    26  	EnclaveMaxTls      = 4 // maximum number of concurrent threads in the enclave.
    27  
    28  	//Tricky but basically we need this struct to have some extra buffer so that
    29  	//we have something that is page aligned.
    30  	_msgxtls_arr_len = (_padding_alignment + _tls_reserved_size*EnclaveMaxTls) / _ptr_expected_size
    31  )
    32  
    33  var (
    34  	//Place holder for the TCS values, declare it as an array of byte pointers
    35  	//so that it ends up in the bss segment (part of mark root set) and not in
    36  	//the noptrbsss segment (not part of mark root set).
    37  	enclaveMsgxTlsArr [_msgxtls_arr_len]*byte
    38  	allcrossedg       map[int64]*g
    39  	gsindex           int
    40  	allcglock         mutex
    41  )
    42  
    43  func InitAllcg() {
    44  	gosecassert(_ptr_expected_size == sys.PtrSize)
    45  	gosecassert((len(enclaveMsgxTlsArr)*sys.PtrSize)%_psize == 0)
    46  	enclaveMsgxTlsArr[0] = nil
    47  	if allcrossedg != nil {
    48  		throw("Trying to reinitialize allcrossedg")
    49  	}
    50  	allcrossedg = make(map[int64]*g)
    51  }
    52  
    53  func allcgadd(gp *g) {
    54  	gosecassert(allcrossedg != nil)
    55  	lock(&allcglock)
    56  	allcrossedg[gp.goid] = gp
    57  	unlock(&allcglock)
    58  }
    59  
    60  func allcgremove(gp *g) {
    61  	if gp == nil {
    62  		throw("[allcgremove] nil gp")
    63  	}
    64  	lock(&allcglock)
    65  	if v, ok := allcrossedg[gp.goid]; ok {
    66  		gosecassert(v == gp)
    67  		delete(allcrossedg, v.goid)
    68  	} else {
    69  		println("[info] g: ", unsafe.Pointer(gp), "goid: ", gp.goid)
    70  		throw("[allcgremove] trying to remove a gp that does not exist")
    71  	}
    72  	unlock(&allcglock)
    73  }