unbroke sparc front-end server protocol
[akaros.git] / kern / arch / sparc / cpuinfo.c
1 #include <arch/sparc.h>
2 #include <arch/arch.h>
3 #include <arch/mmu.h>
4 #include <stdio.h>
5 #include <assert.h>
6 #include <smp.h>
7 #include <pmap.h>
8
9 #ifdef __DEPUTY__
10 #pragma nodeputy
11 #endif
12
13 void
14 static_asserts_can_go_here()
15 {
16         static_assert(SIZEOF_TRAPFRAME_T == sizeof(trapframe_t));
17         static_assert(SIZEOF_TRAPFRAME_T % 8 == 0);
18         static_assert(SIZEOF_ACTIVE_MESSAGE_T == sizeof(active_message_t));
19         static_assert(SIZEOF_ACTIVE_MESSAGE_T % 8 == 0);
20         static_assert(offsetof(env_t,env_tf) % 8 == 0);
21         static_assert(offsetof(env_t,env_ancillary_state) % 8 == 0);
22 }
23
24 void
25 print_cpuinfo(void)
26 {
27         uint32_t psr = read_psr();
28         uint32_t wim = read_wim();
29         uint32_t tbr = read_tbr();
30
31         uint32_t mmucr  = read_mmu_reg(MMU_REG_CTRL);
32         uint32_t mmuctp = read_mmu_reg(MMU_REG_CTXTBL);
33         uint32_t mmuctx = read_mmu_reg(MMU_REG_CTX);
34         uint32_t mmufsr = read_mmu_reg(MMU_REG_FSR);
35         uint32_t mmufar = read_mmu_reg(MMU_REG_FAR);
36
37         cprintf("CPU Info:\n");
38         cprintf("ISA:             SPARC V8\n");
39         cprintf("Implementation:  0x%x\n",(psr >> 28) & 0xF);
40         cprintf("Version:         0x%x\n",(psr >> 24) & 0xF);
41         cprintf("Number of Cores: %d\n",num_cpus);
42         cprintf("Current PSR:     0x%08x\n",psr);
43         cprintf("Current WIM:     0x%08x\n",wim);
44         cprintf("Current TBR:     0x%08x\n",tbr);
45
46         cprintf("SRMMU Info:\n");
47         cprintf("Implementation:  0x%x\n",(mmucr >> 28) & 0xF);
48         cprintf("Version:         0x%x\n",(mmucr >> 24) & 0xF);
49         cprintf("Current CR:      0x%08x\n",mmucr);
50         cprintf("Current CTP:     0x%08x\n",mmuctp);
51         cprintf("Current CTX:     0x%08x\n",mmuctx);
52         cprintf("Current FSR:     0x%08x\n",mmufsr);
53         cprintf("Current FAR:     0x%08x\n",mmufar);
54 }
55
56 void show_mapping(uintptr_t start, size_t size)
57 {
58         extern pde_t l1_page_table[NL1ENTRIES];
59         pte_t* pte;
60         uintptr_t i;
61         page_t* page;
62
63         cprintf("   Virtual    Physical  C M R ACC P\n");
64         cprintf("------------------------------------------\n");
65         for(i = 0; i < size; i += PGSIZE, start += PGSIZE)
66         {
67                 page = page_lookup(l1_page_table,(void*)start,&pte);
68                 cprintf("%08p  ",start);
69                 if(page)
70                 {
71                         cprintf("%08p  %1d %1d %1d  %1x  %1d\n",page2pa(page),
72                                 !!(*pte & PTE_C),!!(*pte & PTE_M),
73                                 !!(*pte & PTE_R),PTE_ACC(*pte),
74                                 !!(*pte & PTE_PTE));
75                 }
76                 else
77                         cprintf("%08p\n",0);
78         }
79 }
80
81 void
82 backtrace(void)
83 {
84         int i = 0, j;
85         env_t* curenv = curenvs[core_id()];
86
87         flush_windows();
88
89         cprintf("Backtrace:\n");
90
91         // hack: assumes (correctly) we aren't a leaf routine
92         void *sp, *pc, *newsp;
93         __asm__ __volatile__ ("mov %%sp,%0; mov %%i7,%1" : "=r"(sp),"=r"(pc));
94
95         assert(sp >= (void*)KERNBASE);
96
97         newsp = *((void**)sp+14);
98         pc = *((void**)sp+15);
99
100         cprintf("initial sp = %x, newsp = %x, pc = %x\n",sp,newsp,pc);
101         assert(newsp >= (void*)KERNBASE);
102
103         while(newsp)
104         {
105                 cprintf("#%02d [<%x>]:\n",++i,pc);
106                 cprintf("    %%sp: %x   Args:",newsp);
107                 for(j = 8; j < 14; j++)
108                         cprintf(" %x",*((void**)sp+j));
109                 cprintf("\n");
110
111                 sp = newsp;
112
113                 if(sp >= (void*)KERNBASE && (void**)sp+16 > ((void**)0+16))
114                 {
115                         newsp = *((void**)sp+14);
116                         pc = *((void**)sp+15);
117                 }
118                 else if(curenv)
119                 {
120                         error_t ret;
121                         ret  = memcpy_from_user(curenv,&newsp,(void**)sp+14,sizeof(void*));
122                         ret |= memcpy_from_user(curenv,&pc,(void**)sp+15,sizeof(void*));
123                         if(ret)
124                         {
125                                 warn("Backtrace would have caused access exception; corrupt user stack?");
126                                 break;
127                         }
128                 }
129                 else
130                 {
131                         warn("Can't backtrace from user with curenv == NULL!");
132                         break;
133                 }
134         }
135 }