#include "darwin_shim.h"
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <unistd.h>
#include <fnmatch.h>
#include "lwp.h"
hrtime_t
gethrtime(void)
{
uint64_t elapsed;
static uint64_t start;
static mach_timebase_info_data_t sTimebaseInfo = { 0, 0 };
if ( sTimebaseInfo.denom == 0 ) {
(void) mach_timebase_info(&sTimebaseInfo);
start = mach_absolute_time();
}
elapsed = mach_absolute_time() - start;
if (sTimebaseInfo.denom == sTimebaseInfo.numer)
return elapsed;
else if (sTimebaseInfo.denom == 1)
return elapsed * (uint64_t)sTimebaseInfo.numer;
else {
uint64_t eta32 = elapsed >> 32;
uint64_t eps32 = elapsed & 0x00000000ffffffffLL;
uint32_t numer = sTimebaseInfo.numer, denom = sTimebaseInfo.denom;
uint64_t mu64 = numer * eta32;
uint64_t lambda64 = numer * eps32;
uint64_t q32 = mu64/denom;
uint64_t r32 = mu64 - (q32 * denom);
return (q32 << 32) + ((r32 << 32) + lambda64)/denom;
}
}
projid_t getprojid(void) { return (projid_t) 0; } taskid_t gettaskid(void) { return (taskid_t) 0; } zoneid_t getzoneid(void) { return (zoneid_t) 0; }
int
gmatch(const char *s, const char *p)
{
return fnmatch( p, s, 0 ) == 0;
}
long
sysinfo(int command, char *buf, long count)
{
switch (command)
{
int mib[2];
size_t len;
case SI_RELEASE:
mib[0] = CTL_KERN;
mib[1] = KERN_OSRELEASE;
len = count;
return sysctl(mib, 2, (void *)buf, &len, NULL, 0);
case SI_SYSNAME:
mib[0] = CTL_KERN;
mib[1] = KERN_OSTYPE;
len = count;
return sysctl(mib, 2, (void *)buf, &len, NULL, 0);
case SI_ISALIST:
#if defined(__i386__)
strlcpy( buf, "x86", count );
#elif defined(__x86_64__)
strlcpy( buf, "x86_64", count );
#else
#error Unknown ISA
#endif
return 1;
default:
return -1;
}
return 0;
}
int _rw_read_held(struct _rwlock *l){ return 1; }
int _rw_write_held(struct _rwlock *l){ return 1; }
int _mutex_held(struct _lwp_mutex *m){ return 1; }
int
p_online(processorid_t processorid, int flag)
{
static int ncpu = -1;
if (ncpu == -1) {
size_t len = sizeof(ncpu);
int mib[2] = { CTL_HW, HW_NCPU };
(void)sysctl(mib, 2, (void *)&ncpu, &len, NULL, 0);
}
switch(flag) {
case P_STATUS:
if (processorid < ncpu)
return P_ONLINE;
default:
errno = EINVAL;
return -1;
}
}
#if defined(_elf_seterr)
#undef _elf_seterr
#endif
void _SHIM_elf_seterr(int x) { _elf_seterr( 0, x); }