feat: dumpcpu inline & test concluant err 1

This commit is contained in:
Nicolas Hordé 2018-10-10 08:06:39 +02:00
parent 109738dca3
commit 5683c69311
7 changed files with 232 additions and 225 deletions

View File

@ -19,6 +19,90 @@
#define popf() asm("popf"::) #define popf() asm("popf"::)
#define getESP() ({ \
u32 _v; \
asm volatile ("movl %%esp,%%eax;": "=a" (_v)); \
_v; \
})
u32 ss;
u32 gs;
u32 fs;
u32 es;
u32 ds;
u32 eip;
u32 cs;
#define dumpcpu() asm("\
mov $0x6666666, %%eax \n \
mov $0x8888888, %%ebx \n \
pushal \n \
pushf \n \
pushl %%cs\n \
pushl $0x0\n \
pushl %%ds\n \
pushl %%es\n \
pushl %%fs\n \
pushl %%gs\n \
pushl %%ss\n \
pushl $0x0\n \
mov %%cr0, %%eax \n \
pushl %%eax\n \
mov %%cr2, %%eax \n \
pushl %%eax\n \
mov %%cr3, %%eax \n \
pushl %%eax\n \
mov %%cr4, %%eax \n \
pushl %%eax \n \
mov %%dr0, %%eax \n \
pushl %%eax\n \
mov %%dr1, %%eax \n \
pushl %%eax\n \
mov %%dr2, %%eax \n \
pushl %%eax\n \
mov %%dr3, %%eax \n \
pushl %%eax\n \
mov %%dr4, %%eax \n \
pushl %%eax\n \
mov %%dr5, %%eax \n \
pushl %%eax\n \
mov %%dr6, %%eax \n \
pushl %%eax\n \
mov %%dr7, %%eax \n \
pushl %%eax\n \
mov $0xC0000080, %%ecx \n \
rdmsr \n \
pushl %%edx \n \
pushl %%eax":::)
#define restcpu() asm("\
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%eax \n \
popl %%ss\n \
popl %%gs\n \
popl %%fs\n \
popl %%es\n \
popl %%ds\n \
popl %%eax \n \
popl %%eax \n \
popf \n \
popal":::)
#define iret() asm("iret"::) #define iret() asm("iret"::)
#define irqendmaster() asm("movb $0x20,%al; \ #define irqendmaster() asm("movb $0x20,%al; \

View File

@ -42,7 +42,4 @@ bool apic2;
bool cansetflag (u32 flag); bool cansetflag (u32 flag);
void cpuid(u32 op, u32 *eax, u32 *ebx,u32 *ecx, u32 *edx); void cpuid(u32 op, u32 *eax, u32 *ebx,u32 *ecx, u32 *edx);
u8 getcpuinfos(cpuinfo *inf); u8 getcpuinfos(cpuinfo *inf);
u32 getESP(void);
u32 setESP(u32 stack);
void dump_cpu(save_stack *stack);
void show_cpu(save_stack *stack); void show_cpu(save_stack *stack);

View File

@ -61,7 +61,7 @@ typedef struct save_stack {
u32 cr3; u32 cr3;
u32 cr2; u32 cr2;
u32 cr0; u32 cr0;
u32 eflags; u32 oldesp;
u32 ss; u32 ss;
u32 gs; u32 gs;
u32 fs; u32 fs;
@ -69,13 +69,14 @@ typedef struct save_stack {
u32 ds; u32 ds;
u32 eip; u32 eip;
u32 cs; u32 cs;
u32 esp; u32 eflags;
u32 ebp;
u32 edi; u32 edi;
u32 esi; u32 esi;
u32 ebp;
u32 esp;
u32 ebx;
u32 edx; u32 edx;
u32 ecx; u32 ecx;
u32 ebx;
u32 eax; u32 eax;
} save_stack __attribute__ ((packed)); } save_stack __attribute__ ((packed));
/* exception pile */ /* exception pile */

View File

@ -126,83 +126,6 @@ u8 getcpuinfos(cpuinfo * proc)
return 0; return 0;
} }
/******************************************************************************/
/* Retourne la tête de pile */
u32 getESP(void)
{
u32 stack = 0;
asm("movl %%esp,%[result];": [result] "=r"(stack));
return stack;
}
/******************************************************************************/
/* Fixe la tête de pile */
u32 setESP(u32 stack)
{
asm("movl %[param],%%esp;": [param] "=r"(stack));
}
/******************************************************************************/
/* Sauvegarde les registres CPU */
void dump_cpu(save_stack *stack)
{
asm(" movl %%eax,%%ebx":::);
asm(" addl %[size],%%esp \n \
addl $0x8,%%esp \n \
pushl %%eax \n \
pushl %%ebx \n \
pushl %%ecx \n \
pushl %%edx \n \
pushl %%esi \n \
pushl %%edi \n \
pushl %%ebp \n \
pushl %%esp \n \
pushl %%cs \n \
pushl $0x0 \n \
pushl %%ds \n \
pushl %%es \n \
pushl %%fs \n \
pushl %%gs \n \
pushl %%ss \n \
pushf \n \
mov %%eax,%%ebx \n \
mov %%cr0, %%eax \n \
pushl %%eax\n \
mov %%cr2, %%eax \n \
pushl %%eax\n \
mov %%cr3, %%eax \n \
pushl %%eax\n \
mov %%cr4, %%eax \n \
pushl %%eax \n \
mov %%dr0, %%eax \n \
pushl %%eax\n \
mov %%dr1, %%eax \n \
pushl %%eax\n \
mov %%dr2, %%eax \n \
pushl %%eax\n \
mov %%dr3, %%eax \n \
pushl %%eax\n \
mov %%dr4, %%eax \n \
pushl %%eax\n \
mov %%dr5, %%eax \n \
pushl %%eax\n \
mov %%dr6, %%eax \n \
pushl %%eax\n \
mov %%dr7, %%eax \n \
pushl %%eax\n \
mov $0xC0000080, %%ecx \n \
rdmsr \n \
pushl %%edx \n \
pushl %%eax \n \
subl $0x8,%%esp \n \
mov %%ebx,%%eax"::[size] "a" (sizeof(save_stack)):);
save_stack new;
memcpy(&new, stack, sizeof(save_stack), 1);
}
/******************************************************************************/ /******************************************************************************/
/* Affiche les registres CPU */ /* Affiche les registres CPU */
@ -210,7 +133,7 @@ void show_lightcpu(save_stack *stack)
{ {
u32 i; u32 i;
printf("\33[0mEAX=%Y EBX=%Y ECX=%Y EDX=%Y\r\n", stack->eax, stack->ebx, stack->ecx, stack->edx); printf("\33[0mEAX=%Y EBX=%Y ECX=%Y EDX=%Y\r\n", stack->eax, stack->ebx, stack->ecx, stack->edx);
printf("ESI=%Y EDI=%Y ESP=%Y EBP=%Y\r\n", stack->esi, stack->edi, stack->esp, stack->ebp); printf("ESI=%Y EDI=%Y ESP=%Y EBP=%Y\r\n", stack->esi, stack->edi, stack->oldesp, stack->ebp);
printf("EIP=%Y EFL=%Y [%c%c%c%c%c%c%c%c%c]\r\n", stack->eip, stack->eflags, printf("EIP=%Y EFL=%Y [%c%c%c%c%c%c%c%c%c]\r\n", stack->eip, stack->eflags,
(stack->eflags & (1 <<11)) ? 'O':'-', (stack->eflags & (1 <<11)) ? 'O':'-',
(stack->eflags & (1 <<10)) ? 'D':'-', (stack->eflags & (1 <<10)) ? 'D':'-',
@ -252,12 +175,12 @@ void show_lightcpu(save_stack *stack)
} }
printf("\33[0m\r\n\r\n\r\nSTACK\r\n"); printf("\33[0m\r\n\r\n\r\nSTACK\r\n");
if (abs(KERNEL_STACK_ADDR-stack->esp)>0x10000) if (abs(KERNEL_STACK_ADDR-stack->oldesp)>0x10000)
printf("Pile invalide !"); printf("Pile invalide !");
else else
{ {
i=0; i=0;
for (u32 *pointer = stack->esp; pointer < KERNEL_STACK_ADDR; pointer ++) { for (u32 *pointer = stack->oldesp; pointer < KERNEL_STACK_ADDR; pointer ++) {
if (i>0 && i % 10 == 0) print("\033[10A"); if (i>0 && i % 10 == 0) print("\033[10A");
if (i>=10) if (i>=10)
print("\033[25C"); print("\033[25C");
@ -274,7 +197,7 @@ void show_lightcpu(save_stack *stack)
void show_cpu(save_stack *stack) void show_cpu(save_stack *stack)
{ {
printf("EAX=%Y EBX=%Y ECX=%Y EDX=%Y\r\n", stack->eax, stack->ebx, stack->ecx, stack->edx); printf("EAX=%Y EBX=%Y ECX=%Y EDX=%Y\r\n", stack->eax, stack->ebx, stack->ecx, stack->edx);
printf("ESI=%Y EDI=%Y ESP=%Y EBP=%Y\r\n", stack->esi, stack->edi, stack->esp, stack->ebp); printf("ESI=%Y EDI=%Y ESP=%Y EBP=%Y\r\n", stack->esi, stack->edi, stack->oldesp, stack->ebp);
printf("EIP=%Y EFL=%Y [%c%c%c%c%c%c%c%c%c]\r\n", stack->eip, stack->eflags, printf("EIP=%Y EFL=%Y [%c%c%c%c%c%c%c%c%c]\r\n", stack->eip, stack->eflags,
(stack->eflags & (1 <<11)) ? 'O':'-', (stack->eflags & (1 <<11)) ? 'O':'-',
(stack->eflags & (1 <<10)) ? 'D':'-', (stack->eflags & (1 <<10)) ? 'D':'-',
@ -305,12 +228,12 @@ void show_cpu(save_stack *stack)
printf("DR4=%Y DR5=%Y DR6=%Y DR7=%Y\r\n",stack->dr4,stack->dr5,stack->dr6,stack->dr7); printf("DR4=%Y DR5=%Y DR6=%Y DR7=%Y\r\n",stack->dr4,stack->dr5,stack->dr6,stack->dr7);
printf("EFER=%lY\r\n",stack->efer); printf("EFER=%lY\r\n",stack->efer);
printf("STACK\r\n"); printf("STACK\r\n");
if (abs(KERNEL_STACK_ADDR-stack->esp)>0x10000) if (abs(KERNEL_STACK_ADDR-stack->oldesp)>0x10000)
printf("Pile invalide !"); printf("Pile invalide !");
else else
{ {
u32 i=0; u32 i=0;
for (u32 *pointer = stack->esp; pointer < KERNEL_STACK_ADDR; pointer ++) { for (u32 *pointer = stack->oldesp; pointer < KERNEL_STACK_ADDR; pointer ++) {
if (i>0 && i % 10 == 0) print("\033[10A"); if (i>0 && i % 10 == 0) print("\033[10A");
if (i>=10) if (i>=10)
print("\033[25C"); print("\033[25C");

View File

@ -171,186 +171,190 @@ void interruption()
void exception0() void exception0()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x28+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#DE Divide error",&dump); cpuerror("#DE Divide error",dump);
} }
void exception1() void exception1()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x28+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
changevc(6); changevc(6);
clearscreen(); clearscreen();
show_lightcpu(&dump); show_lightcpu(&dump);
setdebugreg(0,0, DBG_CLEAR); setdebugreg(0,0, DBG_CLEAR);
sti(); sti();
waitascii(); waitascii();
cli();
changevc(0);
restcpu();
asm("addl $0x01C, %esp;");
iret(); iret();
} }
void exception2() void exception2()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x28+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("NMI Non-maskable hardware interrupt",&dump); cpuerror("NMI Non-maskable hardware interrupt",dump);
} }
void exception3() void exception3()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x28+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#BP INT3 instruction",&dump); cpuerror("#BP INT3 instruction",dump);
} }
void exception4() void exception4()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x28+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#OF INTO instruction detected overflow",&dump); cpuerror("#OF INTO instruction detected overflow",dump);
} }
void exception5() void exception5()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x28+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#BR BOUND instruction detected overrange",&dump); cpuerror("#BR BOUND instruction detected overrange",dump);
} }
void exception6() void exception6()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x28+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#UD Invalid instruction opcode",&dump); cpuerror("#UD Invalid instruction opcode",dump);
} }
void exception7() void exception7()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x28+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#NM No coprocessor",&dump); cpuerror("#NM No coprocessor",dump);
} }
void exception8() void exception8()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x28+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#DF Double fault",&dump); cpuerror("#DF Double fault",dump);
} }
void exception9() void exception9()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x28+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("Coprocessor segment overrun",&dump); cpuerror("Coprocessor segment overrun",dump);
} }
void exception10() void exception10()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x28+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#TS Invalid task state segment (TSS)",&dump); cpuerror("#TS Invalid task state segment (TSS)",dump);
} }
void exception11() void exception11()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x30+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#NP Segment not present",&dump); cpuerror("#NP Segment not present",dump);
} }
void exception12() void exception12()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x30+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#SS Stack fault",&dump); cpuerror("#SS Stack fault",dump);
} }
void exception13() void exception13()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x30+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#GP General protection fault (GPF)",&dump); cpuerror("#GP General protection fault (GPF)",dump);
} }
void exception14() void exception14()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack *current = getESP()+0x28+255+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
u8* errorstring; u8* errorstring;
u8 completeerrorstring[255]; u8 completeerrorstring[255];
switch (current->error_code & 0xF) { switch (current->error_code & 0xF) {
@ -380,56 +384,56 @@ void exception14()
break; break;
} }
// printf("%X",current->error_code); // printf("%X",current->error_code);
sprintf(&completeerrorstring,"#PF Page fault - %s at adress %X",errorstring,dump.cr2); sprintf(&completeerrorstring,"#PF Page fault - %s at adress %X",errorstring,dump->cr2);
cpuerror(&completeerrorstring,&dump); cpuerror(&completeerrorstring,dump);
} }
void exception15() void exception15()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x30+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("(reserved)",&dump); cpuerror("(reserved)",dump);
} }
void exception16() void exception16()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x30+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#MF Coprocessor error",&dump); cpuerror("#MF Coprocessor error",dump);
} }
void exception17() void exception17()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x30+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#AC Alignment check",&dump); cpuerror("#AC Alignment check",dump);
} }
void exception18() void exception18()
{ {
cli(); cli();
save_stack dump; dumpcpu();
exception_stack_noerror *current = getESP()+0x30+sizeof(save_stack); save_stack *dump = getESP();
dump_cpu(&dump); exception_stack_noerror *current = getESP()+36;
dump.eip=current->eip; dump->eip=current->eip;
dump.cs=current->cs; dump->cs=current->cs;
dump.esp=(current+1); dump->oldesp=(current+1);
cpuerror("#MC Machine check",&dump); cpuerror("#MC Machine check",dump);
} }
/******************************************************************************/ /******************************************************************************/

View File

@ -219,7 +219,6 @@ unsigned convert(u32 keypressed)
else if (key == SCAN_F9) { else if (key == SCAN_F9) {
save_stack dump; save_stack dump;
dump_cpu(&dump);
show_cpu(&dump); show_cpu(&dump);
} }

View File

@ -348,7 +348,6 @@ int info()
int regs() int regs()
{ {
save_stack dump; save_stack dump;
dump_cpu(&dump);
show_cpu(&dump); show_cpu(&dump);
return 0; return 0;
} }