From 040fb64b2240a3b3ab354ade18eb1ab8cff52a0f Mon Sep 17 00:00:00 2001 From: Tomoki Shirasawa Date: Mon, 2 Sep 2013 00:39:22 +0900 Subject: [PATCH] add terminate thread --- executer/include/uprotocol.h | 1 + executer/kernel/control.c | 20 +++++++++++++++ executer/kernel/driver.c | 1 + executer/kernel/ikc.c | 44 ++++++++++++++++++++++++++++----- executer/kernel/mcctrl.h | 1 + executer/user/mcexec.c | 24 ++++++++++++------ kernel/host.c | 40 ++++++++++++++++++++++++------ kernel/include/cls.h | 5 +++- kernel/include/init.h | 1 + kernel/init.c | 1 + kernel/syscall.c | 48 +++++++++++++++++++++++++++--------- 11 files changed, 154 insertions(+), 32 deletions(-) diff --git a/executer/include/uprotocol.h b/executer/include/uprotocol.h index d7b82147..607e514b 100644 --- a/executer/include/uprotocol.h +++ b/executer/include/uprotocol.h @@ -8,6 +8,7 @@ #define MCEXEC_UP_RET_SYSCALL 0x30a02904 #define MCEXEC_UP_LOAD_SYSCALL 0x30a02905 #define MCEXEC_UP_SEND_SIGNAL 0x30a02906 +#define MCEXEC_UP_GET_CPU 0x30a02907 #define MCEXEC_UP_PREPARE_DMA 0x30a02910 #define MCEXEC_UP_FREE_DMA 0x30a02911 diff --git a/executer/kernel/control.c b/executer/kernel/control.c index 22c9230f..cdf7ed9c 100644 --- a/executer/kernel/control.c +++ b/executer/kernel/control.c @@ -225,6 +225,23 @@ static long mcexec_send_signal(ihk_os_t os, unsigned long sigparam) return 0; } +static long mcexec_get_cpu(ihk_os_t os) +{ + struct ihk_cpu_info *info; + + info = ihk_os_get_cpu_info(os); + if (!info) { + printk("Error: cannot retrieve CPU info.\n"); + return -EINVAL; + } + if (info->n_cpus < 1) { + printk("Error: # of cpu is invalid.\n"); + return -EINVAL; + } + + return info->n_cpus; +} + int mcexec_syscall(struct mcctrl_channel *c, unsigned long arg) { c->req = 1; @@ -555,6 +572,9 @@ long __mcctrl_control(ihk_os_t os, unsigned int req, unsigned long arg) case MCEXEC_UP_SEND_SIGNAL: return mcexec_send_signal(os, arg); + case MCEXEC_UP_GET_CPU: + return mcexec_get_cpu(os); + case MCEXEC_UP_PREPARE_DMA: return mcexec_pin_region(os, (unsigned long *)arg); diff --git a/executer/kernel/driver.c b/executer/kernel/driver.c index 2aac296e..f56a9618 100644 --- a/executer/kernel/driver.c +++ b/executer/kernel/driver.c @@ -32,6 +32,7 @@ static struct ihk_os_user_call_handler mcctrl_uchs[] = { { .request = MCEXEC_UP_RET_SYSCALL, .func = mcctrl_ioctl }, { .request = MCEXEC_UP_LOAD_SYSCALL, .func = mcctrl_ioctl }, { .request = MCEXEC_UP_SEND_SIGNAL, .func = mcctrl_ioctl }, + { .request = MCEXEC_UP_GET_CPU, .func = mcctrl_ioctl }, { .request = MCEXEC_UP_PREPARE_DMA, .func = mcctrl_ioctl }, { .request = MCEXEC_UP_FREE_DMA, .func = mcctrl_ioctl }, }; diff --git a/executer/kernel/ikc.c b/executer/kernel/ikc.c index 85c0353a..36f8d0c1 100644 --- a/executer/kernel/ikc.c +++ b/executer/kernel/ikc.c @@ -16,7 +16,7 @@ //struct mcctrl_channel *channels; void mcexec_prepare_ack(ihk_os_t os, unsigned long arg, int err); -static void mcctrl_ikc_init(ihk_os_t os, int cpu, unsigned long rphys); +static void mcctrl_ikc_init(ihk_os_t os, int cpu, unsigned long rphys, struct ihk_ikc_channel_desc *c); int mcexec_syscall(struct mcctrl_channel *c, unsigned long arg); static int syscall_packet_handler(struct ihk_ikc_channel_desc *c, @@ -27,7 +27,7 @@ static int syscall_packet_handler(struct ihk_ikc_channel_desc *c, switch (pisp->msg) { case SCD_MSG_INIT_CHANNEL: - mcctrl_ikc_init(__os, pisp->ref, pisp->arg); + mcctrl_ikc_init(__os, pisp->ref, pisp->arg, c); break; case SCD_MSG_PREPARE_PROCESS_ACKED: @@ -97,7 +97,7 @@ int mcctrl_ikc_is_valid_thread(ihk_os_t os, int cpu) //unsigned long *mcctrl_doorbell_va; //unsigned long mcctrl_doorbell_pa; -static void mcctrl_ikc_init(ihk_os_t os, int cpu, unsigned long rphys) +static void mcctrl_ikc_init(ihk_os_t os, int cpu, unsigned long rphys, struct ihk_ikc_channel_desc *c) { struct mcctrl_usrdata *usrdata = ihk_host_os_get_usrdata(os); struct ikc_scd_packet packet; @@ -105,11 +105,14 @@ static void mcctrl_ikc_init(ihk_os_t os, int cpu, unsigned long rphys) unsigned long phys; struct ikc_scd_init_param *rpm; + if(c->port == 502) + pmc = usrdata->channels + usrdata->num_channels - 1; + if (!pmc) { return; } - printk("IKC init: %d\n", cpu); + printk("IKC init: cpu=%d port=%d\n", cpu, c->port); phys = ihk_device_map_memory(ihk_os_to_dev(os), rphys, sizeof(struct ikc_scd_init_param)); @@ -196,7 +199,26 @@ static int connect_handler(struct ihk_ikc_channel_info *param) init_waitqueue_head(&usrdata->channels[cpu].wq_syscall); usrdata->channels[cpu].c = c; - kprintf("syscall: MC CPU %d connected.\n", cpu); + kprintf("syscall: MC CPU %d connected. c=%p\n", cpu, c); + + return 0; +} + +static int connect_handler2(struct ihk_ikc_channel_info *param) +{ + struct ihk_ikc_channel_desc *c; + int cpu; + ihk_os_t os = param->channel->remote_os; + struct mcctrl_usrdata *usrdata = ihk_host_os_get_usrdata(os); + + c = param->channel; + cpu = usrdata->num_channels - 1; + + param->packet_handler = syscall_packet_handler; + init_waitqueue_head(&usrdata->channels[cpu].wq_syscall); + + usrdata->channels[cpu].c = c; + kprintf("syscall: MC CPU %d connected. c=%p\n", cpu, c); return 0; } @@ -209,6 +231,14 @@ static struct ihk_ikc_listen_param listen_param = { .magic = 0x1129, }; +static struct ihk_ikc_listen_param listen_param2 = { + .port = 502, + .handler = connect_handler2, + .pkt_size = sizeof(struct ikc_scd_packet), + .queue_size = PAGE_SIZE, + .magic = 0x1329, +}; + int prepare_ikc_channels(ihk_os_t os) { struct ihk_cpu_info *info; @@ -229,7 +259,7 @@ int prepare_ikc_channels(ihk_os_t os) return -EINVAL; } - usrdata->num_channels = info->n_cpus; + usrdata->num_channels = info->n_cpus + 1; usrdata->channels = kzalloc(sizeof(struct mcctrl_channel) * usrdata->num_channels, GFP_KERNEL); if (!usrdata->channels) { @@ -242,6 +272,8 @@ int prepare_ikc_channels(ihk_os_t os) ihk_host_os_set_usrdata(os, usrdata); memcpy(&usrdata->listen_param, &listen_param, sizeof listen_param); ihk_ikc_listen_port(os, &usrdata->listen_param); + memcpy(&usrdata->listen_param2, &listen_param2, sizeof listen_param2); + ihk_ikc_listen_port(os, &usrdata->listen_param2); error = init_peer_channel_registry(usrdata); if (error) { diff --git a/executer/kernel/mcctrl.h b/executer/kernel/mcctrl.h index f5e90835..b8c1eadd 100644 --- a/executer/kernel/mcctrl.h +++ b/executer/kernel/mcctrl.h @@ -69,6 +69,7 @@ struct mcctrl_channel { struct mcctrl_usrdata { struct ihk_ikc_listen_param listen_param; + struct ihk_ikc_listen_param listen_param2; ihk_os_t os; int num_channels; struct mcctrl_channel *channels; diff --git a/executer/user/mcexec.c b/executer/user/mcexec.c index 3a19e56f..6b7c04d2 100644 --- a/executer/user/mcexec.c +++ b/executer/user/mcexec.c @@ -315,7 +315,7 @@ int flatten_strings(int nr_strings, char **strings, char **flat) return full_len; } -#define NUM_HANDLER_THREADS 248 +//#define NUM_HANDLER_THREADS 248 struct thread_data_s { pthread_t thread_id; @@ -323,7 +323,7 @@ struct thread_data_s { int cpu; int ret; pthread_mutex_t *lock; -} thread_data[NUM_HANDLER_THREADS]; +} *thread_data; static void *main_loop_thread_func(void *arg) { @@ -362,6 +362,7 @@ int main(int argc, char **argv) char **a; char *p; int i; + int ncpu; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; #ifdef USE_SYSCALL_MOD_CALL @@ -415,6 +416,15 @@ int main(int argc, char **argv) return 1; } + ncpu = ioctl(fd, MCEXEC_UP_GET_CPU, 0); + if(ncpu == -1){ + fprintf(stderr, "No CPU found.\n"); + return 1; + } + + thread_data = (struct thread_data_s *)malloc(sizeof(struct thread_data_s) * (ncpu + 1)); + memset(thread_data, '\0', sizeof(struct thread_data_s) * (ncpu + 1)); + #if 0 fdm = open("/dev/fmem", O_RDWR); if (fdm < 0) { @@ -483,7 +493,7 @@ int main(int argc, char **argv) __dprint("mccmd server initialized\n"); #endif - for (i = 0; i < NUM_HANDLER_THREADS; ++i) { + for (i = 0; i <= ncpu; ++i) { int ret; thread_data[i].fd = fd; @@ -509,7 +519,7 @@ int main(int argc, char **argv) i != SIGTSTP && i != SIGTTIN && i != SIGTTOU) signal(i, sendsig); - for (i = 0; i < NUM_HANDLER_THREADS; ++i) { + for (i = 0; i <= ncpu; ++i) { pthread_join(thread_data[i].thread_id, NULL); } @@ -584,7 +594,7 @@ int main_loop(int fd, int cpu, pthread_mutex_t *lock) if (!(w.sr.number == __NR_write && w.sr.args[0] == 1)) __dprintf("[%d] got syscall: %ld\n", cpu, w.sr.number); - pthread_mutex_lock(lock); + //pthread_mutex_lock(lock); switch (w.sr.number) { case __NR_open: @@ -652,7 +662,7 @@ int main_loop(int fd, int cpu, pthread_mutex_t *lock) #endif exit(0); - pthread_mutex_unlock(lock); + //pthread_mutex_unlock(lock); return w.sr.args[0]; case __NR_mmap: @@ -676,7 +686,7 @@ int main_loop(int fd, int cpu, pthread_mutex_t *lock) } - pthread_mutex_unlock(lock); + //pthread_mutex_unlock(lock); } __dprint("timed out.\n"); return 1; diff --git a/kernel/host.c b/kernel/host.c index 5e3651c9..a6d5859c 100644 --- a/kernel/host.c +++ b/kernel/host.c @@ -300,11 +300,8 @@ err: return -ENOMEM; } -static void process_msg_init(struct ikc_scd_init_param *pcp) +static void process_msg_init(struct ikc_scd_init_param *pcp, struct syscall_params *lparam) { - struct syscall_params *lparam; - - lparam = &cpu_local_var(scp); lparam->response_va = allocate_pages(RESPONSE_PAGE_COUNT, 0); lparam->response_pa = virt_to_phys(lparam->response_va); @@ -313,12 +310,14 @@ static void process_msg_init(struct ikc_scd_init_param *pcp) pcp->response_page = lparam->response_pa; } -static void process_msg_init_acked(unsigned long pphys) +static void process_msg_init_acked(struct ihk_ikc_channel_desc *c, unsigned long pphys) { struct ikc_scd_init_param *param = (void *)pphys; struct syscall_params *lparam; lparam = &cpu_local_var(scp); + if(cpu_local_var(syscall_channel2) == c) + lparam = &cpu_local_var(scp2); lparam->request_rpa = param->request_page; lparam->request_pa = ihk_mc_map_memory(NULL, param->request_page, REQUEST_PAGE_COUNT * PAGE_SIZE); @@ -380,7 +379,7 @@ static int syscall_packet_handler(struct ihk_ikc_channel_desc *c, switch (packet->msg) { case SCD_MSG_INIT_CHANNEL_ACKED: dkprintf("SCD_MSG_INIT_CHANNEL_ACKED\n"); - process_msg_init_acked(packet->arg); + process_msg_init_acked(c, packet->arg); return 0; case SCD_MSG_PREPARE_PROCESS: @@ -434,9 +433,36 @@ void init_host_syscall_channel(void) get_this_cpu_local_var()->syscall_channel = param.channel; - process_msg_init(&cpu_local_var(iip)); + process_msg_init(&cpu_local_var(iip), &cpu_local_var(scp)); pckt.msg = SCD_MSG_INIT_CHANNEL; pckt.ref = ihk_mc_get_processor_id(); pckt.arg = virt_to_phys(&cpu_local_var(iip)); syscall_channel_send(param.channel, &pckt); } + +void init_host_syscall_channel2(void) +{ + struct ihk_ikc_connect_param param; + struct ikc_scd_packet pckt; + + param.port = 502; + param.pkt_size = sizeof(struct ikc_scd_packet); + param.queue_size = PAGE_SIZE; + param.magic = 0x1329; + param.handler = syscall_packet_handler; + + dkprintf("(syscall) Trying to connect host ..."); + while (ihk_ikc_connect(NULL, ¶m) != 0) { + dkprintf("."); + ihk_mc_delay_us(1000 * 1000); + } + dkprintf("connected.\n"); + + get_this_cpu_local_var()->syscall_channel2 = param.channel; + + process_msg_init(&cpu_local_var(iip2), &cpu_local_var(scp2)); + pckt.msg = SCD_MSG_INIT_CHANNEL; + pckt.ref = ihk_mc_get_processor_id(); + pckt.arg = virt_to_phys(&cpu_local_var(iip2)); + syscall_channel_send(param.channel, &pckt); +} diff --git a/kernel/include/cls.h b/kernel/include/cls.h index 02cdde11..3086195b 100644 --- a/kernel/include/cls.h +++ b/kernel/include/cls.h @@ -31,9 +31,12 @@ struct cpu_local_var { size_t runq_len; struct ihk_ikc_channel_desc *syscall_channel; - struct syscall_params scp; struct ikc_scd_init_param iip; + + struct ihk_ikc_channel_desc *syscall_channel2; + struct syscall_params scp2; + struct ikc_scd_init_param iip2; int status; int fs; diff --git a/kernel/include/init.h b/kernel/include/init.h index 28f140b5..5febeec9 100644 --- a/kernel/include/init.h +++ b/kernel/include/init.h @@ -13,6 +13,7 @@ extern void kmalloc_init(void); extern void ap_start(void); extern void ihk_mc_dma_init(void); extern void init_host_syscall_channel(void); +extern void init_host_syscall_channel2(void); extern void sched_init(void); extern void pc_ap_init(void); diff --git a/kernel/init.c b/kernel/init.c index d26eecc7..b0249937 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -188,6 +188,7 @@ static void post_init(void) if (find_command_line("hidos")) { init_host_syscall_channel(); + init_host_syscall_channel2(); } ap_start(); } diff --git a/kernel/syscall.c b/kernel/syscall.c index de757c78..cb7d78c2 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -75,47 +75,73 @@ static void do_mod_exit(int status); static void send_syscall(struct syscall_request *req) { struct ikc_scd_packet packet; - struct syscall_response *res = cpu_local_var(scp).response_va; + struct syscall_response *res; unsigned long fin; int w; + struct syscall_params *scp; + struct ihk_ikc_channel_desc *syscall_channel; + int cpu; + + if(req->number == __NR_exit_group){ + extern int num_processors; + + scp = &get_cpu_local_var(0)->scp2; + syscall_channel = get_cpu_local_var(0)->syscall_channel2; + cpu = num_processors; + } + else{ + scp = &cpu_local_var(scp); + syscall_channel = cpu_local_var(syscall_channel); + cpu = ihk_mc_get_processor_id(); + } + res = scp->response_va; res->status = 0; req->valid = 0; - memcpy_async(cpu_local_var(scp).request_pa, + memcpy_async(scp->request_pa, virt_to_phys(req), sizeof(*req), 0, &fin); - memcpy_async_wait(&cpu_local_var(scp).post_fin); - cpu_local_var(scp).post_va->v[0] = cpu_local_var(scp).post_idx; + memcpy_async_wait(&scp->post_fin); + scp->post_va->v[0] = scp->post_idx; - w = ihk_mc_get_processor_id() + 1; + w = cpu + 1; memcpy_async_wait(&fin); barrier(); - cpu_local_var(scp).request_va->valid = 1; - *(unsigned int *)cpu_local_var(scp).doorbell_va = w; + scp->request_va->valid = 1; + *(unsigned int *)scp->doorbell_va = w; #ifdef SYSCALL_BY_IKC packet.msg = SCD_MSG_SYSCALL_ONESIDE; - packet.ref = ihk_mc_get_processor_id(); - packet.arg = cpu_local_var(scp).request_rpa; + packet.ref = cpu; + packet.arg = scp->request_rpa; - ihk_ikc_send(cpu_local_var(syscall_channel), &packet, 0); + ihk_ikc_send(syscall_channel, &packet, 0); #endif } int do_syscall(struct syscall_request *req, ihk_mc_user_context_t *ctx) { - struct syscall_response *res = cpu_local_var(scp).response_va; + struct syscall_response *res; struct syscall_request req2; + struct syscall_params *scp; int error; dkprintf("SC(%d)[%3d] sending syscall\n", ihk_mc_get_processor_id(), req->number); + if(req->number == __NR_exit_group){ + scp = &get_cpu_local_var(0)->scp2; + } + else{ + scp = &cpu_local_var(scp); + } + res = scp->response_va; + send_syscall(req); dkprintf("SC(%d)[%3d] waiting for host.. \n",