SimpleOS

LXR

Navigation



Site hébergé par : enix

The LXR Cross Referencer for SOS

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Article:1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 6.5 ] [ 7 ] [ 7.5 ] [ 8 ] [ 9 ] [ 9.5 ]

Diff markup

Differences between /sos/physmem.c (Article 9.5) and /sos/physmem.c (Article 3)


001 /* Copyright (C) 2004  David Decotigny            001 /* Copyright (C) 2004  David Decotigny
002    Copyright (C) 2000  The KOS Team            << 
003                                                   002 
004    This program is free software; you can redi    003    This program is free software; you can redistribute it and/or
005    modify it under the terms of the GNU Genera    004    modify it under the terms of the GNU General Public License
006    as published by the Free Software Foundatio    005    as published by the Free Software Foundation; either version 2
007    of the License, or (at your option) any lat    006    of the License, or (at your option) any later version.
008                                                   007    
009    This program is distributed in the hope tha    008    This program is distributed in the hope that it will be useful,
010    but WITHOUT ANY WARRANTY; without even the     009    but WITHOUT ANY WARRANTY; without even the implied warranty of
011    MERCHANTABILITY or FITNESS FOR A PARTICULAR    010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012    GNU General Public License for more details    011    GNU General Public License for more details.
013                                                   012    
014    You should have received a copy of the GNU     013    You should have received a copy of the GNU General Public License
015    along with this program; if not, write to t    014    along with this program; if not, write to the Free Software
016    Foundation, Inc., 59 Temple Place - Suite 3    015    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
017    USA.                                           016    USA. 
018 */                                                017 */
019 #include <sos/list.h>                             018 #include <sos/list.h>
020 #include <sos/macros.h>                           019 #include <sos/macros.h>
021 #include <sos/assert.h>                           020 #include <sos/assert.h>
022 #include <sos/klibc.h>                            021 #include <sos/klibc.h>
023                                                   022 
024 #include "physmem.h"                              023 #include "physmem.h"
025                                                   024 
026 /** A descriptor for a physical page in SOS */    025 /** A descriptor for a physical page in SOS */
027 struct physical_page_descr                        026 struct physical_page_descr
028 {                                                 027 {
029   /** The physical base address for the page *    028   /** The physical base address for the page */
030   sos_paddr_t paddr;                              029   sos_paddr_t paddr;
031                                                   030 
032   /** The reference count for this physical pa    031   /** The reference count for this physical page. > 0 means that the
033      page is in the nonfree list. */           !! 032      page is in the used list. */
034   sos_count_t ref_cnt;                            033   sos_count_t ref_cnt;
035                                                   034 
036   /**                                          !! 035   /** The other pages on the list (used, free) */
037    * An additional counter for user-defined us << 
038    * counter is up to the user, however a simp << 
039    *   - it can only be incremented/decremente << 
040    *   - when it reaches 0, no automatic actio << 
041    * The first rule means in particular that a << 
042    * count reaches 0 (=> will be freed) cannot << 
043    * value > 0 ! The system will be HALTED if  << 
044    */                                          << 
045   sos_count_t occupation_cnt;                  << 
046                                                << 
047                                                << 
048   /** Some data associated with the page when  << 
049   struct sos_kmem_range *kernel_range;         << 
050                                                << 
051   /** The other pages on the list (nonfree, fr << 
052   struct physical_page_descr *prev, *next;        036   struct physical_page_descr *prev, *next;
053 };                                                037 };
054                                                   038 
055 /** These are some markers present in the exec    039 /** These are some markers present in the executable file (see sos.lds) */
056 extern char __b_kernel, __e_kernel;               040 extern char __b_kernel, __e_kernel;
057                                                   041 
058 /** The array of ppage descriptors will be loc    042 /** The array of ppage descriptors will be located at this address */
059 #define PAGE_DESCR_ARRAY_ADDR \                   043 #define PAGE_DESCR_ARRAY_ADDR \
060   SOS_PAGE_ALIGN_SUP((sos_paddr_t) (& __e_kern    044   SOS_PAGE_ALIGN_SUP((sos_paddr_t) (& __e_kernel))
061 static struct physical_page_descr * physical_p    045 static struct physical_page_descr * physical_page_descr_array;
062                                                   046 
063 /** The list of physical pages currently avail    047 /** The list of physical pages currently available */
064 static struct physical_page_descr *free_ppage;    048 static struct physical_page_descr *free_ppage;
065                                                   049 
066 /** The list of physical pages currently alloc !! 050 /** The list of physical pages currently in use */
067 static struct physical_page_descr *nonfree_ppa !! 051 static struct physical_page_descr *used_ppage;
068                                                   052 
069 /** We will store here the interval of valid p    053 /** We will store here the interval of valid physical addresses */
070 static sos_paddr_t physmem_base, physmem_top;     054 static sos_paddr_t physmem_base, physmem_top;
071                                                   055 
072 /** We store the number of pages nonfree/free  !! 056 /** We store the number of pages used/free */
073 static sos_count_t physmem_total_pages, physme !! 057 static sos_count_t physmem_total_pages, physmem_used_pages;
074                                                   058 
075 sos_ret_t sos_physmem_subsystem_setup(sos_size !! 059 sos_ret_t sos_physmem_setup(sos_size_t ram_size,
076                                       /* out * !! 060                             /* out */sos_paddr_t *kernel_core_base,
077                                       /* out * !! 061                             /* out */sos_paddr_t *kernel_core_top)
078 {                                                 062 {
079   /* The iterator over the page descriptors */    063   /* The iterator over the page descriptors */
080   struct physical_page_descr *ppage_descr;        064   struct physical_page_descr *ppage_descr;
081                                                   065 
082   /* The iterator over the physical addresses     066   /* The iterator over the physical addresses */
083   sos_paddr_t ppage_addr;                         067   sos_paddr_t ppage_addr;
084                                                   068 
085   /* Make sure ram size is aligned on a page b    069   /* Make sure ram size is aligned on a page boundary */
086   ram_size = SOS_PAGE_ALIGN_INF(ram_size);/* Y    070   ram_size = SOS_PAGE_ALIGN_INF(ram_size);/* Yes, we may lose at most a page */
087                                                   071 
088   /* Reset the nonfree/free page lists before  !! 072   /* Reset the used/free page lists before building them */
089   free_ppage = nonfree_ppage = NULL;           !! 073   free_ppage = used_ppage = NULL;
090   physmem_total_pages = physmem_nonfree_pages  !! 074   physmem_total_pages = physmem_used_pages = 0;
091                                                   075 
092   /* Make sure that there is enough memory to     076   /* Make sure that there is enough memory to store the array of page
093      descriptors */                               077      descriptors */
094   *kernel_core_base = SOS_PAGE_ALIGN_INF((sos_    078   *kernel_core_base = SOS_PAGE_ALIGN_INF((sos_paddr_t)(& __b_kernel));
095   *kernel_core_top                                079   *kernel_core_top
096     = PAGE_DESCR_ARRAY_ADDR                       080     = PAGE_DESCR_ARRAY_ADDR
097       + SOS_PAGE_ALIGN_SUP( (ram_size >> SOS_P    081       + SOS_PAGE_ALIGN_SUP( (ram_size >> SOS_PAGE_SHIFT)
098                             * sizeof(struct ph    082                             * sizeof(struct physical_page_descr));
099   if (*kernel_core_top > ram_size)                083   if (*kernel_core_top > ram_size)
100     return -SOS_ENOMEM;                           084     return -SOS_ENOMEM;
101                                                   085 
102   /* Page 0-4kB is not available in order to r    086   /* Page 0-4kB is not available in order to return address 0 as a
103      means to signal "no page available" */       087      means to signal "no page available" */
104   physmem_base = SOS_PAGE_SIZE;                   088   physmem_base = SOS_PAGE_SIZE;
105   physmem_top  = ram_size;                        089   physmem_top  = ram_size;
106                                                   090 
107   /* Setup the page descriptor arrray */          091   /* Setup the page descriptor arrray */
108   physical_page_descr_array                       092   physical_page_descr_array
109     = (struct physical_page_descr*)PAGE_DESCR_    093     = (struct physical_page_descr*)PAGE_DESCR_ARRAY_ADDR;
110                                                   094 
111   /* Scan the list of physical pages */           095   /* Scan the list of physical pages */
112   for (ppage_addr = 0,                            096   for (ppage_addr = 0,
113          ppage_descr = physical_page_descr_arr    097          ppage_descr = physical_page_descr_array ;
114        ppage_addr < physmem_top ;                 098        ppage_addr < physmem_top ;
115        ppage_addr += SOS_PAGE_SIZE,               099        ppage_addr += SOS_PAGE_SIZE,
116          ppage_descr ++)                          100          ppage_descr ++)
117     {                                             101     {
118       enum { PPAGE_MARK_RESERVED, PPAGE_MARK_F    102       enum { PPAGE_MARK_RESERVED, PPAGE_MARK_FREE,
119              PPAGE_MARK_KERNEL, PPAGE_MARK_HWM    103              PPAGE_MARK_KERNEL, PPAGE_MARK_HWMAP } todo;
120                                                   104 
121       memset(ppage_descr, 0x0, sizeof(struct p    105       memset(ppage_descr, 0x0, sizeof(struct physical_page_descr));
122                                                   106 
123       /* Init the page descriptor for this pag    107       /* Init the page descriptor for this page */
124       ppage_descr->paddr = ppage_addr;            108       ppage_descr->paddr = ppage_addr;
125                                                   109 
126       /* Reserved : 0 ... base */                 110       /* Reserved : 0 ... base */
127       if (ppage_addr < physmem_base)              111       if (ppage_addr < physmem_base)
128         todo = PPAGE_MARK_RESERVED;               112         todo = PPAGE_MARK_RESERVED;
129                                                   113 
130       /* Free : base ... BIOS */                  114       /* Free : base ... BIOS */
131       else if ((ppage_addr >= physmem_base)       115       else if ((ppage_addr >= physmem_base)
132                && (ppage_addr < BIOS_N_VIDEO_S    116                && (ppage_addr < BIOS_N_VIDEO_START))
133         todo = PPAGE_MARK_FREE;                   117         todo = PPAGE_MARK_FREE;
134                                                   118 
135       /* Used : BIOS */                           119       /* Used : BIOS */
136       else if ((ppage_addr >= BIOS_N_VIDEO_STA    120       else if ((ppage_addr >= BIOS_N_VIDEO_START)
137                && (ppage_addr < BIOS_N_VIDEO_E    121                && (ppage_addr < BIOS_N_VIDEO_END))
138         todo = PPAGE_MARK_HWMAP;                  122         todo = PPAGE_MARK_HWMAP;
139                                                   123 
140       /* Free : BIOS ... kernel */                124       /* Free : BIOS ... kernel */
141       else if ((ppage_addr >= BIOS_N_VIDEO_END    125       else if ((ppage_addr >= BIOS_N_VIDEO_END)
142                && (ppage_addr < (sos_paddr_t)     126                && (ppage_addr < (sos_paddr_t) (& __b_kernel)))
143         todo = PPAGE_MARK_FREE;                   127         todo = PPAGE_MARK_FREE;
144                                                   128 
145       /* Used : Kernel code/data/bss + physcal    129       /* Used : Kernel code/data/bss + physcal page descr array */
146       else if ((ppage_addr >= *kernel_core_bas    130       else if ((ppage_addr >= *kernel_core_base)
147                 && (ppage_addr < *kernel_core_    131                 && (ppage_addr < *kernel_core_top))
148         todo = PPAGE_MARK_KERNEL;                 132         todo = PPAGE_MARK_KERNEL;
149                                                   133 
150       /* Free : first page of descr ... end of    134       /* Free : first page of descr ... end of RAM */
151       else                                        135       else
152         todo = PPAGE_MARK_FREE;                   136         todo = PPAGE_MARK_FREE;
153                                                   137 
154       /* Actually does the insertion in the no !! 138       /* Actually does the insertion in the used/free page lists */
155       physmem_total_pages ++;                     139       physmem_total_pages ++;
156       switch (todo)                               140       switch (todo)
157         {                                         141         {
158         case PPAGE_MARK_FREE:                     142         case PPAGE_MARK_FREE:
159           ppage_descr->ref_cnt = 0;               143           ppage_descr->ref_cnt = 0;
160           list_add_head(free_ppage, ppage_desc    144           list_add_head(free_ppage, ppage_descr);
161           break;                                  145           break;
162                                                   146 
163         case PPAGE_MARK_KERNEL:                   147         case PPAGE_MARK_KERNEL:
164         case PPAGE_MARK_HWMAP:                    148         case PPAGE_MARK_HWMAP:
165           ppage_descr->ref_cnt = 1;               149           ppage_descr->ref_cnt = 1;
166           list_add_head(nonfree_ppage, ppage_d !! 150           list_add_head(used_ppage, ppage_descr);
167           physmem_nonfree_pages ++;            !! 151           physmem_used_pages ++;
168           break;                                  152           break;
169                                                   153 
170         default:                                  154         default:
171           /* Reserved page: nop */                155           /* Reserved page: nop */
172           break;                                  156           break;
173         }                                         157         }
174     }                                             158     }
175                                                   159 
176   return SOS_OK;                                  160   return SOS_OK;
177 }                                                 161 }
178                                                   162 
179                                                   163 
180 sos_paddr_t sos_physmem_ref_physpage_new(sos_b    164 sos_paddr_t sos_physmem_ref_physpage_new(sos_bool_t can_block)
181 {                                                 165 {
182   struct physical_page_descr *ppage_descr;        166   struct physical_page_descr *ppage_descr;
183                                                   167 
184   if (! free_ppage)                               168   if (! free_ppage)
185     return (sos_paddr_t)NULL;                     169     return (sos_paddr_t)NULL;
186                                                   170 
187   /* Retrieve a page in the free list */          171   /* Retrieve a page in the free list */
188   ppage_descr = list_pop_head(free_ppage);        172   ppage_descr = list_pop_head(free_ppage);
189                                                   173 
190   /* The page is assumed not to be already in  !! 174   /* The page is assumed not to be already used */
191   SOS_ASSERT_FATAL(ppage_descr->ref_cnt == 0);    175   SOS_ASSERT_FATAL(ppage_descr->ref_cnt == 0);
192                                                   176 
193   /* Mark the page as nonfree (this of course  !! 177   /* Mark the page as used (this of course sets the ref count to 1) */
194   ppage_descr->ref_cnt ++;                        178   ppage_descr->ref_cnt ++;
195                                                   179 
196   /* The page descriptor should be unmodified  !! 180   /* Put the page in the used list */
197      deallocation. Otherwise this means that s !! 181   list_add_tail(used_ppage, ppage_descr);
198      overwrote the page descriptor table conte !! 182   physmem_used_pages ++;
199   SOS_ASSERT_FATAL(ppage_descr->occupation_cnt << 
200   SOS_ASSERT_FATAL(ppage_descr->kernel_range = << 
201                                                << 
202   /* Put the page in the nonfree list */       << 
203   list_add_tail(nonfree_ppage, ppage_descr);   << 
204   physmem_nonfree_pages ++;                    << 
205                                                   183 
206   return ppage_descr->paddr;                      184   return ppage_descr->paddr;
207 }                                                 185 }
208                                                   186 
209                                                   187 
210 /**                                               188 /**
211  * Helper function to get the physical page de    189  * Helper function to get the physical page descriptor for the given
212  * physical page address.                         190  * physical page address.
213  *                                                191  *
214  * @return NULL when out-of-bounds or non-page    192  * @return NULL when out-of-bounds or non-page-aligned
215  */                                               193  */
216 inline static struct physical_page_descr *        194 inline static struct physical_page_descr *
217 get_page_descr_at_paddr(sos_paddr_t ppage_padd    195 get_page_descr_at_paddr(sos_paddr_t ppage_paddr)
218 {                                                 196 {
219   /* Don't handle non-page-aligned addresses *    197   /* Don't handle non-page-aligned addresses */
220   if (ppage_paddr & SOS_PAGE_MASK)                198   if (ppage_paddr & SOS_PAGE_MASK)
221     return NULL;                                  199     return NULL;
222                                                   200   
223   /* Don't support out-of-bounds requests */      201   /* Don't support out-of-bounds requests */
224   if ((ppage_paddr < physmem_base) || (ppage_p    202   if ((ppage_paddr < physmem_base) || (ppage_paddr >= physmem_top))
225     return NULL;                                  203     return NULL;
226                                                   204 
227   return physical_page_descr_array + (ppage_pa    205   return physical_page_descr_array + (ppage_paddr >> SOS_PAGE_SHIFT);
228 }                                                 206 }
229                                                   207 
230                                                   208 
231 sos_ret_t sos_physmem_ref_physpage_at(sos_padd    209 sos_ret_t sos_physmem_ref_physpage_at(sos_paddr_t ppage_paddr)
232 {                                                 210 {
233   struct physical_page_descr *ppage_descr         211   struct physical_page_descr *ppage_descr
234     = get_page_descr_at_paddr(ppage_paddr);       212     = get_page_descr_at_paddr(ppage_paddr);
235                                                   213 
236   if (! ppage_descr)                              214   if (! ppage_descr)
237     return -SOS_EINVAL;                           215     return -SOS_EINVAL;
238                                                   216 
239   /* Increment the reference count for the pag    217   /* Increment the reference count for the page */
240   ppage_descr->ref_cnt ++;                        218   ppage_descr->ref_cnt ++;
241                                                   219 
242   /* If the page is newly referenced (ie we ar    220   /* If the page is newly referenced (ie we are the only owners of the
243      page => ref cnt == 1), transfer it in the !! 221      page => ref cnt == 1), transfer it in the used pages list */
244   if (ppage_descr->ref_cnt == 1)                  222   if (ppage_descr->ref_cnt == 1)
245     {                                             223     {
246       /* The page descriptor should be unmodif << 
247          deallocation. Otherwise this means th << 
248          overwrote the page descriptor table c << 
249       SOS_ASSERT_FATAL(ppage_descr->occupation << 
250       SOS_ASSERT_FATAL(ppage_descr->kernel_ran << 
251                                                << 
252       list_delete(free_ppage, ppage_descr);       224       list_delete(free_ppage, ppage_descr);
253       list_add_tail(nonfree_ppage, ppage_descr !! 225       list_add_tail(used_ppage, ppage_descr);
254       physmem_nonfree_pages ++;                !! 226       physmem_used_pages ++;
255                                                   227 
256       /* The page is newly referenced */          228       /* The page is newly referenced */
257       return FALSE;                               229       return FALSE;
258     }                                             230     }
259                                                !! 231 
260   /* The page was already referenced by someon    232   /* The page was already referenced by someone */
261   return TRUE;                                    233   return TRUE;
262 }                                                 234 }
263                                                   235 
264                                                   236 
265 sos_ret_t                                         237 sos_ret_t
266 sos_physmem_unref_physpage(sos_paddr_t ppage_p    238 sos_physmem_unref_physpage(sos_paddr_t ppage_paddr)
267 {                                                 239 {
268   /* By default the return value indicates tha    240   /* By default the return value indicates that the page is still
269      used */                                      241      used */
270   sos_ret_t retval = FALSE;                       242   sos_ret_t retval = FALSE;
271                                                   243 
272   struct physical_page_descr *ppage_descr         244   struct physical_page_descr *ppage_descr
273     = get_page_descr_at_paddr(ppage_paddr);       245     = get_page_descr_at_paddr(ppage_paddr);
274                                                   246 
275   if (! ppage_descr)                              247   if (! ppage_descr)
276     return -SOS_EINVAL;                           248     return -SOS_EINVAL;
277                                                   249 
278   /* Don't do anything if the page is not in t !! 250   /* Don't do anything if the page is not in the used list */
279   if (ppage_descr->ref_cnt <= 0)                  251   if (ppage_descr->ref_cnt <= 0)
280     return -SOS_EINVAL;                           252     return -SOS_EINVAL;
281                                                   253 
282   /* Unreference the page, and, when no mappin    254   /* Unreference the page, and, when no mapping is active anymore, put
283      the page in the free list */                 255      the page in the free list */
284   ppage_descr->ref_cnt--;                         256   ppage_descr->ref_cnt--;
285   if (ppage_descr->ref_cnt == 0)               !! 257   if (ppage_descr->ref_cnt <= 0)
286     {                                             258     {
287       /* Make sure that the occupation counter !! 259       /* Transfer the page, considered USED, to the free list */
288       SOS_ASSERT_FATAL(ppage_descr->occupation !! 260       list_delete(used_ppage, ppage_descr);
289                                                !! 261       physmem_used_pages --;
290       /* Reset associated kernel range */      << 
291       ppage_descr->kernel_range = NULL;        << 
292                                                << 
293       /* Transfer the page, considered NON-FRE << 
294       list_delete(nonfree_ppage, ppage_descr); << 
295       physmem_nonfree_pages --;                << 
296                                                << 
297       list_add_head(free_ppage, ppage_descr);     262       list_add_head(free_ppage, ppage_descr);
298                                                   263 
299       /* Indicate that the page is now unrefer    264       /* Indicate that the page is now unreferenced */
300       retval = TRUE;                              265       retval = TRUE;
301     }                                             266     }
302                                                   267 
303   /* The page was already referenced by someon << 
304   return retval;                                  268   return retval;
305 }                                                 269 }
306                                                << 
307                                                << 
308 sos_ret_t sos_physmem_get_physpage_refcount(so << 
309 {                                              << 
310   struct physical_page_descr *ppage_descr      << 
311     = get_page_descr_at_paddr(ppage_paddr);    << 
312                                                << 
313   if (! ppage_descr)                           << 
314     return -SOS_EINVAL;                        << 
315                                                << 
316   return ppage_descr->ref_cnt;                 << 
317 }                                              << 
318                                                << 
319                                                << 
320 sos_ret_t sos_physmem_inc_physpage_occupation( << 
321 {                                              << 
322   struct physical_page_descr *ppage_descr      << 
323     = get_page_descr_at_paddr(ppage_paddr);    << 
324                                                << 
325   if (! ppage_descr)                           << 
326     return -SOS_EINVAL;                        << 
327                                                << 
328   /* Don't do anything if the page is not in t << 
329   SOS_ASSERT_FATAL(ppage_descr->ref_cnt > 0);  << 
330                                                << 
331   ppage_descr->occupation_cnt ++;              << 
332   return (ppage_descr->occupation_cnt > 1);    << 
333 }                                              << 
334                                                << 
335                                                << 
336 sos_ret_t sos_physmem_dec_physpage_occupation( << 
337 {                                              << 
338   struct physical_page_descr *ppage_descr      << 
339     = get_page_descr_at_paddr(ppage_paddr);    << 
340                                                << 
341   if (! ppage_descr)                           << 
342     return -SOS_EINVAL;                        << 
343                                                << 
344   /* Don't do anything if the page is not in t << 
345   SOS_ASSERT_FATAL(ppage_descr->ref_cnt > 0);  << 
346   SOS_ASSERT_FATAL(ppage_descr->occupation_cnt << 
347                                                << 
348   ppage_descr->occupation_cnt --;              << 
349   return (ppage_descr->occupation_cnt == 0);   << 
350 }                                              << 
351                                                << 
352                                                << 
353 struct sos_kmem_range* sos_physmem_get_kmem_ra << 
354 {                                              << 
355   struct physical_page_descr *ppage_descr      << 
356     = get_page_descr_at_paddr(ppage_paddr);    << 
357                                                << 
358   if (! ppage_descr)                           << 
359     return NULL;                               << 
360                                                << 
361   return ppage_descr->kernel_range;            << 
362 }                                              << 
363                                                << 
364                                                << 
365 sos_ret_t sos_physmem_set_kmem_range(sos_paddr << 
366                                      struct so << 
367 {                                              << 
368   struct physical_page_descr *ppage_descr      << 
369     = get_page_descr_at_paddr(ppage_paddr);    << 
370                                                << 
371   if (! ppage_descr)                           << 
372     return -SOS_EINVAL;                        << 
373                                                << 
374   ppage_descr->kernel_range = range;           << 
375   return SOS_OK;                               << 
376 }                                              << 
377                                                << 
378 sos_ret_t sos_physmem_get_state(/* out */sos_c << 
379                                 /* out */sos_c << 
380 {                                              << 
381   if (total_ppages)                            << 
382     *total_ppages = physmem_total_pages;       << 
383   if (nonfree_ppages)                          << 
384     *nonfree_ppages = physmem_nonfree_pages;   << 
385   return SOS_OK;                               << 
386 }                                              << 
387                                                << 
                                                      

source navigation ] diff markup ] identifier search ] general search ]