2011-11-14 23:26:45 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-09-30 15:45:08 +02:00
|
|
|
#include <errno.h>
|
2013-07-24 15:18:45 +02:00
|
|
|
#include <signal.h>
|
2011-11-14 23:26:45 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2016-12-08 17:37:54 +01:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
#include <cpuid.h>
|
|
|
|
#endif
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-11-14 23:26:45 +01:00
|
|
|
#include "runtime.h"
|
2014-07-19 10:53:52 +02:00
|
|
|
#include "arch.h"
|
2011-11-14 23:26:45 +01:00
|
|
|
#include "array.h"
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
enum {
|
|
|
|
maxround = sizeof(uintptr),
|
|
|
|
};
|
|
|
|
|
2015-02-06 06:03:22 +01:00
|
|
|
extern volatile intgo runtime_MemProfileRate
|
|
|
|
__asm__ (GOSYM_PREFIX "runtime.MemProfileRate");
|
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
struct gotraceback_ret {
|
|
|
|
int32 level;
|
2017-01-03 23:58:48 +01:00
|
|
|
bool all;
|
|
|
|
bool crash;
|
2016-09-29 02:56:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern struct gotraceback_ret gotraceback(void)
|
|
|
|
__asm__ (GOSYM_PREFIX "runtime.gotraceback");
|
2015-02-06 06:03:22 +01:00
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
// runtime_gotraceback is the C interface to runtime.gotraceback.
|
2012-05-17 07:30:25 +02:00
|
|
|
int32
|
2013-07-16 08:54:42 +02:00
|
|
|
runtime_gotraceback(bool *crash)
|
2012-05-17 07:30:25 +02:00
|
|
|
{
|
2016-09-29 02:56:44 +02:00
|
|
|
struct gotraceback_ret r;
|
2012-05-17 07:30:25 +02:00
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
r = gotraceback();
|
2013-07-16 08:54:42 +02:00
|
|
|
if(crash != nil)
|
2016-09-29 02:56:44 +02:00
|
|
|
*crash = r.crash;
|
|
|
|
return r.level;
|
2011-11-14 23:26:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
2015-10-31 01:59:47 +01:00
|
|
|
runtime_atoi(const byte *p, intgo len)
|
2011-11-14 23:26:45 +01:00
|
|
|
{
|
|
|
|
int32 n;
|
|
|
|
|
|
|
|
n = 0;
|
2015-10-31 01:59:47 +01:00
|
|
|
while(len > 0 && '0' <= *p && *p <= '9') {
|
2011-11-14 23:26:45 +01:00
|
|
|
n = n*10 + *p++ - '0';
|
2015-10-31 01:59:47 +01:00
|
|
|
len--;
|
|
|
|
}
|
2011-11-14 23:26:45 +01:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32
|
2017-01-14 01:05:42 +01:00
|
|
|
runtime_fastrand(void)
|
2011-11-14 23:26:45 +01:00
|
|
|
{
|
2011-11-28 06:45:49 +01:00
|
|
|
M *m;
|
2011-11-14 23:26:45 +01:00
|
|
|
uint32 x;
|
|
|
|
|
2011-11-28 06:45:49 +01:00
|
|
|
m = runtime_m();
|
2011-11-14 23:26:45 +01:00
|
|
|
x = m->fastrand;
|
|
|
|
x += x;
|
|
|
|
if(x & 0x80000000L)
|
|
|
|
x ^= 0x88888eefUL;
|
|
|
|
m->fastrand = x;
|
|
|
|
return x;
|
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
int64
|
|
|
|
runtime_cputicks(void)
|
|
|
|
{
|
|
|
|
#if defined(__386__) || defined(__x86_64__)
|
|
|
|
uint32 low, high;
|
|
|
|
asm("rdtsc" : "=a" (low), "=d" (high));
|
|
|
|
return (int64)(((uint64)high << 32) | (uint64)low);
|
2014-11-04 23:39:30 +01:00
|
|
|
#elif defined (__s390__) || defined (__s390x__)
|
2014-11-06 18:00:13 +01:00
|
|
|
uint64 clock = 0;
|
|
|
|
/* stckf may not write the return variable in case of a clock error, so make
|
|
|
|
it read-write to prevent that the initialisation is optimised out.
|
|
|
|
Note: Targets below z9-109 will crash when executing store clock fast, i.e.
|
|
|
|
we don't support Go for machines older than that. */
|
|
|
|
asm volatile(".insn s,0xb27c0000,%0" /* stckf */ : "+Q" (clock) : : "cc" );
|
2014-11-04 23:39:30 +01:00
|
|
|
return (int64)clock;
|
2012-02-09 09:19:58 +01:00
|
|
|
#else
|
2017-01-14 01:05:42 +01:00
|
|
|
// Currently cputicks() is used in blocking profiler and to seed runtime·fastrand().
|
2016-09-29 02:56:44 +02:00
|
|
|
// runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
|
2017-01-14 01:05:42 +01:00
|
|
|
// TODO: need more entropy to better seed fastrand.
|
2016-09-29 02:56:44 +02:00
|
|
|
return runtime_nanotime();
|
2012-02-09 09:19:58 +01:00
|
|
|
#endif
|
|
|
|
}
|
2012-05-17 07:30:25 +02:00
|
|
|
|
2013-07-24 15:18:45 +02:00
|
|
|
void
|
2016-12-19 19:00:35 +01:00
|
|
|
runtime_signalstack(byte *p, uintptr n)
|
2013-07-24 15:18:45 +02:00
|
|
|
{
|
|
|
|
stack_t st;
|
|
|
|
|
|
|
|
st.ss_sp = p;
|
|
|
|
st.ss_size = n;
|
|
|
|
st.ss_flags = 0;
|
|
|
|
if(p == nil)
|
|
|
|
st.ss_flags = SS_DISABLE;
|
|
|
|
if(sigaltstack(&st, nil) < 0)
|
|
|
|
*(int *)0xf1 = 0xf1;
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
struct debugVars runtime_debug;
|
2016-02-03 22:58:02 +01:00
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
void
|
|
|
|
runtime_setdebug(struct debugVars* d) {
|
|
|
|
runtime_debug = *d;
|
2016-02-03 22:58:02 +01:00
|
|
|
}
|
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
void memclrBytes(Slice)
|
|
|
|
__asm__ (GOSYM_PREFIX "runtime.memclrBytes");
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
void
|
2016-09-29 02:56:44 +02:00
|
|
|
memclrBytes(Slice s)
|
2013-11-06 20:49:01 +01:00
|
|
|
{
|
2016-09-29 02:56:44 +02:00
|
|
|
runtime_memclr(s.__values, s.__count);
|
2016-02-03 22:58:02 +01:00
|
|
|
}
|
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
int32 go_open(char *, int32, int32)
|
|
|
|
__asm__ (GOSYM_PREFIX "runtime.open");
|
2016-02-03 22:58:02 +01:00
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
int32
|
|
|
|
go_open(char *name, int32 mode, int32 perm)
|
|
|
|
{
|
|
|
|
return runtime_open(name, mode, perm);
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
int32 go_read(int32, void *, int32)
|
|
|
|
__asm__ (GOSYM_PREFIX "runtime.read");
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
int32
|
2016-09-29 02:56:44 +02:00
|
|
|
go_read(int32 fd, void *p, int32 n)
|
2013-11-06 20:49:01 +01:00
|
|
|
{
|
2016-09-29 02:56:44 +02:00
|
|
|
return runtime_read(fd, p, n);
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
int32 go_write(uintptr, void *, int32)
|
|
|
|
__asm__ (GOSYM_PREFIX "runtime.write");
|
2013-11-06 20:49:01 +01:00
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
int32
|
|
|
|
go_write(uintptr fd, void *p, int32 n)
|
|
|
|
{
|
|
|
|
return runtime_write(fd, p, n);
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
int32 go_closefd(int32)
|
|
|
|
__asm__ (GOSYM_PREFIX "runtime.closefd");
|
2014-06-07 00:37:27 +02:00
|
|
|
|
2016-09-29 02:56:44 +02:00
|
|
|
int32
|
|
|
|
go_closefd(int32 fd)
|
2014-06-07 00:37:27 +02:00
|
|
|
{
|
2016-09-29 02:56:44 +02:00
|
|
|
return runtime_close(fd);
|
2014-06-07 00:37:27 +02:00
|
|
|
}
|
2016-09-30 15:45:08 +02:00
|
|
|
|
|
|
|
intgo go_errno(void)
|
|
|
|
__asm__ (GOSYM_PREFIX "runtime.errno");
|
|
|
|
|
|
|
|
intgo
|
|
|
|
go_errno()
|
|
|
|
{
|
|
|
|
return (intgo)errno;
|
|
|
|
}
|
2016-12-08 17:37:54 +01:00
|
|
|
|
|
|
|
// CPU-specific initialization.
|
|
|
|
// Fetch CPUID info on x86.
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_cpuinit()
|
|
|
|
{
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
unsigned int eax, ebx, ecx, edx;
|
|
|
|
|
|
|
|
if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
|
|
|
|
setCpuidECX(ecx);
|
|
|
|
}
|
2017-01-03 21:41:54 +01:00
|
|
|
|
|
|
|
#if defined(HAVE_AS_X86_AES)
|
|
|
|
setSupportAES(true);
|
|
|
|
#endif
|
2016-12-08 17:37:54 +01:00
|
|
|
#endif
|
|
|
|
}
|