ReactOS  0.4.13-dev-551-gf37fb1f
ctxswitch.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS kernel
4  * FILE: ntoskrnl/ke/powerpc/ctxswitch.c
5  * PURPOSE: Thread Context Switching
6  *
7  * PROGRAMMERS: arty
8  (i386 implementation by Alex Ionescu)
9  */
10 
11 /* INCLUDES ******************************************************************/
12 
13 #include <ntoskrnl.h>
14 #define NDEBUG
15 #include <debug.h>
16 #include <ppcmmu/mmu.h>
17 
18 /*++
19  * KiThreadStartup
20  *
21  * The KiThreadStartup routine is the beginning of any thread.
22  *
23  * Params:
24  * SystemRoutine - Pointer to the System Startup Routine. Either
25  * PspUserThreadStartup or PspSystemThreadStartup
26  *
27  * StartRoutine - For Kernel Threads only, specifies the starting execution
28  * point of the new thread.
29  *
30  * StartContext - For Kernel Threads only, specifies a pointer to variable
31  * context data to be sent to the StartRoutine above.
32  *
33  * UserThread - Indicates whether or not this is a user thread. This tells
34  * us if the thread has a context or not.
35  *
36  * TrapFrame - Pointer to the KTHREAD to which the caller wishes to
37  * switch from.
38  *
39  * Returns:
40  * Should never return for a system thread. Returns through the System Call
41  * Exit Dispatcher for a user thread.
42  *
43  * Remarks:
44  * If a return from a system thread is detected, a bug check will occur.
45  *
46  *--*/
47 
48 VOID
49 NTAPI
52  PVOID StartContext,
53  BOOLEAN UserThread,
54  KTRAP_FRAME TrapFrame)
55 {
57  __asm__("mr 0,%0\n\t"
58  "mr 3,%1\n\t"
59  "mr 4,%2\n\t"
60  "mr 5,%3\n\t"
61  "mr 6,%4\n\t"
62  "sc" : :
63  "r" (0xf0000), /* Thread start function */
64  "r" (SystemRoutine),
65  "r" (StartRoutine),
66  "r" (StartContext),
67  "r" (UserThread));
69 }
70 
71 /* Take a decrementer trap, and prepare the given trap frame, swapping
72  * process and thread context as appropriate. */
74 
75 VOID
78  IN PKPRCB Prcb);
79 
82 
83 VOID
84 NTAPI
86 {
87  KIRQL Irql;
88  PKPRCB Prcb = KeGetPcr()->Prcb;
89  if (!KiLastThread)
91 
92  if (KiLastThread->State == Running)
94 
96  KiLastThreadTrapFrame = Prcb->IdleThread->TrapFrame;
97 
98  TrapFrame->OldIrql = KeGetCurrentIrql();
99  *KiLastThreadTrapFrame = *TrapFrame;
100 
101  if (Prcb->NextThread)
102  {
103  Prcb->CurrentThread = Prcb->NextThread;
104  Prcb->NextThread = NULL;
105  }
106  else
107  Prcb->CurrentThread = Prcb->IdleThread;
108 
109  Prcb->CurrentThread->State = Running;
110 
111  KiLastThreadTrapFrame = Prcb->CurrentThread->TrapFrame;
112  KiLastThread = Prcb->CurrentThread;
113 
114  *TrapFrame = *KiLastThreadTrapFrame;
116 
117  if (Irql > TrapFrame->OldIrql)
118  KfRaiseIrql(Irql);
119  else if (Irql < TrapFrame->OldIrql)
120  KfLowerIrql(Irql);
121 
122  /* When we return, we'll go through rfi and be in new thread land */
123  __asm__("mtdec %0" : : "r" (0x1000000)); // Reset the trap
124 }
#define KeGetCurrentIrql()
Definition: env_spec_w32.h:706
PKTRAP_FRAME KiLastThreadTrapFrame
Definition: ctxswitch.c:81
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
_In_ ULONG _In_opt_ POBJECT_ATTRIBUTES _In_opt_ HANDLE _Out_opt_ PCLIENT_ID _In_ PKSTART_ROUTINE StartRoutine
Definition: psfuncs.h:87
#define STATUS_THREAD_IS_TERMINATING
Definition: ntstatus.h:297
VOID NTAPI KiThreadStartup(PKSYSTEM_ROUTINE SystemRoutine, PKSTART_ROUTINE StartRoutine, PVOID StartContext, BOOLEAN UserThread, KTRAP_FRAME TrapFrame)
Definition: ctxswitch.c:50
#define FASTCALL
Definition: nt_native.h:50
_Out_ PKIRQL Irql
Definition: csq.h:179
struct _KTHREAD * NextThread
Definition: ketypes.h:567
#define KeGetPcr()
Definition: ke.h:25
VOID FASTCALL KiQueueReadyThread(IN PKTHREAD Thread, IN PKPRCB Prcb)
Definition: thrdschd.c:70
UCHAR KIRQL
Definition: env_spec_w32.h:591
VOID(NTAPI * PKSYSTEM_ROUTINE)(PKSTART_ROUTINE StartRoutine, PVOID StartContext)
Definition: ketypes.h:625
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
KSTART_ROUTINE * PKSTART_ROUTINE
Definition: ketypes.h:487
__asm__("\t.globl GetPhys\n" "GetPhys:\t\n" "mflr 0\n\t" "stwu 0,-16(1)\n\t" "mfmsr 5\n\t" "andi. 6,5,0xffef\n\t" "mtmsr 6\n\t" "isync\n\t" "sync\n\t" "lwz 3,0(3)\n\t" "mtmsr 5\n\t" "isync\n\t" "sync\n\t" "lwz 0,0(1)\n\t" "addi 1,1,16\n\t" "mtlr 0\n\t" "blr")
struct _KTHREAD * CurrentThread
Definition: ketypes.h:566
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
PKTHREAD KiLastThread
Definition: ctxswitch.c:80
KIRQL FASTCALL KfRaiseIrql(IN KIRQL NewIrql)
Definition: pic.c:187
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:803
struct _KTHREAD * IdleThread
Definition: ketypes.h:568
VOID FASTCALL KfLowerIrql(IN KIRQL NewIrql)
Definition: pic.c:232
UCHAR OldIrql
Definition: ketypes.h:96
volatile UCHAR State
Definition: ketypes.h:1679
NTSTATUS NTAPI PspTerminateThreadByPointer(IN PETHREAD Thread, IN NTSTATUS ExitStatus, IN BOOLEAN bSelf)
Definition: kill.c:987
#define KeGetCurrentThread
Definition: hal.h:44
VOID NTAPI KiDecrementerTrap(PKTRAP_FRAME TrapFrame)
Definition: ctxswitch.c:85
#define APC_LEVEL
Definition: env_spec_w32.h:695
VOID KiDecrementerTrapFinish(PKTRAP_FRAME TrapFrame)