1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
/*
* Copyright (C) 2003 Philippe Gerum <rpm@xenomai.org>.
*
* Xenomai is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Xenomai is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xenomai; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <xenomai/pod.h>
#include <xenomai/heap.h>
#include <xenomai/shadow.h>
#include <xenomai/mutex.h>
#include <xenomai/fusion.h>
static int __fusion_muxid;
static xnsynch_t __fusion_barrier;
extern xnmutex_t __imutex;
static inline xnthread_t *__pthread_find_by_handle (struct task_struct *curr, void *khandle)
{
xnthread_t *thread;
if (!khandle)
return NULL;
/* FIXME: we should check if khandle is laid in the system
heap, or at least in kernel space... */
thread = (xnthread_t *)khandle;
if (xnthread_magic(thread) != FUSION_SKIN_MAGIC)
/* FIXME: We should kill all VM threads at once when a signal
is caught for one of them. */
return NULL;
return thread;
}
static int __pthread_shadow_helper (struct task_struct *curr,
struct pt_regs *regs,
pid_t syncpid,
int *u_syncp)
{
char name[XNOBJECT_NAME_LEN];
xnthread_t *thread;
if (__xn_reg_arg2(regs) &&
!__xn_access_ok(curr,VERIFY_WRITE,__xn_reg_arg2(regs),sizeof(thread)))
return -EFAULT;
thread = (xnthread_t *)xnmalloc(sizeof(*thread));
if (!thread)
return -ENOMEM;
if (__xn_reg_arg1(regs))
{
if (!__xn_access_ok(curr,VERIFY_READ,__xn_reg_arg1(regs),sizeof(name)))
{
xnfree(thread);
return -EFAULT;
}
__xn_copy_from_user(curr,name,(const char *)__xn_reg_arg1(regs),sizeof(name) - 1);
name[sizeof(name) - 1] = '\0';
strncpy(curr->comm,name,sizeof(curr->comm));
curr->comm[sizeof(curr->comm) - 1] = '\0';
}
else
{
strncpy(name,curr->comm,sizeof(name) - 1);
name[sizeof(name) - 1] = '\0';
}
if (__xn_reg_arg2(regs))
__xn_copy_to_user(curr,(void *)__xn_reg_arg2(regs),&thread,sizeof(thread));
xnthread_extended_info(thread) = (void *)__xn_reg_arg3(regs);
xnshadow_map(thread,
name,
curr->policy == SCHED_FIFO ? curr->rt_priority : FUSION_LOW_PRI,
syncpid,
u_syncp,
FUSION_SKIN_MAGIC,
NULL);
return 0;
}
static int __pthread_init_rt (struct task_struct *curr, struct pt_regs *regs) {
return __pthread_shadow_helper(curr,regs,0,NULL);
}
static int __pthread_create_rt (struct task_struct *curr, struct pt_regs *regs)
{
if (!__xn_access_ok(curr,VERIFY_WRITE,__xn_reg_arg5(regs),sizeof(int)))
return -EFAULT;
return __pthread_shadow_helper(curr,regs,__xn_reg_arg4(regs),(int *)__xn_reg_arg5(regs));
}
static int __pthread_start_rt (struct task_struct *curr, struct pt_regs *regs)
{
xnthread_t *thread;
int err = -ESRCH;
xnmutex_lock(&__imutex);
thread = __pthread_find_by_handle(curr,(void *)__xn_reg_arg1(regs));
if (!thread)
goto out;
if (!testbits(thread->status,XNSTARTED))
{
xnshadow_start(thread,0,NULL,NULL,0);
xnpod_schedule(&__imutex);
err = 0;
}
else
err = -EBUSY;
out:
xnmutex_unlock(&__imutex);
return err;
}
static int __pthread_time_rt (struct task_struct *curr, struct pt_regs *regs)
{
unsigned long long t;
if (!__xn_access_ok(curr,VERIFY_WRITE,(void *)__xn_reg_arg1(regs),sizeof(t)))
return -EFAULT;
t = xnarch_get_cpu_time();
__xn_copy_to_user(curr,(void *)__xn_reg_arg1(regs),&t,sizeof(t));
return 0;
}
static int __pthread_inquire_rt (struct task_struct *curr, struct pt_regs *regs)
{
xnthread_t *thread = xnshadow_thread(curr); /* Can't be NULL. */
xninquiry_t buf;
if (!__xn_access_ok(curr,VERIFY_WRITE,(void *)__xn_reg_arg1(regs),sizeof(buf)))
return -EFAULT;
strncpy(buf.name,xnthread_name(thread),sizeof(buf.name) - 1);
buf.name[sizeof(buf.name) - 1] = 0;
buf.prio = xnthread_current_priority(thread);
buf.status = xnthread_status_flags(thread);
buf.khandle = thread;
buf.uhandle = xnthread_extended_info(thread);
__xn_copy_to_user(curr,(void *)__xn_reg_arg1(regs),&buf,sizeof(buf));
return 0;
}
static int __pthread_migrate_rt (struct task_struct *curr, struct pt_regs *regs)
{
xnthread_t *thread = xnshadow_thread(curr); /* Can't be NULL. */
switch (__xn_reg_arg1(regs))
{
case FUSION_RTAI_DOMAIN:
if (testbits(thread->status,XNRELAX))
xnshadow_harden(NULL);
setbits(thread->status,XNAUTOSW);
break;
case FUSION_LINUX_DOMAIN:
if (!testbits(thread->status,XNRELAX))
xnshadow_relax();
clrbits(thread->status,XNAUTOSW);
break;
default:
return -EINVAL;
}
return 0;
}
static int __pthread_hold_vm (struct task_struct *curr, struct pt_regs *regs)
{
if (!__xn_access_ok(curr,VERIFY_WRITE,(void *)__xn_reg_arg1(regs),sizeof(int)))
return -EFAULT;
xnmutex_lock(&__imutex);
__xn_put_user(curr,1,(int *)__xn_reg_arg1(regs)); /* Raise the pend flag */
xnsynch_sleep_on(&__fusion_barrier,XN_INFINITE,&__imutex);
xnmutex_unlock(&__imutex);
return 0;
}
static int __pthread_release_vm (struct task_struct *curr, struct pt_regs *regs)
{
if (!__xn_access_ok(curr,VERIFY_WRITE,(void *)__xn_reg_arg1(regs),sizeof(int)))
return -EFAULT;
xnmutex_lock(&__imutex);
__xn_put_user(curr,0,(int *)__xn_reg_arg1(regs)); /* Clear the lock flag */
if (xnsynch_flush(&__fusion_barrier,XNBREAK) == XNSYNCH_RESCHED)
xnpod_schedule(&__imutex);
xnmutex_unlock(&__imutex);
return 0;
}
static int __pthread_idle_vm (struct task_struct *curr, struct pt_regs *regs)
{
xnthread_t *thread = xnpod_current_thread();
if (!__xn_access_ok(curr,VERIFY_WRITE,(void *)__xn_reg_arg1(regs),sizeof(int)))
return -EFAULT;
xnmutex_lock(&__imutex);
__xn_put_user(curr,0,(int *)__xn_reg_arg1(regs)); /* Clear the lock flag */
xnpod_renice_thread(thread,xnthread_initial_priority(thread));
if (xnsynch_nsleepers(&__fusion_barrier) > 0)
xnsynch_flush(&__fusion_barrier,XNBREAK);
xnpod_suspend_thread(thread,XNSUSP,XN_INFINITE,NULL,&__imutex);
xnmutex_unlock(&__imutex);
return 0;
}
static int __pthread_activate_vm (struct task_struct *curr, struct pt_regs *regs)
{
xnthread_t *prev, *next;
int err = -ESRCH;
xnmutex_lock(&__imutex);
next = __pthread_find_by_handle(curr,(void *)__xn_reg_arg1(regs));
if (!next)
goto out;
prev = __pthread_find_by_handle(curr,(void *)__xn_reg_arg2(regs));
if (!prev)
goto out;
xnpod_renice_thread(next,xnthread_initial_priority(next) + 1);
if (!testbits(next->status,XNSTARTED))
xnshadow_start(next,0,NULL,NULL,0);
else if (testbits(next->status,XNSUSP))
xnpod_resume_thread(next,XNSUSP);
xnpod_renice_thread(prev,xnthread_initial_priority(prev));
if (!testbits(prev->status,XNSUSP))
xnpod_suspend_thread(prev,XNSUSP,XN_INFINITE,NULL,&__imutex);
xnpod_schedule(&__imutex);
err = 0;
out:
xnmutex_unlock(&__imutex);
return err;
}
static int __pthread_cancel_vm (struct task_struct *curr, struct pt_regs *regs)
{
xnthread_t *dead, *next;
int err = -ESRCH;
xnmutex_lock(&__imutex);
if (__xn_reg_arg1(regs))
{
dead = __pthread_find_by_handle(curr,(void *)__xn_reg_arg1(regs));
if (!dead)
goto out;
}
else
dead = xnshadow_thread(curr);
if (__xn_reg_arg2(regs))
{
next = __pthread_find_by_handle(curr,(void *)__xn_reg_arg2(regs));
if (!next)
goto out;
xnpod_renice_thread(next,xnthread_initial_priority(next) + 1);
if (testbits(next->status,XNSTARTED))
xnshadow_start(next,0,NULL,NULL,0);
else if (testbits(next->status,XNSUSP))
xnpod_resume_thread(next,XNSUSP);
}
err = 0;
xnpod_delete_thread(dead,&__imutex);
out:
xnmutex_unlock(&__imutex);
return err;
}
static void fusion_shadow_delete_hook (xnthread_t *thread)
{
if (xnthread_magic(thread) == FUSION_SKIN_MAGIC)
{
xnshadow_unmap(thread);
xnfree(thread);
}
}
/* User-space skin services -- The declaration order must be in sync
with the opcodes defined in xenomai/fusion.h. Services marked by
the __xn_flag_suspensive bit must be propagated to the caller's
domain. */
static xnsysent_t fusion_systab[] = {
{ &__pthread_init_rt, __xn_flag_suspensive|__xn_flag_anycontext }, /* 0: __xn_fusion_init */
{ &__pthread_create_rt, __xn_flag_suspensive|__xn_flag_anycontext }, /* 1: __xn_fusion_create */
{ &__pthread_start_rt, __xn_flag_suspensive }, /* 2: __xn_fusion_start */
{ &__pthread_migrate_rt, __xn_flag_suspensive }, /* 3: __xn_fusion_migrate */
{ &__pthread_time_rt, __xn_flag_anycontext }, /* 4: __xn_fusion_time */
{ &__pthread_inquire_rt, __xn_flag_anycontext }, /* 5: __xn_fusion_inquire */
{ &__pthread_idle_vm, __xn_flag_suspensive }, /* 6: __xn_fusion_idle */
{ &__pthread_cancel_vm, __xn_flag_suspensive }, /* 7: __xn_fusion_cancel */
{ &__pthread_activate_vm, __xn_flag_suspensive }, /* 8: __xn_fusion_activate */
{ &__pthread_hold_vm, __xn_flag_suspensive }, /* 9: __xn_fusion_hold */
{ &__pthread_release_vm, __xn_flag_suspensive }, /* 10: __xn_fusion_release */
};
int fusion_register_skin (void)
{
__fusion_muxid = xnshadow_register_skin(FUSION_SKIN_MAGIC,
sizeof(fusion_systab) / sizeof(fusion_systab[0]),
fusion_systab);
if (__fusion_muxid < 0)
return __fusion_muxid;
xnpod_add_hook(XNHOOK_THREAD_DELETE,&fusion_shadow_delete_hook);
xnsynch_init(&__fusion_barrier,XNSYNCH_FIFO);
return XN_OK;
}
void fusion_unregister_skin (void)
{
xnmutex_lock(&__imutex);
if (xnsynch_destroy(&__fusion_barrier) == XNSYNCH_RESCHED)
xnpod_schedule(&__imutex);
xnmutex_unlock(&__imutex);
xnpod_remove_hook(XNHOOK_THREAD_DELETE,&fusion_shadow_delete_hook);
xnshadow_unregister_skin(__fusion_muxid);
}
|