UbixOS  2.0
trap.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2002-2018 The UbixOS Project.
3  * All rights reserved.
4  *
5  * This was developed by Christopher W. Olsen for the UbixOS Project.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted
8  * provided that the following conditions are met:
9  *
10  * 1) Redistributions of source code must retain the above copyright notice, this list of
11  * conditions, the following disclaimer and the list of authors.
12  * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13  * conditions, the following disclaimer and the list of authors in the documentation and/or
14  * other materials provided with the distribution.
15  * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16  * endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <i386/signal.h>
30 #include <sys/trap.h>
31 #include <sys/gdt.h>
32 #include <ubixos/sched.h>
33 #include <lib/kprintf.h>
34 #include <vmm/vmm.h>
35 #include <ubixos/endtask.h>
36 
37 #define FIRST_TSS_ENTRY 6
38 #define VM_MASK 0x00020000
39 
40 #define store_TR(n) \
41  __asm__("str %%ax\n\t" \
42  "subl %2,%%eax\n\t" \
43  "shrl $4,%%eax" \
44  :"=a" (n) \
45  :"0" (0),"i" (FIRST_TSS_ENTRY<<3))
46 
47 #define get_seg_long(seg,addr) ({ \
48  register unsigned long __res; \
49  __asm__("push %%fs;mov %%ax,%%fs;movl %%fs:%2,%%eax;pop %%fs" \
50  :"=a" (__res):"0" (seg),"m" (*(addr))); \
51  __res;})
52 
53 #define get_seg_byte(seg,addr) ({ \
54 register char __res; \
55 __asm__("push %%fs;mov %%ax,%%fs;movb %%fs:%2,%%al;pop %%fs" \
56  :"=a" (__res):"0" (seg),"m" (*(addr))); \
57 __res;})
58 
59 void die_if_kernel(char *str, struct trapframe *regs, long err) {
60  int i;
61  unsigned long esp;
62  unsigned short ss;
63  unsigned long *stack;
64 
65  esp = (unsigned long) &regs->tf_esp;
66 
67  ss = 0x10; //KERNEL_DS
68 
69  //if ((regs->tf_eflags & VM_MASK) || (3 & regs->tf_cs) == 3)
70  // return;
71 
72  if ((regs->tf_cs & 3) == 3) {
73  esp = regs->tf_esp;
74  ss = regs->tf_ss;
75  kprintf("USER TASK!");
76  }
77  else {
78  ss = 0x10;
79  }
80 
81  kprintf("\n%s: 0x%X:%i, CPU %d, EIP: 0x%X, EFLAGS: 0x%X\n", str, regs->tf_err, regs->tf_trapno, 0x0, regs->tf_eip, regs->tf_eflags);
82  kprintf("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n", regs->tf_eax, regs->tf_ebx, regs->tf_ecx, regs->tf_edx);
83  kprintf("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n", regs->tf_esi, regs->tf_edi, regs->tf_ebp, esp);
84  kprintf("cs: 0x%X ds: 0x%X es: 0x%X fs: 0x%X gs: 0x%X ss: 0x%X\n", regs->tf_cs, regs->tf_ds, regs->tf_es, regs->tf_fs, regs->tf_gs, ss);
85  kprintf("cr0: 0x%X, cr2: 0x%X, cr3: 0x%X, cr4: 0x%X\n", rcr0(), rcr2(), rcr3(), rcr4());
86 
87  store_TR(i);
88  kprintf("Process %s (pid: %i, process nr: %d, stackpage=%08lx)\nStack:", _current->name, _current->id, 0xffff & i, esp);
89 
90  stack = (unsigned long *) esp;
91 
92  for (i = 0; i < 16; i++) {
93  if (i && ((i % 8) == 0))
94  kprintf("\n ");
95  kprintf("%08lx ", get_seg_long(ss, stack++));
96  }
97 
99 }
100 
101 void trap(struct trapframe *frame) {
102  u_int trap_code;
103  u_int cr2 = 0;
104 
105  trap_code = frame->tf_trapno;
106 
107  cr2 = rcr2();
108  kprintf("CR2: 0x%X(0x%X)[0x%X]", cr2, _current->tss.eip, _current->tss.ldt);
109  if (_current->id == 7)
110  while (1)
111  asm("nop");
112 
113  if ((frame->tf_eflags & PSL_I) == 0) {
114  if (SEL_GET_PL(frame->tf_cs) == SEL_PL_USER || (frame->tf_eflags & PSL_VM)) {
115  kpanic("INT OFF! USER");
116  die_if_kernel("TEST", frame, 0x100);
117  }
118  else {
119  kpanic("INT OFF! KERN[0x%X]", trap_code);
120  die_if_kernel("TEST", frame, 0x200);
121  }
122  }
123 
124  kprintf("trap_code: %i(0x%X), EIP: 0x%X, CR2: 0x%X\n", frame->tf_trapno, frame->tf_trapno, frame->tf_eip, cr2);
125  if (frame->tf_trapno == 0xc) {
126  vmm_pageFault(frame, cr2);
127  }
128  else {
129  kpanic("TRAPCODE");
130  die_if_kernel("trapCode", frame, frame->tf_trapno);
131  endTask(_current->id);
132  sched_yield();
133  }
134 }
trapframe::tf_gs
int tf_gs
Definition: trap.h:35
trapframe::tf_ds
int tf_ds
Definition: trap.h:38
trapframe::tf_eip
int tf_eip
Definition: trap.h:50
gdt.h
tssStruct::eip
long eip
Definition: tss.h:47
trap.h
tssStruct::ldt
short ldt
Definition: tss.h:66
trapframe::tf_esi
int tf_esi
Definition: trap.h:40
vmm.h
trapframe::tf_ebp
int tf_ebp
Definition: trap.h:41
endtask.h
taskStruct::tss
struct tssStruct tss
Definition: sched.h:67
SEL_PL_USER
#define SEL_PL_USER
Definition: gdt.h:37
sched.h
kpanic
void kpanic(const char *fmt,...)
print panic message and halt system
Definition: kpanic.c:41
trapframe::tf_eflags
int tf_eflags
Definition: trap.h:52
trapframe::tf_trapno
int tf_trapno
Definition: trap.h:47
trapframe::tf_es
int tf_es
Definition: trap.h:37
taskStruct::id
pidType id
Definition: sched.h:63
trapframe::tf_err
int tf_err
Definition: trap.h:49
trapframe::tf_eax
int tf_eax
Definition: trap.h:46
kprintf.h
SEL_GET_PL
#define SEL_GET_PL(sel)
Definition: gdt.h:32
vmm_pageFault
void vmm_pageFault(struct trapframe *, uint32_t)
Definition: pagefault.c:53
trapframe::tf_fs
int tf_fs
Definition: trap.h:36
PSL_I
#define PSL_I
Definition: cpu.h:43
trapframe::tf_ecx
int tf_ecx
Definition: trap.h:45
trapframe
Definition: trap.h:34
endTask
void endTask(pidType)
Definition: endtask.c:44
trap
void trap(struct trapframe *frame)
Definition: trap.c:101
taskStruct::name
char name[256]
Definition: sched.h:64
store_TR
#define store_TR(n)
Definition: trap.c:40
_current
kTask_t * _current
Definition: sched.c:50
die_if_kernel
void die_if_kernel(char *str, struct trapframe *regs, long err)
Definition: trap.c:59
trapframe::tf_ss
int tf_ss
Definition: trap.h:55
trapframe::tf_esp
int tf_esp
Definition: trap.h:54
trapframe::tf_edx
int tf_edx
Definition: trap.h:44
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
PSL_VM
#define PSL_VM
Definition: cpu.h:49
u_int
unsigned int u_int
Definition: types.h:72
trapframe::tf_edi
int tf_edi
Definition: trap.h:39
get_seg_long
#define get_seg_long(seg, addr)
Definition: trap.c:47
trapframe::tf_cs
int tf_cs
Definition: trap.h:51
signal.h
sched_yield
void sched_yield()
Definition: sched.c:244
trapframe::tf_ebx
int tf_ebx
Definition: trap.h:43