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