Files @ 052c0cbc4bbf
Branch filter:

Location: vmkdrivers/vmkdrivers/src_9/vmklinux_9/vmware/linux_task.c

unknown
ESXi-5.0-U3
  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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/* ****************************************************************
 * Portions Copyright 1998, 2007-2010 VMware, Inc.
 *
 * This program 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.
 * ****************************************************************/

#include <asm/semaphore.h>
#include <linux/jiffies.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/slab.h>

#include "vmkapi.h"
#include "linux_stubs.h"
#include "linux_task.h"
#include "vmklinux_dist.h"

#define VMKLNX_LOG_HANDLE LinTask
#include "vmklinux_log.h"

/*
 * Struct used to pass essential arguments to the 
 * startup function of a new task
 */
typedef struct LinuxTaskStartFuncArg {
   int                (*func)(void *);
   void               *arg;
   vmk_ModuleID       modID;
   vmk_HeapID         heapID;
   u32                flags;
} LinuxTaskStartFuncArg;

vmk_SpinlockIRQ               taskLock;
static vmk_WorldStorageHandle taskStorageHandle;


/*
 *----------------------------------------------------------------------
 *
 * LinuxTaskConstruct --
 *      Constructor used by vmk_WorldStorageCreate() to
 *      initialize the per workd LinuxTaskExt struct.
 *
 * Results:
 *      Always return VMK_OK.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------
 */

static VMK_ReturnStatus
LinuxTaskConstruct(vmk_WorldID wid,
                   void *object,
                   vmk_ByteCountSmall size,
                   vmk_AddrCookie arg)
{
   LinuxTaskExt *te = object;

   VMK_ASSERT(size == sizeof(LinuxTaskExt));

   memset(te, 0, size);

   te->task.pid   = (pid_t) wid;
   te->task.state = TASK_RUNNING;
   te->task.cpu   = VMKLNX_INVALID_TASK_CPU;

   VMKLNX_DEBUG(1, "te=%p (pid=%d)", te, te->task.pid);

   return VMK_OK;
}

static void
LinuxTaskDestruct(vmk_WorldID wid,
                   void *object,
                   vmk_ByteCountSmall size,
                   vmk_AddrCookie arg)
{
   LinuxTaskExt       *te = object;
   struct task_struct *tp = &te->task;

   VMK_ASSERT(size == sizeof(LinuxTaskExt));

   VMKLNX_DEBUG(1, "te=%p (pid=%d)", te, te->task.pid);

   if (tp->umem) {
      kfree(tp->umem);
   }
}


/*
 *----------------------------------------------------------------------
 *
 * LinuxTaskGetCurrent --
 *
 *      Get the extended task_struct pointer associated with the 
 *      current running world.
 *
 * Results:
 *      Return the extended task_struct of the current world.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

static inline LinuxTaskExt *
LinuxTaskGetCurrent(void)
{
   LinuxTaskExt *te;
   VMK_ReturnStatus status;

   status = vmk_WorldStorageLookUpLocal(taskStorageHandle, (void **)&te);
   VMK_ASSERT(status == VMK_OK);

   return te;
}


#if defined(VMX86_DEBUG)
/*
 *----------------------------------------------------------------------
 *
 * LinuxTask_ValidTask --
 *      Verify if the pointer te is indeed pointing to a valid
 *      extended task descriptor. 
 *
 * Results:
 *      TRUE if te is a valid pointer; otherwise FALSE.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

vmk_Bool
LinuxTask_ValidTask(LinuxTaskExt *te)
{
   LinuxTaskExt *taskFound;
   vmk_Bool ret;

   taskFound = LinuxTask_Find(te->task.pid);
   if (taskFound == NULL) {
      return VMK_FALSE;
   }

   ret = (taskFound == te) ? VMK_TRUE : VMK_FALSE;

   LinuxTask_Release(taskFound);

   return ret;
}


/*
 *----------------------------------------------------------------------
 *
 * LinuxTask_IsLocked --
 *      Verify that the current world has exclusive access to the
 *      specified extended task struct.
 *
 * Results:
 *      TRUE if the current world has exclusive access; otherwise
 *      FALSE;
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

void
LinuxTask_AssertLocked(LinuxTaskExt *te)
{
   vmk_SPAssertIsLockedIRQ(&taskLock);
}
#endif /* defined(VMX86_DEBUG) */


/*
 *----------------------------------------------------------------------
 *
 * vmklnx_GetCurrent --
 *
 *      Gets current task pointer for the current world. 
 *
 * Results:
 *      task struct of current world.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

struct task_struct *
vmklnx_GetCurrent(void)
{
   LinuxTaskExt *te;

   te = LinuxTaskGetCurrent();
   return &te->task;;
}
EXPORT_SYMBOL(vmklnx_GetCurrent);


/*
 *----------------------------------------------------------------------
 *
 * LinuxTask_WaitEvent --
 *      Block the current world until the event identified by eventID
 *      is delivered to the world.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

void 
LinuxTask_WaitEvent(u32 eventID)
{
   LinuxTaskExt *te = LinuxTaskGetCurrent();
   struct task_struct *task = &te->task;
   unsigned long prevIRQL;

   prevIRQL = LinuxTask_Lock(te);

   VMKLNX_DEBUG(1, "eventID=%d task=%p te=%p te->events=%d",
                 eventID, task, te, te->events);

   while ((te->events & eventID) == 0) {
      te->flags |= LT_SUSPENDED;
   
      VMKLNX_DEBUG(1, "task=%p (te=%p) suspending...", task, te);

      vmk_WorldWaitIRQLegacy((vmk_WorldEventID)te, &taskLock, 0, prevIRQL);
   
      VMKLNX_DEBUG(1, "task=%p (te=%p) resumed.", task, te);

      prevIRQL = LinuxTask_Lock(te);
   
      te->flags &= ~LT_SUSPENDED;

      if (te->events & eventID) {
         break;
      }

      VMKLNX_DEBUG(1, "Spurious wakeup: "
                       "pid=%d te->events=0x%x eventID=0x%x .", 
                       task->pid, te->events, eventID);
   }

   te->events &= ~eventID;
   task->state = TASK_RUNNING;
   LinuxTask_Unlock(te, prevIRQL);
}


/*
 *----------------------------------------------------------------------
 *
 * LinuxTask_SendEvent --
 *      Delivered the event identified by eventID to the world identified
 *      the task descriptor te. If world te is suspended, it will be 
 *      resumed.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

void
LinuxTask_SendEvent(LinuxTaskExt *te, u32 eventID)
{
   unsigned long prevIRQL;

   VMKLNX_DEBUG(1, "eventID=%d task=%p te=%p te->events=%d",
                 eventID, &te->task, te, te->events);

   prevIRQL = LinuxTask_Lock(te);

   te->events |= eventID;

   vmk_WorldWakeup((vmk_WorldEventID)te);

   LinuxTask_Unlock(te, prevIRQL);
}


/*
 *----------------------------------------------------------------------
 *
 * LinuxTask_Exit --
 *      Terminate the current world, removing the descriptor from the
 *      world private data area (which will free it via the destructor).
 *
 * Results:
 *      None.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

void
LinuxTask_Exit(LinuxTaskExt *te)
{
   VMKLNX_DEBUG(1, "Task te=%p (pid=%d) func=%p exited. Return value=%d",
                te, te->task.pid, te->func, te->retval);

   /*
    * If this is a kthread, we need to call LinuxTask_Release()
    * to reduce a reference to 'te' that was created by
    * kthread_create().
    */
   if (te->flags & LT_KTHREAD) {
      LinuxTask_Release(te);
   }
   vmk_WorldExit(VMK_OK);
}


/*
 *----------------------------------------------------------------------
 *
 * LinuxTask_Find --
 *      Get the extended task struct associated to the world whose 
 *      process ID is pid.
 *
 * Results:
 *      Return the extended task struct of the world.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

LinuxTaskExt *
LinuxTask_Find(pid_t pid)
{
   LinuxTaskExt *taskFound = NULL;
   VMK_ReturnStatus status;

   status = vmk_WorldStorageLookUp((vmk_WorldID) pid, 
                                   taskStorageHandle, 
				   (void **)&taskFound);
   if (status == VMK_OK) {
      return taskFound;
   }

   return NULL;
}


/*
 *----------------------------------------------------------------------
 *
 * LinuxStartFunc --
 *      This function starts up a vmklinux task. It sets up the
 *      runtime for the task and release the task resources on exit.
 *      data is the LinuxTaskStartFuncArg which provides the essential
 *      arguments to start up the new task.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

static void
LinuxStartFunc(void *data)
{
   LinuxTaskStartFuncArg *arg = data;
   LinuxTaskExt          *te  = LinuxTaskGetCurrent();

   VMK_ASSERT_CPU_HAS_INTS_ENABLED();

   te->func       = arg->func;
   te->arg        = arg->arg;
   te->modID      = arg->modID;
   te->flags      = arg->flags;
   te->task.state = TASK_RUNNING;

   VMKLNX_DEBUG(1, "task=%p te=%p flags=%d func=%p arg=%p", 
                 &te->task, te, te->flags, te->func, te->arg);

   vmk_HeapFree(arg->heapID, arg);

   if (te->modID != vmklinuxModID) {
      VMKAPI_MODULE_CALL(te->modID, te->retval, te->func, te->arg);
   } else {
      te->retval = te->func(te->arg);
   }

   VMKLNX_DEBUG(1, "Task func=%p exited.", te->func);
 
   LinuxTask_Exit(te);
}


/*
 *----------------------------------------------------------------------
 *
 * LinuxTask_Create --
 *      Create a vmklinux world to execute function fn, passing arg as
 *      argument to fn. fn is executed in the context of the module
 *      specified by module_id.
 *
 * Results:
 *      VMK_OK if success.
 *
 * Side effects:
 *	Return the world ID via the output argument pid.
 *
 *----------------------------------------------------------------------
 */

VMK_ReturnStatus
LinuxTask_Create(int (*fn)(void *),
                 void * arg,
                 vmk_ModuleID module_id,
                 u32 flags,
                 char *fn_name,
		 pid_t *pid)
{
   vmk_ByteCount         byte_left = TASK_NAME_LENGTH;
   VMK_ReturnStatus      status;
   vmk_WorldID           worldID;
   vmk_HeapID            heapID;
   char                  name[TASK_NAME_LENGTH];
   vmk_ByteCount         module_name_len;
   LinuxTaskStartFuncArg *start_func_arg;

   /*
    * Create task name as "module:fn_name"
    */
   vmk_ModuleGetName(module_id, name, TASK_NAME_LENGTH);
   module_name_len = vmk_Strnlen(name, TASK_NAME_LENGTH);
   byte_left -= module_name_len + 1;
   vmk_Snprintf(name + module_name_len, byte_left, ":%s", fn_name);

   VMKLNX_DEBUG(1, "name=%s fn=%p arg=%p module_id=%d",
                name, fn, arg, module_id);

   heapID = vmk_ModuleGetHeapID(module_id);
   VMK_ASSERT(heapID != VMK_INVALID_HEAP_ID);

   start_func_arg = vmk_HeapAlloc(heapID, sizeof(*start_func_arg));
   if (start_func_arg == NULL) {
      status = VMK_NO_MEMORY;
      goto done;
   }

   start_func_arg->func   = fn;
   start_func_arg->arg    = arg;
   start_func_arg->modID  = module_id;
   start_func_arg->heapID = heapID;
   start_func_arg->flags  = flags;

   status = vmk_WorldCreate(vmklinuxModID,
                            name,
                            (vmk_WorldStartFunc)LinuxStartFunc,
                            start_func_arg,
                            &worldID);
   if (status == VMK_OK) {
      *pid = worldID;
   } else {
      vmk_HeapFree(heapID, start_func_arg);
   }

done:
   // XXX worldIDs are not valid in the same range as linux PIDs.
   // Probably okay in the kernel, though.
   return status;
}


/*
 *----------------------------------------------------------------------
 *
 * vmklnx_kernel_thread --
 *      Create a vmklinux world to execute function fn, passing arg as
 *      argument to fn. fn is executed in the context of the module
 *      specified by module_id. The new task is named fn_name.
 *
 * Results:
 *      The world ID of the new world on success; otherwise -ECHILD.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

int 
vmklnx_kernel_thread(vmk_ModuleID module_id,
                     int (*fn)(void *),
                     void * arg,
		     char *fn_name)
{
   VMK_ReturnStatus status;
   pid_t            pid;

   status = LinuxTask_Create(fn, 
                             arg, 
			     module_id, 
			     LT_KERNEL_THREAD, 
			     fn_name, 
			     &pid);
   if (status != VMK_OK) {
      return -ECHILD;
   }

   return pid;
}
EXPORT_SYMBOL(vmklnx_kernel_thread);


/**                                          
 *  wake_up_process - wake up a given task 
 *  @task: a given task to be resumed 
 *                                           
 *  Wake up and resume a task.
 *                                          
 *  RETURN VALUE:
 *  Always return 1.
 */                                          
/* _VMKLNX_CODECHECK_: wake_up_process */
fastcall
int wake_up_process(struct task_struct *task) 
{
   VMK_ASSERT(task && task->pid);

   LinuxTaskExt *te = container_of(task, LinuxTaskExt, task);

   LinuxTask_Resume(te);
   return 1;
}
EXPORT_SYMBOL(wake_up_process);

/**                                          
 *  schedule - select a new process to be executed 
 *
 *  Selects a new process to be executed
 *
 *  RETURN VALUE:
 *  None
 */
/* _VMKLNX_CODECHECK_: schedule */
void 
schedule(void)
{
   struct task_struct *task = get_current();

   if (unlikely((signal_pending(task) 
       && (task->state == TASK_INTERRUPTIBLE 
           || task->state == TASK_RUNNING)))) {
      return;
   }

   if (unlikely(task->state == TASK_RUNNING)) {
      yield();
      return;
   }

   vmk_WorldAssertIsSafeToBlock();
   LinuxTask_Suspend();
}
EXPORT_SYMBOL(schedule);

static void
__wait_timeout(unsigned long data)
{
   struct task_struct *task = (struct task_struct *) data;

   VMK_ASSERT(task && task->pid);

   LinuxTaskExt *te = container_of(task, LinuxTaskExt, task);

   LinuxTask_Resume(te);
}

static signed long
__schedule_timeout(struct task_struct *current_task, signed long wait_time)
{
   unsigned long timeout = wait_time;
   signed long diff;
   long state;
   struct timer_list wait_timer;
   int ret;

   if (unlikely(wait_time == MAX_SCHEDULE_TIMEOUT)) {
      schedule();
      return wait_time;
   }

   if (unlikely(wait_time < 0)) {
      VMKLNX_DEBUG(1, "negative timeout value %lx from %p",
                     wait_time, __builtin_return_address(0));
      return 0;
   }

   timeout = wait_time + jiffies;
   state = current_task->state;

   setup_timer(&wait_timer, __wait_timeout, (unsigned long) current_task);

   /*
    * We might need to set a timer ***twice*** because jiffies may not have
    * advanced fully for the first timer.  That's because jiffies is
    * maintained on CPU 0, whereas the local CPU clock may not be fully
    * synchronized to it.
    *
    * See PR 330905.
    */
   for (;;) {
      __mod_timer(&wait_timer, timeout);
      schedule();
      ret = del_timer_sync(&wait_timer);
      diff = timeout - jiffies;
      if (likely(diff <= 0 || ret)) {
         break;
      }
      // set the state back in preparation for blocking again
      current_task->state = state;
   }

   return diff < 0 ? 0 : diff;
}

/**
 *  schedule_timeout - sleep until timeout
 *  @wait_time: timeout value in jiffies
 *
 *  Make the current task sleep until specified number of jiffies have elapsed.
 *  The routine may return early if a signal is delivered to the current task.
 *  Specifying a timeout value of MAX_SCHEDULE_TIMEOUT will schedule
 *  the CPU away without a bound on the timeout.
 *
 *  RETURN VALUE:
 *  0 if the timer expired in time,
 *  remaining time in jiffies if function returned early, or
 *  MAX_SCHEDULE_TIMEOUT if timeout value of MAX_SCHEDULE_TIMEOUT was specified
 */
/* _VMKLNX_CODECHECK_: schedule_timeout */
fastcall signed long
schedule_timeout(signed long wait_time)
{
   return __schedule_timeout(get_current(), wait_time);
}
EXPORT_SYMBOL(schedule_timeout);

/**
 *  schedule_timeout_interruptible - sleep until timeout or event
 *  @wait_time: the timeout in jiffies
 *
 *  Make the current task sleep until @wait_time jiffies have elapsed or if a
 *  signal is delivered to the current task.
 *
 *  RETURN VALUE:
 *  Returns the remaining time in jiffies.
 *
 */
/* _VMKLNX_CODECHECK_: schedule_timeout_interruptible */
signed long 
schedule_timeout_interruptible(signed long wait_time)
{
   struct task_struct *current_task = get_current();

   current_task->state = TASK_INTERRUPTIBLE;
   return __schedule_timeout(current_task, wait_time);
}
EXPORT_SYMBOL(schedule_timeout_interruptible);

/**                                          
 *  schedule_timeout_uninterruptible - sleep until at least specified timeout
 *                                     has elapsed
 *  @wait_time: timeout value in jiffies
 *                                           
 *  Make the current task sleep until at least specified number of jiffies 
 *  have elapsed. Specifying a timeout value of MAX_SCHEDULE_TIMEOUT will 
 *  schedule the CPU away without a bound on the timeout.
 *  
 *  RETURN VALUE:
 *  0 or MAX_SCHEDULE_TIMEOUT if timeout value of MAX_SCHEDULE_TIMEOUT 
 *  was specified
 */                                          
/* _VMKLNX_CODECHECK_: schedule_timeout_uninterruptible */
signed long 
schedule_timeout_uninterruptible(signed long wait_time)
{
   struct task_struct *current_task = get_current();

   current_task->state = TASK_UNINTERRUPTIBLE;
   return __schedule_timeout(current_task, wait_time);
}
EXPORT_SYMBOL(schedule_timeout_uninterruptible);

/**                                          
 *  cond_resched - latency reduction via explicit rescheduling
 *                                           
 *  Performs 'possible' rescheduling of this task by invoking builtin 
 *  throttling yield mechanism. The task may or may not yield the CPU 
 *  depending on the decision. 
 *  
 *  RETURN VALUE:
 *  0
 *
 *  ESX DEVIATION NOTES: Always returns 0, unlike Linux where the
 *  return value indicates whether a reschedule was done in fact.
 */                                          
/* _VMKLNX_CODECHECK_: cond_resched */
int
cond_resched(void)
{
   /*
    * Let vmk_WorldYield builtin throttling yield mechanism to
    * decide whether this world should yield the CPU.
    */
   vmk_WorldYield();

   /*
    * Always assume we have not yielded.
    */
   return 0;
}
EXPORT_SYMBOL(cond_resched);

/**                                          
 *  yield - yield the CPU       
 *                                           
 *  Yield the CPU to other tasks.                       
 *                                           
 *  ESX Deviation Notes:                     
 *  The calling task will be descheduled for at least 1 millisecond. 
 *
 *  RETURN VALUE:
 *  This function does not return a value.
 *                                           
 */                                          
/* _VMKLNX_CODECHECK_: yield */
void 
yield(void)
{
   /*
    * vmk_WorldYield doesn't always yield currently since the yield
    * is throttled. In order to force the caller to block we can call
    * vmk_WorldSleep() but if the delay is too short, the call may
    * return without yielding. So sleep for at least a 1 millisecond
    * to ensure that CPU actually is given up.
    */
   vmk_WorldSleep(1000);
}
EXPORT_SYMBOL(yield);

/**                                          
 *  msleep - Deschedules the current task for a duration
 *  @msecs: time, in milliseconds, that the current task needs to be descheduled
 *                                           
 *  Deschedules the current task for a duration
 *
 *  See Also:
 *  msleep_interruptible
 */                                          
/* _VMKLNX_CODECHECK_: msleep */
void
msleep(unsigned int msecs)
{
   signed long sleep_time = msecs_to_jiffies(msecs) + 1;

   while (sleep_time) {
      sleep_time = schedule_timeout_uninterruptible(sleep_time);
   }
}
EXPORT_SYMBOL(msleep);

/**                                          
 *  msleep_interruptible - deschedules the current task for a duration
 *  @msecs: time, in milliseconds, that the current task needs to be descheduled.
 *                                           
 *  Deschedules the current task for a duration. The function returns when
 *  the task has been descheduled for the whole intended duration,
 *  or returns when the task is interrupted by an external event
 *  while it is descheduled. In this case, the amount of time unslept is
 *  returned.
 *
 *  Return Value:
 *  Amount of time unslept
 *
 *  See Also:
 *  msleep
 *                                           
 */                                          
/* _VMKLNX_CODECHECK_: msleep_interruptible */
unsigned long
msleep_interruptible(unsigned int msecs)
{
   signed long sleep_time = msecs_to_jiffies(msecs) + 1;
   struct task_struct *current_task = get_current();

   while(sleep_time && !signal_pending(current_task)) {
      current_task->state = TASK_INTERRUPTIBLE;
      sleep_time = __schedule_timeout(current_task, sleep_time);
   }

   return jiffies_to_msecs(sleep_time);
}
EXPORT_SYMBOL(msleep_interruptible);


/*
 *----------------------------------------------------------------------------
 *
 * LinuxTask(Init|Cleanup)Tasks --
 *
 *      Set up the world storage property for the 
 *      memory allocation of the per world extended
 *      task struct.
 *
 * Results:
 *      Init: VMK_OK on success.
 *
 * Side effects:
 *      Will vmk_Panic() on failure.
 *
 *----------------------------------------------------------------------------
 */

VMK_ReturnStatus
LinuxTaskInitTasks(void)
{
   VMK_ReturnStatus status = VMK_OK;
   vmk_WorldStorageProps props;

   props.type       = VMK_WORLD_STORAGE_TYPE_SIMPLE;
   props.moduleID   = vmklinuxModID;
   props.size       = sizeof(LinuxTaskExt);
   props.align      = sizeof(long);
   props.constructor= LinuxTaskConstruct;
   props.destructor = LinuxTaskDestruct;

   status = vmk_WorldStorageCreate(&props, &taskStorageHandle);
   if (status != VMK_OK) {
      VMKLNX_WARN("Failed to create world storage (%s)", vmk_StatusToString(status));
      VMK_ASSERT(0);
   }

   return status;
}

void
LinuxTaskCleanupTasks(void)
{
   VMK_ReturnStatus status;

   status = vmk_WorldStorageDestroy(taskStorageHandle);
   VMK_ASSERT(status == VMK_OK);
}


/*
 *----------------------------------------------------------------------------
 *
 * LinuxTask_(Init|Cleanup) --
 *
 *      Initialization and cleanup of LinuxTask.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

void
LinuxTask_Init(void)
{
   VMK_ReturnStatus status;

   VMKLNX_CREATE_LOG();

   status = vmk_SPCreateIRQ_LEGACY(&taskLock, vmklinuxModID,
                            "taskLock", NULL, VMK_SP_RANK_IRQ_BLOCK_LEGACY);
   VMK_ASSERT(status == VMK_OK);

   status = LinuxTaskInitTasks();
   VMK_ASSERT(status == VMK_OK);
}

void
LinuxTask_Cleanup(void)
{
   LinuxTaskCleanupTasks();
   vmk_SPDestroyIRQ(&taskLock);
   VMKLNX_DESTROY_LOG();
}