| 1 | /* | 
|---|
| 2 |  * kern/sys_thread_migrate.c - calls the scheduler to yield current CPU | 
|---|
| 3 |  *  | 
|---|
| 4 |  * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless | 
|---|
| 5 |  * Copyright (c) 2011,2012 UPMC Sorbonne Universites | 
|---|
| 6 |  * | 
|---|
| 7 |  * This file is part of ALMOS-kernel. | 
|---|
| 8 |  * | 
|---|
| 9 |  * ALMOS-kernel is free software; you can redistribute it and/or modify it | 
|---|
| 10 |  * under the terms of the GNU General Public License as published by | 
|---|
| 11 |  * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 12 |  * | 
|---|
| 13 |  * ALMOS-kernel is distributed in the hope that it will be useful, but | 
|---|
| 14 |  * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 16 |  * General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  * You should have received a copy of the GNU General Public License | 
|---|
| 19 |  * along with ALMOS-kernel; if not, write to the Free Software Foundation, | 
|---|
| 20 |  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 21 |  */ | 
|---|
| 22 |  | 
|---|
| 23 | #include <cpu.h> | 
|---|
| 24 | #include <thread.h> | 
|---|
| 25 | #include <task.h> | 
|---|
| 26 | #include <scheduler.h> | 
|---|
| 27 |  | 
|---|
| 28 | #define MAX_CPU_NR (CONFIG_MAX_CLUSTER_NR * CONFIG_MAX_CPU_PER_CLUSTER_NR) | 
|---|
| 29 |  | 
|---|
| 30 | int sys_thread_migrate(pthread_attr_t *thread_attr) | 
|---|
| 31 | { | 
|---|
| 32 |         struct thread_s *this; | 
|---|
| 33 |         pthread_attr_t attr; | 
|---|
| 34 |         uint_t mode; | 
|---|
| 35 |         sint_t cpu_gid; | 
|---|
| 36 |         error_t err; | 
|---|
| 37 |  | 
|---|
| 38 |         this = current_thread; | 
|---|
| 39 |  | 
|---|
| 40 |         if(thread_attr == NULL) | 
|---|
| 41 |         { | 
|---|
| 42 |                 err = EINVAL; | 
|---|
| 43 |                 goto fail_inval; | 
|---|
| 44 |         } | 
|---|
| 45 |  | 
|---|
| 46 |         if(NOT_IN_USPACE((uint_t)thread_attr)) | 
|---|
| 47 |         { | 
|---|
| 48 |                 err = EPERM; | 
|---|
| 49 |                 goto fail_access; | 
|---|
| 50 |         } | 
|---|
| 51 |  | 
|---|
| 52 |         if((err = cpu_copy_from_uspace(&attr, thread_attr, sizeof(attr)))) | 
|---|
| 53 |                 goto fail_ucopy_attr; | 
|---|
| 54 |  | 
|---|
| 55 |         cpu_gid = (sint_t)cpu_get_id(); | 
|---|
| 56 |  | 
|---|
| 57 |         if(attr.cpu_gid == cpu_gid) | 
|---|
| 58 |         { | 
|---|
| 59 |                 thread_migration_disable(this); | 
|---|
| 60 |                 return 0; | 
|---|
| 61 |         } | 
|---|
| 62 |  | 
|---|
| 63 |         if((attr.cpu_gid > 0) && (attr.cpu_gid >= MAX_CPU_NR)) | 
|---|
| 64 |         { | 
|---|
| 65 |                 err = EINVAL; | 
|---|
| 66 |                 goto fail_attr_inval; | 
|---|
| 67 |         } | 
|---|
| 68 |  | 
|---|
| 69 |         cpu_gid = (attr.cpu_gid < 0) ? -1 : (attr.cpu_gid % arch_onln_cpu_nr()); | 
|---|
| 70 |  | 
|---|
| 71 |         if(attr.flags & PT_ATTR_AUTO_MGRT) | 
|---|
| 72 |                 this->info.attr.flags |= PT_ATTR_AUTO_MGRT; | 
|---|
| 73 |  | 
|---|
| 74 | #if CONFIG_SHOW_SYSMGRT_MSG | 
|---|
| 75 |         printk(INFO, "INFO: %s: pid %d, tid %d (%x), cpu %d: asked to migrate to cpu %d [%u]\n", | 
|---|
| 76 |                __FUNCTION__, | 
|---|
| 77 |                this->task->pid, | 
|---|
| 78 |                this->info.order, | 
|---|
| 79 |                this, | 
|---|
| 80 |                cpu_get_id(), | 
|---|
| 81 |                cpu_gid, | 
|---|
| 82 |                cpu_time_stamp()); | 
|---|
| 83 | #endif | 
|---|
| 84 |  | 
|---|
| 85 |         cpu_disable_all_irq(&mode); | 
|---|
| 86 |         thread_clear_cap_migrate(this); | 
|---|
| 87 |         cpu_restore_irq(mode); | 
|---|
| 88 |  | 
|---|
| 89 |         err = thread_migrate(this, cpu_gid); | 
|---|
| 90 |  | 
|---|
| 91 |         /* Reload this pointer */ | 
|---|
| 92 |         this = current_thread; | 
|---|
| 93 |  | 
|---|
| 94 |         cpu_disable_all_irq(&mode); | 
|---|
| 95 |         thread_set_cap_migrate(this); | 
|---|
| 96 |         thread_migration_deactivate(this); | 
|---|
| 97 |         cpu_restore_irq(mode); | 
|---|
| 98 |  | 
|---|
| 99 | #if CONFIG_SHOW_SYSMGRT_MSG | 
|---|
| 100 |         printk(INFO, "INFO: %s: pid %d, tid %d (%x), cpu %d, err %d, done [%u]\n",  | 
|---|
| 101 |                __FUNCTION__, | 
|---|
| 102 |                this->task->pid, | 
|---|
| 103 |                this->info.order, | 
|---|
| 104 |                current_thread, | 
|---|
| 105 |                cpu_get_id(), | 
|---|
| 106 |                err, | 
|---|
| 107 |                cpu_time_stamp()); | 
|---|
| 108 | #endif | 
|---|
| 109 |  | 
|---|
| 110 | fail_attr_inval: | 
|---|
| 111 | fail_ucopy_attr: | 
|---|
| 112 | fail_access: | 
|---|
| 113 | fail_inval: | 
|---|
| 114 |         this->info.errno = err; | 
|---|
| 115 |         return (err) ? -1 : 0; | 
|---|
| 116 | } | 
|---|