Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/sched/task.S need to be fixed.

0001 /*                    The Quest Operating System
0002  *  Copyright (C) 2005-2010  Richard West, Boston University
0003  *
0004  *  This program is free software: you can redistribute it and/or modify
0005  *  it under the terms of the GNU General Public License as published by
0006  *  the Free Software Foundation, either version 3 of the License, or
0007  *  (at your option) any later version.
0008  *
0009  *  This program is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *  GNU General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU General Public License
0015  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018         /* segments */
0019 #define USER_CS 0x1B
0020 #define USER_DS 0x23
0021 
0022         /* offsets into TSS/TCB */
0023 #define initial_EIP 8
0024 #define EBP 4
0025 #define EFLAGS 16
0026 #define ESP 0
0027 #define CR3 12
0028 
0029         /* Software task-switch */
0030         /* %esi: source TSS pointer
0031          * %edi: destination TSS pointer */
0032         .globl _sw_jmp_task
0033 _sw_jmp_task:
0034         /* check if source is NULL */
0035         testl %esi, %esi
0036         jz srcNULL
0037 
0038         /* save EBP */
0039         movl %ebp, EBP(%esi)
0040 
0041         /* save EFLAGS */
0042         pushf
0043         popl %ebx
0044         movl %ebx, EFLAGS(%esi)
0045 
0046         /* save stack */
0047         movl %esp, %ebp
0048         movl %ebp, ESP(%esi)
0049 
0050         jmp restore
0051 
0052         /* force CR3 load if source is NULL */
0053 srcNULL:
0054         movl CR3(%edi), %ebx
0055         jmp stoCR3
0056 
0057         /* -------------------- */
0058         /* restore destination TSS */
0059 restore:
0060         /* restore CR3 if necessary */
0061         movl CR3(%edi), %ebx
0062 
0063         /* skip if kernel thread */
0064         movl $pgd, %edx
0065         cmpl %ebx, %edx
0066         je skipCR3
0067 
0068         /* skip if already loaded */
0069         movl %cr3, %edx
0070         cmpl %ebx, %edx
0071         je skipCR3
0072 
0073 stoCR3: movl %ebx, %cr3
0074 skipCR3:
0075 
0076         /* restore stack */
0077         movl ESP(%edi), %ebp
0078         movl %ebp, %esp
0079 
0080         /* restore EBP */
0081         movl EBP(%edi), %ebp
0082 
0083         /* restore EFLAGS */
0084         movl EFLAGS(%edi), %ebx
0085         pushl %ebx
0086         popf
0087 
0088         ret
0089 
0090 
0091         /* Start initial task */
0092         /* %edi: TSS pointer */
0093         .globl _sw_init_task
0094 _sw_init_task:
0095         /* setup EFLAGS */
0096         movl EFLAGS(%edi), %ebx
0097         pushl %ebx
0098         popf
0099 
0100         /* setup stack */
0101         movl ESP(%edi), %ebp
0102         movl %ebp, %esp
0103 
0104         /* setup CR3 */
0105         movl CR3(%edi), %ebx
0106         movl %ebx, %cr3
0107 
0108         /* invoke task */
0109         ret
0110 
0111         /* Start initial task in userspace */
0112         /* %edi: TSS pointer */
0113         .globl _sw_init_user_task
0114 _sw_init_user_task:
0115         subl $20, %esp
0116 
0117         /* setup CR3 */
0118         movl CR3(%edi), %ebx
0119         movl %ebx, %cr3
0120 
0121         /* setup stack */
0122         movl $USER_DS, 16(%esp)
0123         movl ESP(%edi), %ebp
0124         movl %ebp, 12(%esp)
0125 
0126         /* setup EFLAGS */
0127         movl EFLAGS(%edi), %ebx
0128         movl %ebx, 8(%esp)
0129 
0130         /* setup CS:EIP */
0131         movl $USER_CS, 4(%esp)
0132         movl initial_EIP(%edi), %ebx
0133         movl %ebx, 0(%esp)
0134 
0135         /* setup data segments */
0136         movl $USER_DS, %ebx
0137         movw %bx, %ds
0138         movw %bx, %es
0139         movw %bx, %fs
0140         movw %bx, %gs
0141 
0142         /* so programs can assume zeroed registers */
0143         xorl %eax, %eax
0144         xorl %ebx, %ebx
0145         xorl %ecx, %ecx
0146         xorl %edx, %edx
0147         xorl %esi, %esi
0148         xorl %edi, %edi
0149 
0150         iret
0151 
0152 
0153         /* Fast-path IPC: pass registers EAX, EDX */
0154         /* %esi: source TSS pointer
0155          * %edi: destination TSS pointer */
0156         .globl _sw_ipc
0157 _sw_ipc:
0158         /* save EBP */
0159         movl %ebp, EBP(%esi)
0160 
0161         /* save EFLAGS */
0162         pushf
0163         popl %ebx
0164         movl %ebx, EFLAGS(%esi)
0165 
0166         /* save stack */
0167         movl %esp, %ebp
0168         movl %ebp, ESP(%esi)
0169 
0170         /* restore CR3 if necessary */
0171         movl CR3(%edi), %ebx
0172 
0173         /* skip if kernel thread */
0174         movl $pgd, %ebp
0175         cmpl %ebx, %ebp
0176         je 2f
0177 
0178         /* skip if already loaded */
0179         movl %cr3, %ebp
0180         cmpl %ebx, %ebp
0181         je 2f
0182 
0183         movl %ebx, %cr3
0184 2:
0185 
0186         /* restore stack */
0187         movl ESP(%edi), %ebp
0188         movl %ebp, %esp
0189 
0190         /* restore EBP */
0191         movl EBP(%edi), %ebp
0192 
0193         /* restore EFLAGS */
0194         movl EFLAGS(%edi), %ebx
0195         pushl %ebx
0196         popf
0197 
0198         ret
0199 
0200 /*
0201  * Local Variables:
0202  * mode: asm
0203  * comment-start: "\/*"
0204  * comment-end: "*\/"
0205  * indent-tabs-mode: nil
0206  * End:
0207  */
0208 
0209 /* vi: set et sw=8 sts=8: */