POK(kernelpart)
space.c
Go to the documentation of this file.
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Thu Jan 15 23:34:13 2009
15  */
16 
23 #include <types.h>
24 #include <errno.h>
25 #include <libc.h>
26 #include <bsp.h>
27 #include <core/sched.h>
28 
29 #include <arch.h>
30 #include "thread.h"
31 #include "space.h"
32 #include "sparc_conf.h"
33 #include "context_offset.h"
34 #include "ioports.h"
35 
36 #define KERNEL_STACK_SIZE 8192
37 
41 struct pok_space
42 {
44  uint32_t size;
45 };
46 
48 
52 ptd mmu_contexts_tab[POK_CONFIG_NB_PARTITIONS]
53 __attribute__ ((aligned (POK_CONFIG_NB_PARTITIONS * sizeof (ptd))));
54 
59 __attribute__ ((aligned (MM_LVL1_ENTRIES_NBR * sizeof (ptd))));
60 
65 __attribute__ ((aligned (MM_LVL2_ENTRIES_NBR * sizeof (pte))));
66 
71  uint32_t addr,
72  uint32_t size)
73 {
74  if (size > SPARC_PARTITION_SIZE)
75  {
76 #ifdef POK_NEEDS_DEBUG
77  printf ("pok_create_space: %d: partition size too big 0x%x\n", partition_id, size);
78 #endif
79  return (POK_ERRNO_SIZE);
80  }
81 
82  if ((addr & (SPARC_PAGE_SIZE - 1)) != 0)
83  {
84 #ifdef POK_NEEDS_DEBUG
85  printf ("pok_create_space: %d: partition address not aligned 0x%x\n", partition_id, addr);
86 #endif
87  return (POK_ERRNO_EFAULT);
88  }
89 #ifdef POK_NEEDS_DEBUG
90  printf ("pok_create_space: %d: %x %x\n", partition_id, addr, size);
91 #endif
92  spaces[partition_id].phys_base = addr;
93  spaces[partition_id].size = size;
94 
95  unsigned int as_ptd = mm_index1(SPARC_PARTITION_BASE_VADDR);
96  unsigned int as_pte = mm_index2(SPARC_PARTITION_BASE_VADDR);
97 
98  mmu_level1_tab[partition_id][as_ptd] = ((unsigned int) &(mmu_level2_tab[partition_id]) >> 4) | MM_ET_PTD;
99  /* partition as */
100  mmu_level2_tab[partition_id][as_pte] = ((addr) >> 4) | MM_ACC_RWE | MM_ET_PTE | MM_CACHEABLE;
101 
102  return (POK_ERRNO_OK);
103 }
104 
109  uint8_t new_partition_id)
110 {
111  (void) old_partition_id;
112 
113  asm volatile ("flush\n"
114  "sta %0, [%1] %2;\n"
115  : /* no output */
116  : "r" (new_partition_id), "r" (MMU_CTX_REG), "i" (ASI_M_MMUREGS)
117  : "memory");
118  return (POK_ERRNO_OK);
119 }
120 
126 {
127  (void) addr;
129 }
130 
135  uint32_t entry_rel,
136  uint32_t stack_rel,
137  uint32_t arg1,
138  uint32_t arg2)
139 {
140  uint32_t ctx = spaces[id].phys_base + stack_rel - 0x40;
141 
142  outw(ctx - RESTORE_CNT_OFFSET, 1); /* Only 1 register window needed */
143  outw(ctx - PC_OFFSET, entry_rel);
144  outw(ctx - NPC_OFFSET, entry_rel + 4);
145  outw(ctx - I0_OFFSET, arg1);
146  outw(ctx - I1_OFFSET, arg2);
147 
148 #ifdef POK_NEEDS_DEBUG
149  printf ("space_context_create part_id=%d entry=%x stack=%x arg1=%x arg2=%x\n",
150  id, entry_rel, stack_rel, arg1, arg2);
151 #endif
152 
153  return SPARC_PARTITION_BASE_VADDR + stack_rel - 0x40;
154 }
155 
160 {
161  int i = 0;
162  int j = 0;
163 
164  for (i = 0; i < POK_CONFIG_NB_PARTITIONS; i++)
165  mmu_contexts_tab[i] = MM_ET_INVALID;
166 
167  for (i = 0; i < POK_CONFIG_NB_PARTITIONS; i++)
168  {
169  mmu_contexts_tab[i] = (unsigned int)&(mmu_level1_tab[i]) >> 4 | MM_ET_PTD;
170 
171  for (j = 0; j < MM_LVL1_ENTRIES_NBR; j++)
172  {
173  mmu_level1_tab[i][j] = MM_ET_INVALID;
174  }
175 
176  for (j = 0; j < MM_LVL2_ENTRIES_NBR; j++)
177  {
178  mmu_level2_tab[i][j] = MM_ET_INVALID;
179  }
180  }
181 
182  unsigned int kernel_pte = mm_index1(SPARC_RAM_ADDR);
183 
184  /* the kernel code is always mapped on a 16Mb page (including all partitions) */
185  for (i = 0; i < POK_CONFIG_NB_PARTITIONS; i++)
186  {
187  mmu_level1_tab[i][kernel_pte] = (SPARC_RAM_ADDR >> 4) | MM_ACC_S_RWE | MM_ET_PTE | MM_CACHEABLE;
188  }
189 
190 
191  /* set context table */
192  asm volatile ("sta %0, [%1] %2;\n"
193  : /* no output */
194  : "r" (((unsigned int) mmu_contexts_tab) >> 4), "r" (MMU_CTXTBL_PTR), "i" (ASI_M_MMUREGS)
195  : "memory");
196 
197  /* set context number */
198  pok_space_switch(0, 0);
199 
200  asm volatile ("flush\n"
201  "sta %0, [%1] %2;\n"
202  : /* no output */
203  : "r" (0x1), "r" (MMU_CTRL_REG), "i" (ASI_M_MMUREGS)
204  : "memory");
205 
206 
207 #ifdef POK_NEEDS_DEBUG
208  printf ("pok_arch_space_init: ctx nbr=%u\n", POK_CONFIG_NB_PARTITIONS);
209 #endif
210 }