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 6.5) and /sos/physmem.c (Article 4)


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 used list. */                 032      page is in the used list. */
034   sos_count_t ref_cnt;                            033   sos_count_t ref_cnt;
035                                                   034 
036   /** Some data associated with the page when  << 
037   struct sos_kmem_range *kernel_range;         << 
038                                                << 
039   /** The other pages on the list (used, free)    035   /** The other pages on the list (used, free) */
040   struct physical_page_descr *prev, *next;        036   struct physical_page_descr *prev, *next;
041 };                                                037 };
042                                                   038 
043 /** These are some markers present in the exec    039 /** These are some markers present in the executable file (see sos.lds) */
044 extern char __b_kernel, __e_kernel;               040 extern char __b_kernel, __e_kernel;
045                                                   041 
046 /** The array of ppage descriptors will be loc    042 /** The array of ppage descriptors will be located at this address */
047 #define PAGE_DESCR_ARRAY_ADDR \                   043 #define PAGE_DESCR_ARRAY_ADDR \
048   SOS_PAGE_ALIGN_SUP((sos_paddr_t) (& __e_kern    044   SOS_PAGE_ALIGN_SUP((sos_paddr_t) (& __e_kernel))
049 static struct physical_page_descr * physical_p    045 static struct physical_page_descr * physical_page_descr_array;
050                                                   046 
051 /** The list of physical pages currently avail    047 /** The list of physical pages currently available */
052 static struct physical_page_descr *free_ppage;    048 static struct physical_page_descr *free_ppage;
053                                                   049 
054 /** The list of physical pages currently in us    050 /** The list of physical pages currently in use */
055 static struct physical_page_descr *used_ppage;    051 static struct physical_page_descr *used_ppage;
056                                                   052 
057 /** We will store here the interval of valid p    053 /** We will store here the interval of valid physical addresses */
058 static sos_paddr_t physmem_base, physmem_top;     054 static sos_paddr_t physmem_base, physmem_top;
059                                                   055 
060 /** We store the number of pages used/free */     056 /** We store the number of pages used/free */
061 static sos_count_t physmem_total_pages, physme    057 static sos_count_t physmem_total_pages, physmem_used_pages;
062                                                   058 
063 sos_ret_t sos_physmem_subsystem_setup(sos_size !! 059 sos_ret_t sos_physmem_setup(sos_size_t ram_size,
064                                       /* out * !! 060                             /* out */sos_paddr_t *kernel_core_base,
065                                       /* out * !! 061                             /* out */sos_paddr_t *kernel_core_top)
066 {                                                 062 {
067   /* The iterator over the page descriptors */    063   /* The iterator over the page descriptors */
068   struct physical_page_descr *ppage_descr;        064   struct physical_page_descr *ppage_descr;
069                                                   065 
070   /* The iterator over the physical addresses     066   /* The iterator over the physical addresses */
071   sos_paddr_t ppage_addr;                         067   sos_paddr_t ppage_addr;
072                                                   068 
073   /* Make sure ram size is aligned on a page b    069   /* Make sure ram size is aligned on a page boundary */
074   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 */
075                                                   071 
076   /* Reset the used/free page lists before bui    072   /* Reset the used/free page lists before building them */
077   free_ppage = used_ppage = NULL;                 073   free_ppage = used_ppage = NULL;
078   physmem_total_pages = physmem_used_pages = 0    074   physmem_total_pages = physmem_used_pages = 0;
079                                                   075 
080   /* Make sure that there is enough memory to     076   /* Make sure that there is enough memory to store the array of page
081      descriptors */                               077      descriptors */
082   *kernel_core_base = SOS_PAGE_ALIGN_INF((sos_    078   *kernel_core_base = SOS_PAGE_ALIGN_INF((sos_paddr_t)(& __b_kernel));
083   *kernel_core_top                                079   *kernel_core_top
084     = PAGE_DESCR_ARRAY_ADDR                       080     = PAGE_DESCR_ARRAY_ADDR
085       + SOS_PAGE_ALIGN_SUP( (ram_size >> SOS_P    081       + SOS_PAGE_ALIGN_SUP( (ram_size >> SOS_PAGE_SHIFT)
086                             * sizeof(struct ph    082                             * sizeof(struct physical_page_descr));
087   if (*kernel_core_top > ram_size)                083   if (*kernel_core_top > ram_size)
088     return -SOS_ENOMEM;                           084     return -SOS_ENOMEM;
089                                                   085 
090   /* 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
091      means to signal "no page available" */       087      means to signal "no page available" */
092   physmem_base = SOS_PAGE_SIZE;                   088   physmem_base = SOS_PAGE_SIZE;
093   physmem_top  = ram_size;                        089   physmem_top  = ram_size;
094                                                   090 
095   /* Setup the page descriptor arrray */          091   /* Setup the page descriptor arrray */
096   physical_page_descr_array                       092   physical_page_descr_array
097     = (struct physical_page_descr*)PAGE_DESCR_    093     = (struct physical_page_descr*)PAGE_DESCR_ARRAY_ADDR;
098                                                   094 
099   /* Scan the list of physical pages */           095   /* Scan the list of physical pages */
100   for (ppage_addr = 0,                            096   for (ppage_addr = 0,
101          ppage_descr = physical_page_descr_arr    097          ppage_descr = physical_page_descr_array ;
102        ppage_addr < physmem_top ;                 098        ppage_addr < physmem_top ;
103        ppage_addr += SOS_PAGE_SIZE,               099        ppage_addr += SOS_PAGE_SIZE,
104          ppage_descr ++)                          100          ppage_descr ++)
105     {                                             101     {
106       enum { PPAGE_MARK_RESERVED, PPAGE_MARK_F    102       enum { PPAGE_MARK_RESERVED, PPAGE_MARK_FREE,
107              PPAGE_MARK_KERNEL, PPAGE_MARK_HWM    103              PPAGE_MARK_KERNEL, PPAGE_MARK_HWMAP } todo;
108                                                   104 
109       memset(ppage_descr, 0x0, sizeof(struct p    105       memset(ppage_descr, 0x0, sizeof(struct physical_page_descr));
110                                                   106 
111       /* Init the page descriptor for this pag    107       /* Init the page descriptor for this page */
112       ppage_descr->paddr = ppage_addr;            108       ppage_descr->paddr = ppage_addr;
113                                                   109 
114       /* Reserved : 0 ... base */                 110       /* Reserved : 0 ... base */
115       if (ppage_addr < physmem_base)              111       if (ppage_addr < physmem_base)
116         todo = PPAGE_MARK_RESERVED;               112         todo = PPAGE_MARK_RESERVED;
117                                                   113 
118       /* Free : base ... BIOS */                  114       /* Free : base ... BIOS */
119       else if ((ppage_addr >= physmem_base)       115       else if ((ppage_addr >= physmem_base)
120                && (ppage_addr < BIOS_N_VIDEO_S    116                && (ppage_addr < BIOS_N_VIDEO_START))
121         todo = PPAGE_MARK_FREE;                   117         todo = PPAGE_MARK_FREE;
122                                                   118 
123       /* Used : BIOS */                           119       /* Used : BIOS */
124       else if ((ppage_addr >= BIOS_N_VIDEO_STA    120       else if ((ppage_addr >= BIOS_N_VIDEO_START)
125                && (ppage_addr < BIOS_N_VIDEO_E    121                && (ppage_addr < BIOS_N_VIDEO_END))
126         todo = PPAGE_MARK_HWMAP;                  122         todo = PPAGE_MARK_HWMAP;
127                                                   123 
128       /* Free : BIOS ... kernel */                124       /* Free : BIOS ... kernel */
129       else if ((ppage_addr >= BIOS_N_VIDEO_END    125       else if ((ppage_addr >= BIOS_N_VIDEO_END)
130                && (ppage_addr < (sos_paddr_t)     126                && (ppage_addr < (sos_paddr_t) (& __b_kernel)))
131         todo = PPAGE_MARK_FREE;                   127         todo = PPAGE_MARK_FREE;
132                                                   128 
133       /* Used : Kernel code/data/bss + physcal    129       /* Used : Kernel code/data/bss + physcal page descr array */
134       else if ((ppage_addr >= *kernel_core_bas    130       else if ((ppage_addr >= *kernel_core_base)
135                 && (ppage_addr < *kernel_core_    131                 && (ppage_addr < *kernel_core_top))
136         todo = PPAGE_MARK_KERNEL;                 132         todo = PPAGE_MARK_KERNEL;
137                                                   133 
138       /* Free : first page of descr ... end of    134       /* Free : first page of descr ... end of RAM */
139       else                                        135       else
140         todo = PPAGE_MARK_FREE;                   136         todo = PPAGE_MARK_FREE;
141                                                   137 
142       /* Actually does the insertion in the us    138       /* Actually does the insertion in the used/free page lists */
143       physmem_total_pages ++;                     139       physmem_total_pages ++;
144       switch (todo)                               140       switch (todo)
145         {                                         141         {
146         case PPAGE_MARK_FREE:                     142         case PPAGE_MARK_FREE:
147           ppage_descr->ref_cnt = 0;               143           ppage_descr->ref_cnt = 0;
148           list_add_head(free_ppage, ppage_desc    144           list_add_head(free_ppage, ppage_descr);
149           break;                                  145           break;
150                                                   146 
151         case PPAGE_MARK_KERNEL:                   147         case PPAGE_MARK_KERNEL:
152         case PPAGE_MARK_HWMAP:                    148         case PPAGE_MARK_HWMAP:
153           ppage_descr->ref_cnt = 1;               149           ppage_descr->ref_cnt = 1;
154           list_add_head(used_ppage, ppage_desc    150           list_add_head(used_ppage, ppage_descr);
155           physmem_used_pages ++;                  151           physmem_used_pages ++;
156           break;                                  152           break;
157                                                   153 
158         default:                                  154         default:
159           /* Reserved page: nop */                155           /* Reserved page: nop */
160           break;                                  156           break;
161         }                                         157         }
162     }                                             158     }
163                                                   159 
164   return SOS_OK;                                  160   return SOS_OK;
165 }                                                 161 }
166                                                   162 
167                                                   163 
168 sos_paddr_t sos_physmem_ref_physpage_new(sos_b    164 sos_paddr_t sos_physmem_ref_physpage_new(sos_bool_t can_block)
169 {                                                 165 {
170   struct physical_page_descr *ppage_descr;        166   struct physical_page_descr *ppage_descr;
171                                                   167 
172   if (! free_ppage)                               168   if (! free_ppage)
173     return (sos_paddr_t)NULL;                     169     return (sos_paddr_t)NULL;
174                                                   170 
175   /* Retrieve a page in the free list */          171   /* Retrieve a page in the free list */
176   ppage_descr = list_pop_head(free_ppage);        172   ppage_descr = list_pop_head(free_ppage);
177                                                   173 
178   /* The page is assumed not to be already use    174   /* The page is assumed not to be already used */
179   SOS_ASSERT_FATAL(ppage_descr->ref_cnt == 0);    175   SOS_ASSERT_FATAL(ppage_descr->ref_cnt == 0);
180                                                   176 
181   /* Mark the page as used (this of course set    177   /* Mark the page as used (this of course sets the ref count to 1) */
182   ppage_descr->ref_cnt ++;                        178   ppage_descr->ref_cnt ++;
183                                                   179 
184   /* No associated kernel range by default */  << 
185   ppage_descr->kernel_range = NULL;            << 
186                                                << 
187   /* Put the page in the used list */             180   /* Put the page in the used list */
188   list_add_tail(used_ppage, ppage_descr);         181   list_add_tail(used_ppage, ppage_descr);
189   physmem_used_pages ++;                          182   physmem_used_pages ++;
190                                                   183 
191   return ppage_descr->paddr;                      184   return ppage_descr->paddr;
192 }                                                 185 }
193                                                   186 
194                                                   187 
195 /**                                               188 /**
196  * Helper function to get the physical page de    189  * Helper function to get the physical page descriptor for the given
197  * physical page address.                         190  * physical page address.
198  *                                                191  *
199  * @return NULL when out-of-bounds or non-page    192  * @return NULL when out-of-bounds or non-page-aligned
200  */                                               193  */
201 inline static struct physical_page_descr *        194 inline static struct physical_page_descr *
202 get_page_descr_at_paddr(sos_paddr_t ppage_padd    195 get_page_descr_at_paddr(sos_paddr_t ppage_paddr)
203 {                                                 196 {
204   /* Don't handle non-page-aligned addresses *    197   /* Don't handle non-page-aligned addresses */
205   if (ppage_paddr & SOS_PAGE_MASK)                198   if (ppage_paddr & SOS_PAGE_MASK)
206     return NULL;                                  199     return NULL;
207                                                   200   
208   /* Don't support out-of-bounds requests */      201   /* Don't support out-of-bounds requests */
209   if ((ppage_paddr < physmem_base) || (ppage_p    202   if ((ppage_paddr < physmem_base) || (ppage_paddr >= physmem_top))
210     return NULL;                                  203     return NULL;
211                                                   204 
212   return physical_page_descr_array + (ppage_pa    205   return physical_page_descr_array + (ppage_paddr >> SOS_PAGE_SHIFT);
213 }                                                 206 }
214                                                   207 
215                                                   208 
216 sos_ret_t sos_physmem_ref_physpage_at(sos_padd    209 sos_ret_t sos_physmem_ref_physpage_at(sos_paddr_t ppage_paddr)
217 {                                                 210 {
218   struct physical_page_descr *ppage_descr         211   struct physical_page_descr *ppage_descr
219     = get_page_descr_at_paddr(ppage_paddr);       212     = get_page_descr_at_paddr(ppage_paddr);
220                                                   213 
221   if (! ppage_descr)                              214   if (! ppage_descr)
222     return -SOS_EINVAL;                           215     return -SOS_EINVAL;
223                                                   216 
224   /* Increment the reference count for the pag    217   /* Increment the reference count for the page */
225   ppage_descr->ref_cnt ++;                        218   ppage_descr->ref_cnt ++;
226                                                   219 
227   /* If the page is newly referenced (ie we ar    220   /* If the page is newly referenced (ie we are the only owners of the
228      page => ref cnt == 1), transfer it in the    221      page => ref cnt == 1), transfer it in the used pages list */
229   if (ppage_descr->ref_cnt == 1)                  222   if (ppage_descr->ref_cnt == 1)
230     {                                             223     {
231       list_delete(free_ppage, ppage_descr);       224       list_delete(free_ppage, ppage_descr);
232                                                << 
233       /* No associated kernel range by default << 
234       ppage_descr->kernel_range = NULL;        << 
235                                                << 
236       list_add_tail(used_ppage, ppage_descr);     225       list_add_tail(used_ppage, ppage_descr);
237       physmem_used_pages ++;                      226       physmem_used_pages ++;
238                                                   227 
239       /* The page is newly referenced */          228       /* The page is newly referenced */
240       return FALSE;                               229       return FALSE;
241     }                                             230     }
242                                                   231 
243   /* The page was already referenced by someon    232   /* The page was already referenced by someone */
244   return TRUE;                                    233   return TRUE;
245 }                                                 234 }
246                                                   235 
247                                                   236 
248 sos_ret_t                                         237 sos_ret_t
249 sos_physmem_unref_physpage(sos_paddr_t ppage_p    238 sos_physmem_unref_physpage(sos_paddr_t ppage_paddr)
250 {                                                 239 {
251   /* By default the return value indicates tha    240   /* By default the return value indicates that the page is still
252      used */                                      241      used */
253   sos_ret_t retval = FALSE;                       242   sos_ret_t retval = FALSE;
254                                                   243 
255   struct physical_page_descr *ppage_descr         244   struct physical_page_descr *ppage_descr
256     = get_page_descr_at_paddr(ppage_paddr);       245     = get_page_descr_at_paddr(ppage_paddr);
257                                                   246 
258   if (! ppage_descr)                              247   if (! ppage_descr)
259     return -SOS_EINVAL;                           248     return -SOS_EINVAL;
260                                                   249 
261   /* 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 */
262   if (ppage_descr->ref_cnt <= 0)                  251   if (ppage_descr->ref_cnt <= 0)
263     return -SOS_EINVAL;                           252     return -SOS_EINVAL;
264                                                   253 
265   /* Unreference the page, and, when no mappin    254   /* Unreference the page, and, when no mapping is active anymore, put
266      the page in the free list */                 255      the page in the free list */
267   ppage_descr->ref_cnt--;                         256   ppage_descr->ref_cnt--;
268   if (ppage_descr->ref_cnt <= 0)                  257   if (ppage_descr->ref_cnt <= 0)
269     {                                             258     {
270       /* Reset associated kernel range */      << 
271       ppage_descr->kernel_range = NULL;        << 
272                                                << 
273       /* Transfer the page, considered USED, t    259       /* Transfer the page, considered USED, to the free list */
274       list_delete(used_ppage, ppage_descr);       260       list_delete(used_ppage, ppage_descr);
275       physmem_used_pages --;                      261       physmem_used_pages --;
276       list_add_head(free_ppage, ppage_descr);     262       list_add_head(free_ppage, ppage_descr);
277                                                   263 
278       /* Indicate that the page is now unrefer    264       /* Indicate that the page is now unreferenced */
279       retval = TRUE;                              265       retval = TRUE;
280     }                                             266     }
281                                                   267 
282   return retval;                                  268   return retval;
283 }                                              << 
284                                                << 
285                                                << 
286 struct sos_kmem_range* sos_physmem_get_kmem_ra << 
287 {                                              << 
288   struct physical_page_descr *ppage_descr      << 
289     = get_page_descr_at_paddr(ppage_paddr);    << 
290                                                << 
291   if (! ppage_descr)                           << 
292     return NULL;                               << 
293                                                << 
294   return ppage_descr->kernel_range;            << 
295 }                                              << 
296                                                << 
297                                                << 
298 sos_ret_t sos_physmem_set_kmem_range(sos_paddr << 
299                                      struct so << 
300 {                                              << 
301   struct physical_page_descr *ppage_descr      << 
302     = get_page_descr_at_paddr(ppage_paddr);    << 
303                                                << 
304   if (! ppage_descr)                           << 
305     return -SOS_EINVAL;                        << 
306                                                << 
307   ppage_descr->kernel_range = range;           << 
308   return SOS_OK;                               << 
309 }                                              << 
310                                                << 
311 sos_ret_t sos_physmem_get_state(/* out */sos_c << 
312                                 /* out */sos_c << 
313 {                                              << 
314   if (total_ppages)                            << 
315     *total_ppages = physmem_total_pages;       << 
316   if (used_ppages)                             << 
317     *used_ppages = physmem_used_pages;         << 
318   return SOS_OK;                               << 
319 }                                                 269 }
                                                      

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