Merge branch 'master' of postpeta.pccluster.org:mckernel

This commit is contained in:
takatori
2016-02-22 13:34:00 +09:00
27 changed files with 2082 additions and 995 deletions

View File

@@ -242,7 +242,7 @@ int prepare_process_ranges_args_envs(struct thread *thread,
p->args_len = args_len;
}
dkprintf("args copy, nr: %d\n", *((int*)args_envs_r));
dkprintf("args copy, nr: %d\n", *((long *)args_envs_r));
memcpy_long(args_envs, args_envs_r, p->args_len + sizeof(long) - 1);
@@ -277,7 +277,7 @@ int prepare_process_ranges_args_envs(struct thread *thread,
p->envs_len = envs_len;
}
dkprintf("envs copy, nr: %d\n", *((int*)args_envs_r));
dkprintf("envs copy, nr: %d\n", *((long *)args_envs_r));
memcpy_long(args_envs + p->args_len, args_envs_r, p->envs_len + sizeof(long) - 1);
@@ -289,10 +289,10 @@ int prepare_process_ranges_args_envs(struct thread *thread,
flush_tlb();
// Update variables
argc = *((int*)(args_envs));
argc = *((long *)(args_envs));
dkprintf("argc: %d\n", argc);
argv = (char **)(args_envs + (sizeof(int)));
argv = (char **)(args_envs + (sizeof(long)));
if(proc->saved_cmdline){
kfree(proc->saved_cmdline);
proc->saved_cmdline_len = 0;
@@ -309,17 +309,17 @@ int prepare_process_ranges_args_envs(struct thread *thread,
*a = (char *)addr + (unsigned long)*a; // Process' address space!
}
envc = *((int*)(args_envs + p->args_len));
envc = *((long *)(args_envs + p->args_len));
dkprintf("envc: %d\n", envc);
env = (char **)(args_envs + p->args_len + sizeof(int));
env = (char **)(args_envs + p->args_len + sizeof(long));
while (*env) {
char **_env = env;
//dkprintf("%s\n", args_envs + p->args_len + (unsigned long)*env);
*env = (char *)addr + p->args_len + (unsigned long)*env;
env = ++_env;
}
env = (char **)(args_envs + p->args_len + sizeof(int));
env = (char **)(args_envs + p->args_len + sizeof(long));
dkprintf("env OK\n");
@@ -449,7 +449,7 @@ static void process_msg_init(struct ikc_scd_init_param *pcp, struct syscall_para
static void process_msg_init_acked(struct ihk_ikc_channel_desc *c, unsigned long pphys)
{
struct ikc_scd_init_param *param = (void *)pphys;
struct ikc_scd_init_param *param = phys_to_virt(pphys);
struct syscall_params *lparam;
enum ihk_mc_pt_attribute attr;

View File

@@ -312,18 +312,22 @@ struct vm_regions {
struct process_vm;
struct sigfd {
struct sigfd *next;
struct mckfd {
struct mckfd *next;
int fd;
__sigset_t mask;
long data;
void *opt;
long (*read_cb)(struct mckfd *, ihk_mc_user_context_t *);
int (*ioctl_cb)(struct mckfd *, ihk_mc_user_context_t *);
int (*close_cb)(struct mckfd *, ihk_mc_user_context_t *);
};
#define SFD_CLOEXEC 02000000
#define SFD_NONBLOCK 04000
struct sig_common {
ihk_spinlock_t lock;
ihk_atomic_t use;
struct sigfd *sigfd;
struct k_sigaction action[_NSIG];
struct list_head sigpending;
};
@@ -343,7 +347,7 @@ typedef void pgio_func_t(void *arg);
* special "init" process */
struct process {
struct list_head hash_list;
mcs_rwlock_lock_t update_lock; // lock for parent, status, ...?
mcs_rwlock_lock_t update_lock; // lock for parent, status, cpu time...
// process vm
struct process_vm *vm;
@@ -425,6 +429,16 @@ struct process {
/* Store signal sent to parent when the process terminates. */
int termsig;
ihk_spinlock_t mckfd_lock;
struct mckfd *mckfd;
// cpu time (summary)
struct timespec stime;
struct timespec utime;
// cpu time (children)
struct timespec stime_children;
struct timespec utime_children;
};
void hold_thread(struct thread *ftn);
@@ -512,6 +526,20 @@ struct thread {
unsigned long *ptrace_debugreg; /* debug registers for ptrace */
struct sig_pending *ptrace_recvsig;
struct sig_pending *ptrace_sendsig;
// cpu time
struct timespec stime;
struct timespec utime;
struct timespec btime;
int times_update;
int in_kernel;
// interval timers
int itimer_enabled;
struct itimerval itimer_virtual;
struct itimerval itimer_prof;
struct timespec itimer_virtual_value;
struct timespec itimer_prof_value;
};
struct process_vm {
@@ -615,5 +643,6 @@ void process_unlock(struct process *proc, struct mcs_rwlock_node_irqsave *lock);
void chain_process(struct process *);
void chain_thread(struct thread *);
void proc_init();
void set_timer();
#endif

View File

@@ -66,6 +66,8 @@
#define SCD_MSG_SYSFS_RESP_SETUP 0x41
/* #define SCD_MSG_SYSFS_REQ_CLEANUP 0x42 */
/* #define SCD_MSG_SYSFS_RESP_CLEANUP 0x43 */
#define SCD_MSG_PROCFS_TID_CREATE 0x44
#define SCD_MSG_PROCFS_TID_DELETE 0x45
#define ARCH_SET_GS 0x1001
#define ARCH_SET_FS 0x1002
@@ -314,6 +316,7 @@ struct procfs_read {
int ret; /* read bytes (answer) */
int status; /* non-zero if done (answer) */
int newcpu; /* migrated new cpu (answer) */
int readwrite; /* 0:read, 1:write */
char fname[PROCFS_NAME_MAX]; /* procfs filename (request) */
};
@@ -334,4 +337,7 @@ struct tod_data_s {
};
extern struct tod_data_s tod_data; /* residing in arch-dependent file */
void reset_cputime();
void set_cputime(int mode);
#endif

View File

@@ -53,5 +53,72 @@ struct timezone
int tz_dsttime; /* Nonzero if DST is ever in effect. */
};
#define ITIMER_REAL 0
#define ITIMER_VIRTUAL 1
#define ITIMER_PROF 2
struct itimerval {
struct timeval it_interval;
struct timeval it_value;
};
static inline void
ts_add(struct timespec *ats, const struct timespec *bts)
{
ats->tv_sec += bts->tv_sec;
ats->tv_nsec += bts->tv_nsec;
while(ats->tv_nsec >= 1000000000){
ats->tv_sec++;
ats->tv_nsec -= 1000000000;
}
}
static inline void
ts_sub(struct timespec *ats, const struct timespec *bts)
{
ats->tv_sec -= bts->tv_sec;
ats->tv_nsec -= bts->tv_nsec;
while(ats->tv_nsec < 0){
ats->tv_sec--;
ats->tv_nsec += 1000000000;
}
}
static inline void
tv_add(struct timeval *ats, const struct timeval *bts)
{
ats->tv_sec += bts->tv_sec;
ats->tv_usec += bts->tv_usec;
while(ats->tv_usec >= 1000000){
ats->tv_sec++;
ats->tv_usec -= 1000000;
}
}
static inline void
tv_sub(struct timeval *ats, const struct timeval *bts)
{
ats->tv_sec -= bts->tv_sec;
ats->tv_usec -= bts->tv_usec;
while(ats->tv_usec < 0){
ats->tv_sec--;
ats->tv_usec += 1000000;
}
}
static inline void
tv_to_ts(struct timespec *ats, const struct timeval *bts)
{
ats->tv_sec = bts->tv_sec;
ats->tv_nsec = bts->tv_usec * 1000;
}
static inline void
ts_to_tv(struct timeval *ats, const struct timespec *bts)
{
ats->tv_sec = bts->tv_sec;
ats->tv_usec = bts->tv_nsec / 1000;
}
#endif // __TIME_H

View File

@@ -283,7 +283,6 @@ static void post_init(void)
zero_tsc();
ap_start();
create_os_procfs_files();
sysfs_init();
populate_sysfs();
}

View File

@@ -53,6 +53,7 @@ static unsigned long pa_start, pa_end;
static struct page *pa_pages;
extern int ihk_mc_pt_print_pte(struct page_table *pt, void *virt);
extern int interrupt_from_user(void *);
struct tlb_flush_entry tlb_flush_vector[IHK_TLB_FLUSH_IRQ_VECTOR_SIZE];
@@ -369,6 +370,7 @@ static void page_fault_handler(void *fault_addr, uint64_t reason, void *regs)
struct thread *thread = cpu_local_var(current);
int error;
set_cputime(interrupt_from_user(regs)? 1: 2);
dkprintf("[%d]page_fault_handler(%p,%lx,%p)\n",
ihk_mc_get_processor_id(), fault_addr, reason, regs);
@@ -427,6 +429,7 @@ out:
ihk_mc_get_processor_id(), fault_addr, reason,
regs, error);
check_need_resched();
set_cputime(0);
return;
}

View File

@@ -63,6 +63,8 @@ extern int num_processors;
extern ihk_spinlock_t cpuid_head_lock;
int ptrace_detach(int pid, int data);
extern unsigned long do_kill(struct thread *, int pid, int tid, int sig, struct siginfo *info, int ptracecont);
extern void procfs_create_thread(struct thread *);
extern void procfs_delete_thread(struct thread *);
struct list_head resource_set_list;
mcs_rwlock_lock_t resource_set_lock;
@@ -96,6 +98,7 @@ init_process(struct process *proc, struct process *parent)
INIT_LIST_HEAD(&proc->ptraced_children_list);
mcs_rwlock_init(&proc->threads_lock);
mcs_rwlock_init(&proc->children_lock);
ihk_mc_spinlock_init(&proc->mckfd_lock);
waitq_init(&proc->waitpid_q);
ihk_atomic_set(&proc->refcount, 2);
}
@@ -2158,13 +2161,20 @@ void destroy_thread(struct thread *thread)
void release_thread(struct thread *thread)
{
struct process_vm *vm;
struct mcs_rwlock_node lock;
if (!ihk_atomic_dec_and_test(&thread->refcount)) {
return;
}
mcs_rwlock_writer_lock_noirq(&thread->proc->update_lock, &lock);
ts_add(&thread->proc->stime, &thread->stime);
ts_add(&thread->proc->utime, &thread->utime);
mcs_rwlock_writer_unlock_noirq(&thread->proc->update_lock, &lock);
vm = thread->vm;
procfs_delete_thread(thread);
destroy_thread(thread);
release_process_vm(vm);
@@ -2468,6 +2478,26 @@ ack:
ihk_mc_spinlock_unlock(&cur_v->migq_lock, irqstate);
}
void
set_timer()
{
struct cpu_local_var *v = get_this_cpu_local_var();
/* Toggle timesharing if CPU core is oversubscribed */
if (v->runq_len > 1 || v->current->itimer_enabled) {
if (!cpu_local_var(timer_enabled)) {
lapic_timer_enable(10000000);
cpu_local_var(timer_enabled) = 1;
}
}
else {
if (cpu_local_var(timer_enabled)) {
lapic_timer_disable();
cpu_local_var(timer_enabled) = 0;
}
}
}
void schedule(void)
{
struct cpu_local_var *v;
@@ -2506,20 +2536,6 @@ redo:
list_add_tail(&prev->sched_list, &(v->runq));
++v->runq_len;
}
/* Toggle timesharing if CPU core is oversubscribed */
if (v->runq_len > 1) {
if (!cpu_local_var(timer_enabled)) {
lapic_timer_enable(10000000);
cpu_local_var(timer_enabled) = 1;
}
}
else {
if (cpu_local_var(timer_enabled)) {
lapic_timer_disable();
cpu_local_var(timer_enabled) = 0;
}
}
}
if (v->flags & CPU_FLAG_NEED_MIGRATE) {
@@ -2543,8 +2559,11 @@ redo:
if (prev != next) {
switch_ctx = 1;
v->current = next;
reset_cputime();
}
set_timer();
if (switch_ctx) {
dkprintf("schedule: %d => %d \n",
prev ? prev->tid : 0, next ? next->tid : 0);
@@ -2752,7 +2771,7 @@ void runq_add_thread(struct thread *thread, int cpu_id)
__runq_add_thread(thread, cpu_id);
ihk_mc_spinlock_unlock(&(v->runq_lock), irqstate);
create_proc_procfs_files(thread->proc->pid, cpu_id);
procfs_create_thread(thread);
/* Kick scheduler */
if (cpu_id != ihk_mc_get_processor_id())

View File

@@ -38,212 +38,37 @@ extern int sscanf(const char * buf, const char * fmt, ...);
extern int osnum;
void create_proc_procfs_files(int pid, int cpuid);
void delete_proc_procfs_files(int pid);
void create_os_procfs_files(void);
void delete_os_procfs_files(void);
static void create_proc_procfs_file(int pid, char *fname, int mode, int cpuid);
static void delete_proc_procfs_file(int pid, char *fname);
static void operate_proc_procfs_file(int pid, char *fname, int msg, int mode, int cpuid);
int copy_from_user(void *dst, const void *src, size_t siz);
int copy_to_user(void *dst, const void *src, size_t siz);
/**
* \brief Create all procfs files for process.
*
* \param pid pid of the process
* \param cpuid cpuid of the process
*/
void create_proc_procfs_files(int pid, int cpuid)
{
char fname[PROCFS_NAME_MAX];
dprintf("create procfs files:\n");
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/auxv", osnum, pid);
create_proc_procfs_file(pid, fname, 0400, cpuid);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/cmdline", osnum, pid);
create_proc_procfs_file(pid, fname, 0444, cpuid);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/mem", osnum, pid);
create_proc_procfs_file(pid, fname, 0400, cpuid);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/maps", osnum, pid);
create_proc_procfs_file(pid, fname, 0444, cpuid);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/pagemap", osnum, pid);
create_proc_procfs_file(pid, fname, 0444, cpuid);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/status", osnum, pid);
create_proc_procfs_file(pid, fname, 0444, cpuid);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/task/%d/mem", osnum, pid, pid);
create_proc_procfs_file(pid, fname, 0400, cpuid);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/task/%d/stat", osnum, pid, pid);
create_proc_procfs_file(pid, fname, 0444, cpuid);
dprintf("create procfs files: done\n");
}
/**
* \brief Create a procfs file for process.
*
* \param pid pid of the process
* \param fname file name of the procfs file
* \param mode file mode
* \param cpuid cpuid of the process
*/
static void create_proc_procfs_file(int pid, char *fname, int mode, int cpuid)
{
dprintf("create procfs file: %s, mode: %o, cpuid: %d\n", fname, mode, cpuid);
operate_proc_procfs_file(pid, fname, SCD_MSG_PROCFS_CREATE, mode, cpuid);
}
/**
* \brief Delete all procfs files for process.
*
* \param pid pid of the process
*/
void delete_proc_procfs_files(int pid)
{
char fname[PROCFS_NAME_MAX];
dprintf("delete procfs files for pid %d.\n", pid);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/task/%d/mem", osnum, pid, pid);
delete_proc_procfs_file(pid, fname);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/task/%d/stat", osnum, pid, pid);
delete_proc_procfs_file(pid, fname);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/task/%d", osnum, pid, pid);
delete_proc_procfs_file(pid, fname);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/task", osnum, pid);
delete_proc_procfs_file(pid, fname);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/mem", osnum, pid);
delete_proc_procfs_file(pid, fname);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/maps", osnum, pid);
delete_proc_procfs_file(pid, fname);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/status", osnum, pid);
delete_proc_procfs_file(pid, fname);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/pagemap", osnum, pid);
delete_proc_procfs_file(pid, fname);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/cmdline", osnum, pid);
delete_proc_procfs_file(pid, fname);
snprintf(fname, PROCFS_NAME_MAX, "mcos%d/%d/auxv", osnum, pid);
delete_proc_procfs_file(pid, fname);
/* NOTE: Directory is removed on the host when mcexec drops the executable */
dprintf("delete procfs files for pid %d: done\n", pid);
}
/**
* \brief Delete a procfs file for process.
*
* \param pid pid of the process
* \param fname file name of the procfs file
*/
static void delete_proc_procfs_file(int pid, char *fname)
{
dprintf("delete procfs file: %s\n", fname);
operate_proc_procfs_file(pid, fname, SCD_MSG_PROCFS_DELETE, 0, 0);
dprintf("delete procfs file: %s done\n", fname);
}
/**
* \brief create a procfs file for this operating system
* \param fname relative path name from "host:/proc".
* \param mode permissions of the file to be created
*
* Though operate_proc_procfs_file() is intended to create a process
* specific file, it is reused to create a OS specific file by
* specifying -1 as the pid parameter.
*/
static void create_os_procfs_file(char *fname, int mode)
{
const pid_t pid = -1;
const int msg = SCD_MSG_PROCFS_CREATE;
const int cpuid = ihk_mc_get_processor_id(); /* i.e. BSP */
operate_proc_procfs_file(pid, fname, msg, mode, cpuid);
return;
}
/**
* \brief create all procfs files for this operating system
*/
void create_os_procfs_files(void)
{
char *fname = NULL;
size_t n;
fname = kmalloc(PROCFS_NAME_MAX, IHK_MC_AP_CRITICAL);
n = snprintf(fname, PROCFS_NAME_MAX, "mcos%d/stat", osnum);
if (n >= PROCFS_NAME_MAX) panic("/proc/stat");
create_os_procfs_file(fname, 0444);
return;
}
/**
* \brief Create/delete a procfs file for process.
*
* \param pid pid of the process
* \param fname file name of the procfs file
* \param msg message (create/delete)
* \param mode file mode
* \param cpuid cpuid of the process
*/
static void operate_proc_procfs_file(int pid, char *fname, int msg, int mode, int cpuid)
static void
procfs_thread_ctl(struct thread *thread, int msg)
{
struct ihk_ikc_channel_desc *syscall_channel;
struct ikc_scd_packet pckt;
struct procfs_file *f;
int ret;
struct ikc_scd_packet packet;
syscall_channel = cpu_local_var(syscall_channel);
memset(&packet, '\0', sizeof packet);
packet.arg = thread->tid;
packet.msg = msg;
packet.osnum = osnum;
packet.ref = thread->cpu_id;
packet.pid = thread->proc->pid;
packet.err = 0;
f = kmalloc(sizeof(struct procfs_file), IHK_MC_AP_NOWAIT);
if (!f) {
kprintf("ERROR: not enough memory for dealing procfs file %s!",
fname);
return;
}
f->status = 0;
f->mode = mode;
strncpy(f->fname, fname, PROCFS_NAME_MAX);
pckt.arg = virt_to_phys(f);
pckt.msg = msg;
pckt.osnum = osnum;
pckt.ref = cpuid;
pckt.pid = pid;
pckt.err = 0;
ihk_ikc_send(syscall_channel, &packet, 0);
}
ret = ihk_ikc_send(syscall_channel, &pckt, 0);
if (ret < 0) {
kprintf("ERROR: sending IKC msg, ret: %d\n", ret);
}
void
procfs_create_thread(struct thread *thread)
{
procfs_thread_ctl(thread, SCD_MSG_PROCFS_TID_CREATE);
}
while (f->status != 1) {
cpu_pause();
}
kfree(f);
void
procfs_delete_thread(struct thread *thread)
{
procfs_thread_ctl(thread, SCD_MSG_PROCFS_TID_DELETE);
}
/**
@@ -251,12 +76,13 @@ static void operate_proc_procfs_file(int pid, char *fname, int msg, int mode, in
*
* \param rarg returned argument
*/
void process_procfs_request(unsigned long rarg)
void
process_procfs_request(unsigned long rarg)
{
unsigned long parg, pbuf;
struct thread *thread = cpu_local_var(current);
struct process *proc = thread->proc;
struct thread *thread = NULL;
struct process *proc = NULL;
struct process_vm *vm = NULL;
struct procfs_read *r;
struct ikc_scd_packet packet;
int rosnum, ret, pid, tid, ans = -EIO, eof = 0;
@@ -266,7 +92,7 @@ void process_procfs_request(unsigned long rarg)
unsigned long offset;
int count;
int npages;
int is_current = 1; /* is 'proc' same as 'current'? */
int readwrite = 0;
dprintf("process_procfs_request: invoked.\n");
@@ -296,6 +122,7 @@ void process_procfs_request(unsigned long rarg)
goto bufunavail;
}
readwrite = r->readwrite;
count = r->count;
offset = r->offset;
dprintf("fname: %s, offset: %lx, count:%d.\n", r->fname, r->offset, r->count);
@@ -334,32 +161,47 @@ void process_procfs_request(unsigned long rarg)
*/
ret = sscanf(p, "%d/", &pid);
if (ret == 1) {
if (pid != cpu_local_var(current)->proc->pid) {
/* We are not located in the proper cpu for some reason. */
struct mcs_rwlock_node tlock;
int tids;
struct thread *thread1 = NULL;
dprintf("mismatched pid. We are %d, but requested pid is %d.\n",
pid, cpu_local_var(current)->pid);
tid = pid; /* main thread */
thread = find_thread(pid, tid, &lock);
if (!thread) {
dprintf("We cannot find the proper cpu for requested pid.\n");
goto end;
}
else if (thread->cpu_id != ihk_mc_get_processor_id()) {
/* The target process has gone by migration. */
r->newcpu = thread->cpu_id;
dprintf("expected cpu id is %d.\n", thread->cpu_id);
thread_unlock(thread, &lock);
ans = 0;
goto end;
}
else {
thread_unlock(thread, &lock);
/* 'proc' is not 'current' */
is_current = 0;
}
proc = thread->proc;
proc = find_process(pid, &lock);
if(proc == NULL){
kprintf("process_procfs_request: no such pid %d\n", pid);
goto end;
}
p = strchr(p, '/') + 1;
if((tids = sscanf(p, "task/%d/", &tid)) == 1){
p = strchr(p, '/') + 1;
p = strchr(p, '/') + 1;
}
else
tid = pid;
mcs_rwlock_reader_lock_noirq(&proc->threads_lock, &tlock);
list_for_each_entry(thread, &proc->threads_list, siblings_list){
if(thread->tid == tid)
break;
if(!thread1)
thread1 = thread;
}
if(thread == NULL){
kprintf("process_procfs_request: no such tid %d-%d\n", pid, tid);
if(tids){
process_unlock(proc, &lock);
mcs_rwlock_reader_unlock_noirq(&proc->threads_lock, &tlock);
goto end;
}
thread = thread1;
}
if(thread)
hold_thread(thread);
mcs_rwlock_reader_unlock_noirq(&proc->threads_lock, &tlock);
hold_process(proc);
vm = proc->vm;
if(vm)
hold_process_vm(vm);
process_unlock(proc, &lock);
}
else if (!strcmp(p, "stat")) { /* "/proc/stat" */
extern int num_processors; /* kernel/ap.c */
@@ -390,10 +232,9 @@ void process_procfs_request(unsigned long rarg)
goto end;
}
else {
kprintf("unsupported procfs entry: %s\n", p);
goto end;
}
dprintf("matched PID: %d.\n", pid);
p = strchr(p, '/') + 1;
/*
* mcos%d/PID/mem
@@ -402,74 +243,55 @@ void process_procfs_request(unsigned long rarg)
* of the process. The count is the length of the area.
*/
if (strcmp(p, "mem") == 0) {
struct vm_range *range;
struct process_vm *vm = proc->vm;
uint64_t reason = PF_POPULATE | PF_WRITE | PF_USER;
unsigned long offset = r->offset;
unsigned long left = r->count;
int ret;
struct page_table *pt = vm->address_space->page_table;
if (!is_current) {
uint64_t reason = PF_POPULATE | PF_WRITE | PF_USER;
unsigned long offset = r->offset;
unsigned long left = r->count;
int ret;
ans = 0;
if(left == 0)
goto end;
ans = 0;
if(left == 0)
goto end;
while(left){
unsigned long pa;
char *va;
int pos = offset & (PAGE_SIZE - 1);
int size = PAGE_SIZE - pos;
if(size > left)
size = left;
ret = page_fault_process_vm(proc->vm,
(void *)offset, reason);
if(ret){
if(ans == 0)
ans = -EIO;
goto end;
}
ret = ihk_mc_pt_virt_to_phys(vm->address_space->page_table,
(void *)offset, &pa);
if(ret){
if(ans == 0)
ans = -EIO;
goto end;
}
va = phys_to_virt(pa);
memcpy(buf + ans, va, size);
offset += size;
left -= size;
ans += size;
}
#if 0
if(!(proc->ptrace & PT_TRACED) ||
!(proc->status & (PS_STOPPED | PS_TRACED))){
ans = -EIO;
goto end;
}
else{
unsigned long offset = r->offset;
unsigned long left = r->count;
unsigned long pos;
unsigned long l;
ans = 0;
list_for_each_entry(range, &vm->vm_range_list, list) {
dprintf("range: %lx - %lx\n", range->start, range->end);
while (left &&
(range->start <= offset) &&
(offset < range->end)) {
pos = offset & (PAGE_SIZE - 1);
l = PAGE_SIZE - pos;
if(l > left)
l = left;
if(copy_from_user(buf, (void *)offset, l)){
if(ans == 0)
ans = -EIO;
goto end;
}
buf += l;
ans += l;
offset += l;
left -= l;
}
#endif
if(readwrite == 0)
reason = PF_POPULATE | PF_USER;
while(left){
unsigned long pa;
char *va;
int pos = offset & (PAGE_SIZE - 1);
int size = PAGE_SIZE - pos;
if(size > left)
size = left;
ret = page_fault_process_vm(vm, (void *)offset, reason);
if(ret){
if(ans == 0)
ans = -EIO;
goto end;
}
ret = ihk_mc_pt_virt_to_phys(pt, (void *)offset, &pa);
if(ret){
if(ans == 0)
ans = -EIO;
goto end;
}
va = phys_to_virt(pa);
if(readwrite)
memcpy(va, buf + ans, size);
else
memcpy(buf + ans, va, size);
offset += size;
left -= size;
ans += size;
}
goto end;
}
@@ -479,7 +301,6 @@ void process_procfs_request(unsigned long rarg)
*/
if (strcmp(p, "maps") == 0) {
struct vm_range *range;
struct process_vm *vm = proc->vm;
int left = r->count - 1; /* extra 1 for terminating NULL */
int written = 0;
char *_buf = buf;
@@ -537,7 +358,6 @@ void process_procfs_request(unsigned long rarg)
* mcos%d/PID/pagemap
*/
if (strcmp(p, "pagemap") == 0) {
struct process_vm *vm = proc->vm;
uint64_t *_buf = (uint64_t *)buf;
uint64_t start, end;
@@ -620,7 +440,7 @@ void process_procfs_request(unsigned long rarg)
* mcos%d/PID/auxv
*/
if (strcmp(p, "auxv") == 0) {
unsigned int limit = AUXV_LEN * sizeof(int);
unsigned int limit = AUXV_LEN * sizeof(unsigned long);
unsigned int len = r->count;
if (r->offset < limit) {
if (limit < r->offset + r->count) {
@@ -673,112 +493,71 @@ void process_procfs_request(unsigned long rarg)
* The offset is treated as the beginning of the virtual address area
* of the process. The count is the length of the area.
*/
tid = pid;
ret = sscanf(p, "task/%d/", &tid);
if (ret == 1) {
p = strchr(p, '/') + 1;
p = strchr(p, '/') + 1;
if (!strcmp(p, "mem")){
struct vm_range *range;
struct process_vm *vm = proc->vm;
if (!strcmp(p, "stat")) {
char tmp[1024];
int len;
if (!is_current) {
goto end;
/*
* pid (comm) state ppid
* pgrp session tty_nr tpgid
* flags minflt cminflt majflt
* cmajflt utime stime cutime
* cstime priority nice num_threads
* itrealvalue starttime vsize rss
* rsslim startcode endcode startstack
* kstkesp kstkeip signal blocked
* sigignore sigcatch wchan nswap
* cnswap exit_signal processor rt_priority
* policy delayacct_blkio_ticks guest_time cguest_time
*/
ans = sprintf(tmp,
"%d (%s) %c %d " // pid...
"%d %d %d %d " // pgrp...
"%u %lu %lu %lu " // flags...
"%lu %lu %lu %ld " // cmajflt...
"%ld %ld %ld %ld " // cstime...
"%ld %llu %lu %ld " // itrealvalue...
"%lu %lu %lu %lu " // rsslim...
"%lu %lu %lu %lu " // kstkesp...
"%lu %lu %lu %lu " // sigignore...
"%lu %d %d %u " // cnswap...
"%u %llu %lu %ld\n", // policy...
0, "exe", 'R', 0, // pid...
0, 0, 0, 0, // pgrp...
0, 0L, 0L, 0L, // flags...
0L, 0L, 0L, 0L, // cmajflt...
0L, 0L, 0L, 0L, // cstime...
0L, 0LL, 0L, 0L, // itrealvalue...
0L, 0L, 0L, 0L, // rsslim...
0L, 0L, 0L, 0L, // kstkesp...
0L, 0L, 0L, 0L, // sigignore...
0L, 0, thread->cpu_id, 0, // cnswap...
0, 0LL, 0L, 0L // policy...
);
dprintf("tmp=%s\n", tmp);
len = strlen(tmp);
if (r->offset < len) {
if (r->offset + r->count < len) {
ans = r->count;
} else {
eof = 1;
ans = len;
}
if (pid != tid) {
/* We are not multithreaded yet. */
goto end;
}
list_for_each_entry(range, &vm->vm_range_list, list) {
dprintf("range: %lx - %lx\n", range->start, range->end);
if ((range->start <= r->offset) &&
(r->offset < range->end)) {
unsigned int len = r->count;
if (range->end < r->offset + r->count) {
len = range->end - r->offset;
}
memcpy((void *)buf, (void *)range->start, len);
ans = len;
break;
}
}
goto end;
strncpy(buf, tmp + r->offset, ans);
} else if (r->offset == len) {
ans = 0;
eof = 1;
}
if (!strcmp(p, "stat")) {
char tmp[1024];
int len;
if ((thread = find_thread(pid, tid, &lock))){
dprintf("thread found! pid=%d tid=%d\n", pid, tid);
/*
* pid (comm) state ppid
* pgrp session tty_nr tpgid
* flags minflt cminflt majflt
* cmajflt utime stime cutime
* cstime priority nice num_threads
* itrealvalue starttime vsize rss
* rsslim startcode endcode startstack
* kstkesp kstkeip signal blocked
* sigignore sigcatch wchan nswap
* cnswap exit_signal processor rt_priority
* policy delayacct_blkio_ticks guest_time cguest_time
*/
ans = sprintf(tmp,
"%d (%s) %c %d " // pid...
"%d %d %d %d " // pgrp...
"%u %lu %lu %lu " // flags...
"%lu %lu %lu %ld " // cmajflt...
"%ld %ld %ld %ld " // cstime...
"%ld %llu %lu %ld " // itrealvalue...
"%lu %lu %lu %lu " // rsslim...
"%lu %lu %lu %lu " // kstkesp...
"%lu %lu %lu %lu " // sigignore...
"%lu %d %d %u " // cnswap...
"%u %llu %lu %ld\n", // policy...
0, "exe", 'R', 0, // pid...
0, 0, 0, 0, // pgrp...
0, 0L, 0L, 0L, // flags...
0L, 0L, 0L, 0L, // cmajflt...
0L, 0L, 0L, 0L, // cstime...
0L, 0LL, 0L, 0L, // itrealvalue...
0L, 0L, 0L, 0L, // rsslim...
0L, 0L, 0L, 0L, // kstkesp...
0L, 0L, 0L, 0L, // sigignore...
0L, 0, thread->cpu_id, 0, // cnswap...
0, 0LL, 0L, 0L // policy...
);
thread_unlock(thread, &lock);
dprintf("tmp=%s\n", tmp);
len = strlen(tmp);
if (r->offset < len) {
if (r->offset + r->count < len) {
ans = r->count;
} else {
eof = 1;
ans = len;
}
strncpy(buf, tmp + r->offset, ans);
} else if (r->offset == len) {
ans = 0;
eof = 1;
}
goto end;
}
else{
dprintf("no thread found pid=%d tid=%d\n", pid, tid);
}
}
dprintf("could not find a matching entry for task/%d/%s.\n", tid, p);
goto end;
}
/*
* Processing for pattern "mcos%d/PID/xxx" files should be here.
*/
dprintf("could not find a matching entry for %s.\n", p);
if(thread)
kprintf("unsupported procfs entry: %d/task/%d/%s\n", pid, tid, p);
else
kprintf("unsupported procfs entry: %d/%s\n", pid, p);
end:
ihk_mc_unmap_virtual(buf, npages, 0);
dprintf("ret: %d, eof: %d\n", ans, eof);
@@ -799,6 +578,12 @@ dataunavail:
if (ret < 0) {
kprintf("ERROR: sending IKC msg, ret: %d\n", ret);
}
if(proc)
release_process(proc);
if(thread)
release_thread(thread);
if(vm)
release_process_vm(vm);
return;
}

View File

@@ -113,6 +113,7 @@ extern unsigned long ihk_mc_get_ns_per_tsc(void);
extern int ptrace_detach(int pid, int data);
extern void debug_log(unsigned long);
extern void free_all_process_memory_range(struct process_vm *vm);
extern struct cpu_local_var *clv;
int prepare_process_ranges_args_envs(struct thread *thread,
struct program_load_desc *pn,
@@ -401,6 +402,12 @@ do_wait(int pid, int *status, int options, void *rusage)
ret = wait_zombie(thread, child, status, options);
mcs_rwlock_writer_unlock_noirq(&thread->proc->children_lock, &lock);
if(!(options & WNOWAIT)){
mcs_rwlock_writer_lock_noirq(&proc->update_lock, &lock);
ts_add(&proc->stime_children, &child->stime);
ts_add(&proc->utime_children, &child->utime);
ts_add(&proc->stime_children, &child->stime_children);
ts_add(&proc->utime_children, &child->utime_children);
mcs_rwlock_writer_unlock_noirq(&proc->update_lock, &lock);
release_process(child);
}
goto out_found;
@@ -632,8 +639,6 @@ terminate(int rc, int sig)
list_add_tail(&mythread->siblings_list, &proc->threads_list);
mcs_rwlock_writer_unlock(&proc->threads_lock, &lock);
delete_proc_procfs_files(proc->pid);
vm = proc->vm;
free_all_process_memory_range(vm);
@@ -2036,6 +2041,49 @@ SYSCALL_DECLARE(set_tid_address)
return cpu_local_var(current)->proc->pid;
}
static unsigned long
timespec_to_jiffy(const struct timespec *ats)
{
return ats->tv_sec * 100 + ats->tv_nsec / 10000000;
}
SYSCALL_DECLARE(times)
{
struct tms {
unsigned long tms_utime;
unsigned long tms_stime;
unsigned long tms_cutime;
unsigned long tms_cstime;
};
struct tms mytms;
struct tms *buf = (struct tms *)ihk_mc_syscall_arg0(ctx);
struct thread *thread = cpu_local_var(current);
struct process *proc = thread->proc;
struct timespec ats;
mytms.tms_utime = timespec_to_jiffy(&thread->utime);
mytms.tms_stime = timespec_to_jiffy(&thread->stime);
ats.tv_sec = proc->utime.tv_sec;
ats.tv_nsec = proc->utime.tv_nsec;
ts_add(&ats, &proc->utime_children);
mytms.tms_cutime = timespec_to_jiffy(&ats);
ats.tv_sec = proc->stime.tv_sec;
ats.tv_nsec = proc->stime.tv_nsec;
ts_add(&ats, &proc->stime_children);
mytms.tms_cstime = timespec_to_jiffy(&ats);
if(copy_to_user(buf, &mytms, sizeof mytms))
return -EFAULT;
if(gettime_local_support){
calculate_time_from_tsc(&ats);
}
else{
ats.tv_sec = 0;
ats.tv_nsec = 0;
}
return timespec_to_jiffy(&ats);
}
SYSCALL_DECLARE(kill)
{
int pid = ihk_mc_syscall_arg0(ctx);
@@ -2077,6 +2125,24 @@ SYSCALL_DECLARE(tgkill)
return do_kill(thread, tgid, tid, sig, &info, 0);
}
SYSCALL_DECLARE(tkill)
{
int tid = ihk_mc_syscall_arg0(ctx);
int sig = ihk_mc_syscall_arg1(ctx);
struct thread *thread = cpu_local_var(current);
struct siginfo info;
memset(&info, '\0', sizeof info);
info.si_signo = sig;
info.si_code = SI_TKILL;
info._sifields._kill.si_pid = thread->proc->pid;
if(tid <= 0)
return -EINVAL;
return do_kill(thread, -1, tid, sig, &info, 0);
}
int *
getcred(int *_buf)
{
@@ -2359,34 +2425,85 @@ do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
return 0;
}
SYSCALL_DECLARE(read)
{
int fd = ihk_mc_syscall_arg0(ctx);
long rc;
struct thread *thread = cpu_local_var(current);
struct process *proc = thread->proc;
struct mckfd *fdp;
long irqstate;
irqstate = ihk_mc_spinlock_lock(&proc->mckfd_lock);
for(fdp = proc->mckfd; fdp; fdp = fdp->next)
if(fdp->fd == fd)
break;
ihk_mc_spinlock_unlock(&proc->mckfd_lock, irqstate);
if(fdp && fdp->read_cb){
kprintf("read: found system fd %d\n", fd);
rc = fdp->read_cb(fdp, ctx);
}
else{
rc = syscall_generic_forwarding(__NR_read, ctx);
}
return rc;
}
SYSCALL_DECLARE(ioctl)
{
int fd = ihk_mc_syscall_arg0(ctx);
long rc;
struct thread *thread = cpu_local_var(current);
struct process *proc = thread->proc;
struct mckfd *fdp;
long irqstate;
irqstate = ihk_mc_spinlock_lock(&proc->mckfd_lock);
for(fdp = proc->mckfd; fdp; fdp = fdp->next)
if(fdp->fd == fd)
break;
ihk_mc_spinlock_unlock(&proc->mckfd_lock, irqstate);
if(fdp && fdp->ioctl_cb){
kprintf("ioctl: found system fd %d\n", fd);
rc = fdp->ioctl_cb(fdp, ctx);
}
else{
rc = syscall_generic_forwarding(__NR_ioctl, ctx);
}
return rc;
}
SYSCALL_DECLARE(close)
{
int fd = ihk_mc_syscall_arg0(ctx);
int rc;
long rc;
struct thread *thread = cpu_local_var(current);
struct sigfd *sfd;
struct sigfd *sb;
long irqstate;
struct process *proc = thread->proc;
struct mckfd *fdp;
struct mckfd *fdq;
long irqstate;
irqstate = ihk_mc_spinlock_lock(&thread->sigcommon->lock);
for(sfd = thread->sigcommon->sigfd, sb = NULL; sfd; sb = sfd, sfd = sfd->next)
if(sfd->fd == fd)
irqstate = ihk_mc_spinlock_lock(&proc->mckfd_lock);
for(fdp = proc->mckfd, fdq = NULL; fdp; fdq = fdp, fdp = fdp->next)
if(fdp->fd == fd)
break;
if(sfd){
struct syscall_request request IHK_DMA_ALIGN;
if(sb)
sb->next = sfd->next;
if(fdp){
kprintf("close: found system fd %d pid=%d\n", fd, proc->pid);
if(fdq)
fdq->next = fdp->next;
else
thread->sigcommon->sigfd = sfd->next;
ihk_mc_spinlock_unlock(&thread->sigcommon->lock, irqstate);
request.number = __NR_signalfd4;
request.args[0] = 1;
request.args[1] = sfd->fd;
kfree(sfd);
rc = do_syscall(&request, ihk_mc_get_processor_id(), 0);
proc->mckfd = fdp->next;
ihk_mc_spinlock_unlock(&proc->mckfd_lock, irqstate);
if(fdp->close_cb)
fdp->close_cb(fdp, ctx);
kfree(fdp);
rc = syscall_generic_forwarding(__NR_close, ctx);
}
else{
ihk_mc_spinlock_unlock(&thread->sigcommon->lock, irqstate);
ihk_mc_spinlock_unlock(&proc->mckfd_lock, irqstate);
rc = syscall_generic_forwarding(__NR_close, ctx);
}
return rc;
@@ -2487,7 +2604,8 @@ SYSCALL_DECLARE(signalfd4)
{
int fd = ihk_mc_syscall_arg0(ctx);
struct thread *thread = cpu_local_var(current);
struct sigfd *sfd;
struct process *proc = thread->proc;
struct mckfd *sfd;
long irqstate;
sigset_t *maskp = (sigset_t *)ihk_mc_syscall_arg1(ctx);;
__sigset_t mask;
@@ -2501,10 +2619,9 @@ SYSCALL_DECLARE(signalfd4)
if(flags & ~(SFD_NONBLOCK | SFD_CLOEXEC))
return -EINVAL;
irqstate = ihk_mc_spinlock_lock(&thread->sigcommon->lock);
if(fd == -1){
struct syscall_request request IHK_DMA_ALIGN;
ihk_mc_spinlock_unlock(&thread->sigcommon->lock, irqstate);
request.number = __NR_signalfd4;
request.args[0] = 0;
request.args[1] = flags;
@@ -2512,25 +2629,73 @@ SYSCALL_DECLARE(signalfd4)
if(fd < 0){
return fd;
}
sfd = kmalloc(sizeof(struct sigfd), IHK_MC_AP_NOWAIT);
sfd = kmalloc(sizeof(struct mckfd), IHK_MC_AP_NOWAIT);
if(!sfd)
return -ENOMEM;
sfd->fd = fd;
irqstate = ihk_mc_spinlock_lock(&thread->sigcommon->lock);
sfd->next = thread->sigcommon->sigfd;
thread->sigcommon->sigfd = sfd;
irqstate = ihk_mc_spinlock_lock(&proc->mckfd_lock);
sfd->next = proc->mckfd;
proc->mckfd = sfd;
}
else{
for(sfd = thread->sigcommon->sigfd; sfd; sfd = sfd->next)
irqstate = ihk_mc_spinlock_lock(&proc->mckfd_lock);
for(sfd = proc->mckfd; sfd; sfd = sfd->next)
if(sfd->fd == fd)
break;
if(!sfd){
ihk_mc_spinlock_unlock(&thread->sigcommon->lock, irqstate);
ihk_mc_spinlock_unlock(&proc->mckfd_lock, irqstate);
return -EINVAL;
}
}
memcpy(&sfd->mask, &mask, sizeof mask);
ihk_mc_spinlock_unlock(&thread->sigcommon->lock, irqstate);
memcpy(&sfd->data, &mask, sizeof mask);
ihk_mc_spinlock_unlock(&proc->mckfd_lock, irqstate);
return sfd->fd;
}
static long
perf_event_read(struct mckfd *sfd, ihk_mc_user_context_t *ctx)
{
return 0;
}
static int
perf_event_ioctl(struct mckfd *sfd, ihk_mc_user_context_t *ctx)
{
return 0;
}
static int
perf_event_close(struct mckfd *sfd, ihk_mc_user_context_t *ctx)
{
return 0;
}
SYSCALL_DECLARE(perf_event_open)
{
struct syscall_request request IHK_DMA_ALIGN;
struct thread *thread = cpu_local_var(current);
struct process *proc = thread->proc;
struct mckfd *sfd;
int fd;
long irqstate;
request.number = __NR_perf_event_open;
request.args[0] = 0;
fd = do_syscall(&request, ihk_mc_get_processor_id(), 0);
if(fd < 0){
return fd;
}
sfd = kmalloc(sizeof(struct mckfd), IHK_MC_AP_NOWAIT);
if(!sfd)
return -ENOMEM;
sfd->fd = fd;
sfd->read_cb = perf_event_read;
sfd->ioctl_cb = perf_event_ioctl;
sfd->close_cb = perf_event_close;
irqstate = ihk_mc_spinlock_lock(&proc->mckfd_lock);
sfd->next = proc->mckfd;
proc->mckfd = sfd;
ihk_mc_spinlock_unlock(&proc->mckfd_lock, irqstate);
return sfd->fd;
}
@@ -5073,6 +5238,125 @@ static void calculate_time_from_tsc(struct timespec *ts)
return;
}
SYSCALL_DECLARE(setitimer)
{
int which = (int)ihk_mc_syscall_arg0(ctx);
struct itimerval *new = (struct itimerval *)ihk_mc_syscall_arg1(ctx);
struct itimerval *old = (struct itimerval *)ihk_mc_syscall_arg2(ctx);
struct syscall_request request IHK_DMA_ALIGN;
struct thread *thread = cpu_local_var(current);
int timer_start = 1;
struct itimerval wkval;
struct timeval tv;
if(which != ITIMER_REAL &&
which != ITIMER_VIRTUAL &&
which != ITIMER_PROF)
return -EINVAL;
if(which == ITIMER_REAL){
request.number = __NR_setitimer;
request.args[0] = ihk_mc_syscall_arg0(ctx);
request.args[1] = ihk_mc_syscall_arg1(ctx);
request.args[2] = ihk_mc_syscall_arg2(ctx);
return do_syscall(&request, ihk_mc_get_processor_id(), 0);
}
else if(which == ITIMER_VIRTUAL){
if(old){
memcpy(&wkval, &thread->itimer_virtual, sizeof wkval);
if(wkval.it_value.tv_sec != 0 ||
wkval.it_value.tv_usec != 0){
ts_to_tv(&tv, &thread->itimer_virtual_value);
tv_sub(&wkval.it_value, &tv);
}
if(copy_to_user(old, &wkval, sizeof wkval))
return -EFAULT;
}
if(!new){
return 0;
}
if(copy_from_user(&thread->itimer_virtual, new, sizeof(struct itimerval)))
thread->itimer_virtual_value.tv_sec = 0;
thread->itimer_virtual_value.tv_nsec = 0;
if(thread->itimer_virtual.it_value.tv_sec == 0 &&
thread->itimer_virtual.it_value.tv_usec == 0)
timer_start = 0;
}
else if(which == ITIMER_PROF){
if(old){
memcpy(&wkval, &thread->itimer_prof, sizeof wkval);
if(wkval.it_value.tv_sec != 0 ||
wkval.it_value.tv_usec != 0){
ts_to_tv(&tv, &thread->itimer_prof_value);
tv_sub(&wkval.it_value, &tv);
}
if(copy_to_user(old, &wkval, sizeof wkval))
return -EFAULT;
}
if(!new){
return 0;
}
if(copy_from_user(&thread->itimer_prof, new, sizeof(struct itimerval)))
thread->itimer_prof_value.tv_sec = 0;
thread->itimer_prof_value.tv_nsec = 0;
if(thread->itimer_prof.it_value.tv_sec == 0 &&
thread->itimer_prof.it_value.tv_usec == 0)
timer_start = 0;
}
thread->itimer_enabled = timer_start;
set_timer();
return 0;
}
SYSCALL_DECLARE(getitimer)
{
int which = (int)ihk_mc_syscall_arg0(ctx);
struct itimerval *old = (struct itimerval *)ihk_mc_syscall_arg1(ctx);
struct syscall_request request IHK_DMA_ALIGN;
struct thread *thread = cpu_local_var(current);
struct itimerval wkval;
struct timeval tv;
if(which != ITIMER_REAL &&
which != ITIMER_VIRTUAL &&
which != ITIMER_PROF)
return -EINVAL;
if(which == ITIMER_REAL){
request.number = __NR_getitimer;
request.args[0] = ihk_mc_syscall_arg0(ctx);
request.args[1] = ihk_mc_syscall_arg1(ctx);
return do_syscall(&request, ihk_mc_get_processor_id(), 0);
}
else if(which == ITIMER_VIRTUAL){
if(old){
memcpy(&wkval, &thread->itimer_virtual, sizeof wkval);
if(wkval.it_value.tv_sec != 0 ||
wkval.it_value.tv_usec != 0){
ts_to_tv(&tv, &thread->itimer_virtual_value);
tv_sub(&wkval.it_value, &tv);
}
if(copy_to_user(old, &wkval, sizeof wkval))
return -EFAULT;
}
}
else if(which == ITIMER_PROF){
if(old){
memcpy(&wkval, &thread->itimer_prof, sizeof wkval);
if(wkval.it_value.tv_sec != 0 ||
wkval.it_value.tv_usec != 0){
ts_to_tv(&tv, &thread->itimer_prof_value);
tv_sub(&wkval.it_value, &tv);
}
if(copy_to_user(old, &wkval, sizeof wkval))
return -EFAULT;
}
}
return 0;
}
SYSCALL_DECLARE(clock_gettime)
{
/* TODO: handle clock_id */
@@ -5096,6 +5380,41 @@ SYSCALL_DECLARE(clock_gettime)
dkprintf("clock_gettime(): %d\n", error);
return error;
}
else if(clock_id == CLOCK_PROCESS_CPUTIME_ID){
struct thread *thread = cpu_local_var(current);
struct process *proc = thread->proc;
struct thread *child;
struct mcs_rwlock_node lock;
mcs_rwlock_reader_lock_noirq(&proc->children_lock, &lock);
list_for_each_entry(child, &proc->threads_list, siblings_list){
if(child != thread &&
child->status == PS_RUNNING &&
!child->in_kernel){
child->times_update = 0;
ihk_mc_interrupt_cpu(get_x86_cpu_local_variable(child->cpu_id)->apic_id, 0xd1);
}
}
ats.tv_sec = proc->utime.tv_sec;
ats.tv_nsec = proc->utime.tv_nsec;
ts_add(&ats, &proc->stime);
list_for_each_entry(child, &proc->threads_list, siblings_list){
while(!child->times_update)
cpu_pause();
ts_add(&ats, &child->utime);
ts_add(&ats, &child->stime);
}
mcs_rwlock_reader_unlock_noirq(&proc->children_lock, &lock);
return copy_to_user(ts, &ats, sizeof ats);
}
else if(clock_id == CLOCK_THREAD_CPUTIME_ID){
struct thread *thread = cpu_local_var(current);
ats.tv_sec = thread->utime.tv_sec;
ats.tv_nsec = thread->utime.tv_nsec;
ts_add(&ats, &thread->stime);
return copy_to_user(ts, &ats, sizeof ats);
}
/* Otherwise offload */
request.number = __NR_clock_gettime;
@@ -6489,13 +6808,136 @@ SYSCALL_DECLARE(pmc_reset)
return ihk_mc_perfctr_reset(counter);
}
void
reset_cputime()
{
struct thread *thread;
if(clv == NULL)
return;
if(!(thread = cpu_local_var(current)))
return;
thread->btime.tv_sec = 0;
thread->btime.tv_nsec = 0;
}
/**
* mode == 0: kernel -> user
* mode == 1: user -> kernel
* mode == 2: kernel -> kernel
*/
void
set_cputime(int mode)
{
struct thread *thread;
struct timespec ats;
struct cpu_local_var *v;
if(clv == NULL)
return;
v = get_this_cpu_local_var();
if(!(thread = v->current))
return;
if(!gettime_local_support){
thread->times_update = 1;
return;
}
calculate_time_from_tsc(&ats);
if(thread->btime.tv_sec != 0 && thread->btime.tv_nsec != 0){
struct timespec dts;
dts.tv_sec = ats.tv_sec;
dts.tv_nsec = ats.tv_nsec;
ts_sub(&dts, &thread->btime);
if(mode == 1){
ts_add(&thread->utime, &dts);
ts_add(&thread->itimer_virtual_value, &dts);
ts_add(&thread->itimer_prof_value, &dts);
}
else{
ts_add(&thread->stime, &dts);
ts_add(&thread->itimer_prof_value, &dts);
}
}
if(mode == 2){
thread->btime.tv_sec = 0;
thread->btime.tv_nsec = 0;
}
else{
thread->btime.tv_sec = ats.tv_sec;
thread->btime.tv_nsec = ats.tv_nsec;
}
thread->times_update = 1;
thread->in_kernel = mode;
if(thread->itimer_enabled){
struct timeval tv;
int ev = 0;
if(thread->itimer_virtual.it_value.tv_sec != 0 ||
thread->itimer_virtual.it_value.tv_usec){
ts_to_tv(&tv, &thread->itimer_virtual_value);
tv_sub(&tv, &thread->itimer_virtual.it_value);
if(tv.tv_sec > 0 ||
(tv.tv_sec == 0 &&
tv.tv_usec > 0)){
thread->itimer_virtual_value.tv_sec = 0;
thread->itimer_virtual_value.tv_nsec = 0;
thread->itimer_virtual.it_value.tv_sec =
thread->itimer_virtual.it_interval.tv_sec;
thread->itimer_virtual.it_value.tv_usec =
thread->itimer_virtual.it_interval.tv_usec;
do_kill(thread, thread->proc->pid, thread->tid,
SIGVTALRM, NULL, 0);
ev = 1;
}
}
if(thread->itimer_prof.it_value.tv_sec != 0 ||
thread->itimer_prof.it_value.tv_usec){
ts_to_tv(&tv, &thread->itimer_prof_value);
tv_sub(&tv, &thread->itimer_prof.it_value);
if(tv.tv_sec > 0 ||
(tv.tv_sec == 0 &&
tv.tv_usec > 0)){
thread->itimer_prof_value.tv_sec = 0;
thread->itimer_prof_value.tv_nsec = 0;
thread->itimer_prof.it_value.tv_sec =
thread->itimer_prof.it_interval.tv_sec;
thread->itimer_prof.it_value.tv_usec =
thread->itimer_prof.it_interval.tv_usec;
do_kill(thread, thread->proc->pid, thread->tid,
SIGPROF, NULL, 0);
ev = 1;
}
}
if(ev){
if(thread->itimer_virtual.it_value.tv_sec == 0 &&
thread->itimer_virtual.it_value.tv_usec == 0 &&
thread->itimer_prof.it_value.tv_sec == 0 &&
thread->itimer_prof.it_value.tv_usec == 0){
thread->itimer_enabled = 0;
set_timer();
}
}
}
}
long syscall(int num, ihk_mc_user_context_t *ctx)
{
long l;
set_cputime(1);
if(cpu_local_var(current)->proc->status == PS_EXITED &&
(num != __NR_exit && num != __NR_exit_group)){
check_signal(-EINVAL, NULL, 0);
set_cputime(0);
return -EINVAL;
}
@@ -6554,5 +6996,6 @@ long syscall(int num, ihk_mc_user_context_t *ctx)
ptrace_syscall_exit(cpu_local_var(current));
}
set_cputime(0);
return l;
}