📄 asm.cpp
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/Platform/Asm.cpp,v $
* $Date: 2003/05/13 18:42:13 $
*
Description:
Assemly language implementation of low level user-context threading
functions
\*____________________________________________________________________________*/
//$SourceTop:$
#include "OSConfig.h"
#if defined(CONFIG_MULTITHREADED) && defined(CONFIG_MT_USER)
/* ---
Make sure this architecure is supported
--- */
#if !defined(CONFIG_ASM_I386) && \
!defined(CONFIG_ASM_SPARC) && \
!defined(CONFIG_ASM_MIPS) && \
!defined(CONFIG_ASM_HP)
# error Unsupported Machine Architecture
#endif
#include "UserSync.h"
/* ---
This is global because it needs to be referenced after the
stack and base pointers have been changed and wiped out automatic variables
--- */
static void **__ppCurrentThread;
/*____________________________________________________________________________*\
*
Function: StackContextSwitch()
Synopsis:
Description:
This is a very specical function. It swaps out the stack and so does
a 'return' into another thread.
- Assumes the current thread has been initializated, wrt to runnable
state and reason it is yielding.
- Saves the current context for the thread and adds it the scheduler.
- Finds the next runnable thread and restores its context.
- If a new thread has been created then it is used as the
next thread to run and its BeginThread() (via __Pi_BeginThread() )
is run. When its BeginThread() returns it is not restored as a runnable
thread and effectively that thread of execution terminates.
- If a thread is exiting then
its stack is restored and an exception or setjmp() is done immediately
afterwards to unwind its stack. This is achieved by calling the function
__Pi_UnwindThreadStack.
At the stack base (__Pi_BeginThread()) this
exception is caught, the thread object cleaned up wrt outstanding locks,
and the thread marked terminated. The return from (__Pi_BeginThread()) back
to StackContextSwitch() causes the next thread to be scheduled.
\*____________________________________________________________________________*/
void __Pi_StackContextSwitch(
void **ppCurrentThread,
void *pNextThread,
int iAction )
{
#if defined(CONFIG_ASM_I386)
/* make it easy to spot in ASM */
size_t *__pContext = (size_t *)0x3737;
#elif defined(CONFIG_ASM_SPARC) || defined(CONFIG_ASM_MIPS) || defined(CONFIG_ASM_HP)
/* make it easy to spot in ASM */
register size_t *__pContext = (size_t *)0x3737;
#endif
size_t tFrameSize;
__ppCurrentThread = ppCurrentThread;
__pContext = __Pi_GetThreadContext( *__ppCurrentThread );
/* ---
Save some machine registers in this thread's context object.
--- */
#if defined(CONFIG_ASM_I386) /* Intel 386 series */
/* assume eax==__pContext */
# if defined(CONFIG_ASM_I386_WIN32)
__asm mov eax, (-4)[ebp]; // eax = __pContext;
__asm mov 0[eax], esp;
__asm mov 4[eax], ebp;
__asm mov 12[eax], ebx;
__asm mov 16[eax], ecx;
__asm mov 20[eax], edx;
__asm mov 24[eax], esi;
__asm mov 28[eax], edi;
# elif defined(CONFIG_ASM_I386_WIN16)
asm {
push ds
push si
push di
push ax
push cx
push es
push bx
push dx
les bx,dword ptr [bp-6]
mov word ptr es:[bx+0],sp
mov word ptr es:[bx+2],bp
}
# else
asm( "movl -4(%ebp), %eax" ); // eax = __pContext
asm( "movl %esp, 0(%eax)" );
asm( "movl %ebp, 4(%eax)" );
asm( "movl %ebx, 12(%eax)" );
asm( "movl %ecx, 16(%eax)" );
asm( "movl %edx, 20(%eax)" );
asm( "movl %esi, 24(%eax)" );
asm( "movl %edi, 28(%eax)" );
# endif
#elif defined(CONFIG_ASM_SPARC) /* Sparc chip */
/* assume %l0 == __pContext; */
asm( "st %sp, [%l0]" );
asm( "st %fp, [%l0+4]" );
asm( "st %o7, [%l0+8]" );
asm( "st %i7, [%l0+12]" );
#elif defined(CONFIG_ASM_MIPS) /* IRIX on MIPS */
/* assume $16 == __pContext; */
asm( "sw $sp, 0($16)" );
asm( "sw $fp, 4($16)" );
asm( "sw $gp, 8($16)" );
asm( "sw $17, 12($16)" );
asm( "sw $18, 16($16)" );
asm( "sw $19, 20($16)" );
asm( "sw $20, 24($16)" );
asm( "sw $21, 28($16)" );
asm( "sw $22, 32($16)" );
asm( "sw $23, 36($16)" );
asm( "sw $31, 40($16)" );
#endif
__Pi_AddLatentThread( *__ppCurrentThread );
/* --- new thread ? --- */
if ( iAction==SCHED_BEGIN )
{
/*
** A new thread has been primed, begin execution of that thread
*/
tFrameSize = (size_t) __pContext[1] - /* assumes the stack */
(size_t) __pContext[0]; /* grows down */
*__ppCurrentThread = pNextThread;
pNextThread = 0;
__pContext = __Pi_GetThreadContext( *__ppCurrentThread );
__pContext[1] = __pContext[0];
__pContext[0] = __pContext[1] - tFrameSize;
/* ---
Setup base of stack for new thread
--- */
# if defined(CONFIG_ASM_I386)
# if defined(CONFIG_ASM_I386_WIN32)
__asm mov eax, (-4)[ebp]; // eax = __pContext;
__asm mov esp, 0[eax];
__asm mov ebp, 4[eax];
# elif defined(CONFIG_ASM_I386_WIN16)
asm {
les bx,dword ptr [bp-6]
mov sp, es:[bx+0]
mov bp, es:[bx+2]
};
# else
asm( "movl -4(%ebp), %eax" ); // eax = __pContext
asm( "movl 0(%eax), %esp" );
asm( "movl 4(%eax), %ebp" );
# endif
# elif defined(CONFIG_ASM_SPARC)
asm( "mov 0, %o7" );
asm( "mov 0, %i7" );
asm( "ld [%l0+0], %sp" );
asm( "ld [%l0+4], %fp" );
# elif defined(CONFIG_ASM_MIPS)
asm( "lw $sp, 0($16)" );
asm( "lw $fp, 4($16)" );
asm( "sw $gp, 16($fp)" );
# endif
pNextThread = 0;
__Pi_BeginThread( *__ppCurrentThread );
}
else if ( iAction==SCHED_EXIT ) /* --- an exiting thread exists --- */
{
/*
** A thread has been scheduled for termination, swap in the thread
** by adjusting the current stack to its stack, but kill an
** exception or other stack unwinding contruct immediately, then
** schedule the next thread. Thus terminating this thread
*/
*__ppCurrentThread = pNextThread;
pNextThread = 0;
__pContext = __Pi_GetThreadContext( *__ppCurrentThread );
#if defined(CONFIG_ASM_I386)
# if defined(CONFIG_ASM_I386_WIN32)
__asm mov esp, 0[eax];
__asm mov ebp, 4[eax];
__asm mov ebx, 12[eax];
__asm mov ecx, 16[eax];
__asm mov edx, 20[eax];
__asm mov esi, 24[eax];
__asm mov edi, 28[eax];
__asm mov eax, 8[eax];
# elif defined(CONFIG_ASM_I386_WIN16)
asm {
les bx,dword ptr [bp-6]
mov sp, word ptr es:[bx+0]
mov bp, word ptr es:[bx+2]
pop dx
pop bx
pop es
pop cx
pop ax
pop di
pop si
pop ds
}
# else
asm( "movl -4(%ebp), %eax" ); // eax = __pContext
asm( "movl 0(%eax), %esp" );
asm( "movl 4(%eax), %ebp" );
asm( "movl 12(%eax), %ebx" );
asm( "movl 16(%eax), %ecx" );
asm( "movl 20(%eax), %edx" );
asm( "movl 24(%eax), %esi" );
asm( "movl 28(%eax), %edi" );
asm( "movl 8(%eax), %eax" );
# endif
#elif defined(CONFIG_ASM_SPARC)
asm( "ld [%l0+8], %o7" );
asm( "ld [%l0+12], %i7" );
asm( "ld [%l0+0], %sp" );
asm( "ld [%l0+4], %fp" );
#elif defined(CONFIG_ASM_MIPS)
asm( "lw $sp, 0($16)" );
asm( "lw $fp, 4($16)" );
asm( "lw $gp, 8($16)" );
asm( "lw $17, 12($16)" );
asm( "lw $18, 16($16)" );
asm( "lw $19, 20($16)" );
asm( "lw $20, 24($16)" );
asm( "lw $21, 28($16)" );
asm( "lw $22, 32($16)" );
asm( "lw $23, 36($16)" );
asm( "lw $31, 40($16)" );
#endif
__Pi_UnwindThreadStack( *__ppCurrentThread );
}
/* --- select the next thread to run --- */
/* if ( pNextThread ) : temp. see UserSync.cpp ___pPlaceHolder */
if ( 0 )
{
*__ppCurrentThread = pNextThread;
}
else
{
*__ppCurrentThread = __Pi_SelectNextRunnableThread();
};
__pContext = __Pi_GetThreadContext( *__ppCurrentThread );
#if defined(CONFIG_ASM_I386)
# if defined(CONFIG_ASM_I386_WIN32)
__asm mov esp, 0[eax];
__asm mov ebp, 4[eax];
__asm mov ebx, 12[eax];
__asm mov ecx, 16[eax];
__asm mov edx, 20[eax];
__asm mov esi, 24[eax];
__asm mov edi, 28[eax];
__asm mov eax, 8[eax];
# elif defined(CONFIG_ASM_I386_WIN16)
asm {
les bx,dword ptr [bp-6]
mov sp, word ptr es:[bx+0]
mov bp, word ptr es:[bx+2]
pop dx
pop bx
pop es
pop cx
pop ax
pop di
pop si
pop ds
};
# else
asm( "movl -4(%ebp), %eax" ); // eax = __pContext
asm( "movl 0(%eax), %esp" );
asm( "movl 4(%eax), %ebp" );
asm( "movl 12(%eax), %ebx" );
asm( "movl 16(%eax), %ecx" );
asm( "movl 20(%eax), %edx" );
asm( "movl 24(%eax), %esi" );
asm( "movl 28(%eax), %edi" );
asm( "movl 8(%eax), %eax" );
# endif
#elif defined(CONFIG_ASM_SPARC)
asm( "ld [%l0+8], %o7" );
asm( "ld [%l0+12], %i7" );
asm( "ld [%l0+0], %sp" );
asm( "ld [%l0+4], %fp" );
#elif defined(CONFIG_ASM_MIPS)
asm( "lw $sp, 0($16)" );
asm( "lw $fp, 4($16)" );
asm( "lw $gp, 8($16)" );
asm( "lw $17, 12($16)" );
asm( "lw $18, 16($16)" );
asm( "lw $19, 20($16)" );
asm( "lw $20, 24($16)" );
asm( "lw $21, 28($16)" );
asm( "lw $22, 32($16)" );
asm( "lw $23, 36($16)" );
asm( "lw $31, 40($16)" );
#endif
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Only used for Win16.
\*____________________________________________________________________________*/
void *__Pi_GetCurrentStackPtr()
{
size_t *__pStackPtr = 0;
#if defined(CONFIG_ASM_I386_WIN16)
asm {
mov word ptr [bp-6], sp
};
#endif
return __pStackPtr;
}
#endif /* defined(CONFIG_MULTITHREADED) && defined(CONFIG_MT_USER) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -