source: trunk/hal/tsar_mips32/core/hal_vmm.c @ 633

Last change on this file since 633 was 633, checked in by alain, 5 years ago

cosmetic

File size: 9.7 KB
RevLine 
[411]1/*
[587]2 * hal_vmm.c - Virtual Memory Manager Initialisation for TSAR
[411]3 *
[623]4 * Authors  Alain Greiner (2016,2017,2018,2019)
[411]5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
[457]25#include <hal_kernel_types.h>
[411]26#include <hal_vmm.h>
27#include <hal_gpt.h>
[623]28#include <process.h>
[624]29#include <thread.h>
[411]30#include <vseg.h>
31#include <xlist.h>
32#include <vmm.h>
33#include <remote_rwlock.h>
34
35//////////////////////////////////////////////////////////////////////////////////////////
[623]36// This file contains the TSAR specific code used to initialize the kernel process VMM,
37// or to update an user process VMM with informations related to the kernel vsegs.
38// As the TSAR architure does not use the DATA MMU, but use only the DATA extension
39// address register to access local and remote kernel data, the kernel VSL contains only
40// one "kcode" segment, and the kernel GPT contains only one big page in PT1[0] slot.
[411]41//////////////////////////////////////////////////////////////////////////////////////////
42
[623]43// extern global variables
[624]44extern process_t            process_zero;
45extern chdev_directory_t    chdev_dir;
[623]46
47//////////////////////////////////////////////////////////////////////////////////////////
48// This function is called by the process_zero_init() function during kernel_init.
49// It initializes the VMM of the kernel proces_zero (containing all kernel threads)
[625]50// in the local cluster: For TSAR, it registers one "kcode" vseg in kernel VSL,
51// and registers one big page in slot[0] of kernel GPT.
[623]52//////////////////////////////////////////////////////////////////////////////////////////
53error_t  hal_vmm_kernel_init( boot_info_t * info )
[411]54{
[623]55    error_t   error;
[411]56
[623]57    // get pointer on kernel GPT
58    gpt_t * gpt = &process_zero.vmm.gpt;
59
60    // get cluster identifier
61    cxy_t cxy = local_cxy;
62
63    // allocate memory for kernel GPT
64    error = hal_gpt_create( gpt );
65
66    if( error )
[411]67    {
[623]68        printk("\n[PANIC] in %s : cannot allocate kernel GPT in cluster %x\n",
69        __FUNCTION__ , cxy );
70        hal_core_sleep();
71    }
[411]72
[624]73#if DEBUG_HAL_VMM
74thread_t * this = CURRENT_THREAD;
75printk("\n[%s] thread[%x,%x] enter in cluster %x / gpt %x\n", 
76__FUNCTION__, this->process->pid, this->trdid, local_cxy, gpt );
77#endif
78
[623]79    // compute attr and ppn for one PTE1
[624]80    uint32_t attr = GPT_MAPPED | GPT_READABLE | GPT_CACHABLE | GPT_EXECUTABLE | GPT_GLOBAL;
81    uint32_t ppn  = cxy << 20;   
[623]82
[629]83    // set PT1[0]
84    hal_gpt_set_pte( XPTR( cxy , gpt ) , 0 , attr , ppn );
[623]85
[624]86#if DEBUG_HAL_VMM
87printk("\n[%s] thread[%x,%x] created PT1[0] : ppn %x / attr %x\n", 
88__FUNCTION__, this->process->pid, this->trdid, ppn, attr );
89#endif
90
[623]91    // create kcode vseg and register it in kernel VSL
92    vseg_t * vseg = vmm_create_vseg( &process_zero,
[624]93                                     VSEG_TYPE_KCODE,
[623]94                                     info->kcode_base,
95                                     info->kcode_size,
96                                     0, 0,                  // file ofset and file size (unused)
97                                     XPTR_NULL,             // no mapper
98                                     local_cxy );
99    if( vseg == NULL )
100    {
101        printk("\n[PANIC] in %s : cannot register vseg to VSL in cluster %x\n",
102        __FUNCTION__ , cxy );
103        hal_core_sleep();
104    }
105
[624]106#if DEBUG_HAL_VMM
107printk("\n[%s] thread[%x,%x] registered kcode vseg[%x,%x]\n",
108__FUNCTION__, this->process->pid, this->trdid, info->kcode_base, info->kcode_size );
109hal_vmm_display( &process_zero , true );
110#endif
[623]111
[624]112    return 0;
113
[625]114}  // end hal_vmm_kernel_init()
[624]115
[623]116//////////////////////////////////////////////////////////////////////////////////////////
[625]117// This function registers in the VMM of an user process identified by the <process>
118// argument all required kernel vsegs.
119// For TSAR, it registers in the user VSL the "kcode" vseg, from the local kernel VSL,
120// and register in the user GPT the big page[0] from the local kernel GPT.
[623]121//////////////////////////////////////////////////////////////////////////////////////////
122error_t hal_vmm_kernel_update( process_t * process )
123{
124    uint32_t attr;
125    uint32_t ppn;
126
[625]127    // get cluster identifier
128    cxy_t cxy = local_cxy;
129
[624]130#if DEBUG_HAL_VMM
131thread_t * this = CURRENT_THREAD;
132printk("\n[%s] thread[%x,%x] enter in cluster %x \n", 
[625]133__FUNCTION__, this->process->pid, this->trdid, cxy );
134hal_vmm_display( &process_zero , true );
[624]135hal_vmm_display( process , true );
136#endif
[623]137
[625]138    // get extended pointer on local kernel GPT
[624]139    xptr_t k_gpt_xp = XPTR( cxy , &process_zero.vmm.gpt );
140
141    // get ppn and attributes from slot[0] of kernel GPT
142    hal_gpt_get_pte( k_gpt_xp , 0 , &attr , &ppn );
143
144#if DEBUG_HAL_VMM
145printk("\n[%s] thread[%x,%x] get PT1[0] ( ppn %x / attr %x ) from kernel  GPT\n", 
146__FUNCTION__, this->process->pid, this->trdid, ppn, attr );
147#endif
148
[623]149    // get extended pointer on user GPT
[624]150    xptr_t u_gpt_xp = XPTR( cxy , &process->vmm.gpt );
[623]151
152    // update user GPT : set PTE1 in slot[0]
[629]153    hal_gpt_set_pte( u_gpt_xp , 0 , attr , ppn );
[623]154
[624]155#if DEBUG_HAL_VMM
156printk("\n[%s] thread[%x,%x] registered PT1[0] ( ppn %x / attr %x ) to user GPT\n", 
157__FUNCTION__, this->process->pid, this->trdid, ppn, attr );
158#endif
159
[623]160    // get pointer on the unique vseg registered in kernel VSL
[624]161    xptr_t   root_xp = XPTR( cxy , &process_zero.vmm.vsegs_root );
162    xptr_t   vseg_xp = XLIST_FIRST( root_xp , vseg_t , xlist );
163    vseg_t * vseg    = GET_PTR( vseg_xp );
[411]164
[623]165// check vsegs_nr
[624]166assert( (process_zero.vmm.vsegs_nr == 1 ) ,
167"bad vsegs number in kernel VSL = %d\n", process_zero.vmm.vsegs_nr );
[411]168
[623]169    // update user VSL : register one new vseg for kcode
170    vseg_t * new = vmm_create_vseg( process,
171                                    vseg->type,
172                                    vseg->min,
173                                    vseg->max - vseg->min,
[633]174                                    0, 0,          // file ofset and file size (unused)
175                                    XPTR_NULL,     // no mapper
[623]176                                    local_cxy );
177    if( new == NULL )
178    {
[624]179        printk("\n[ERROR] in %s : cannot update user VSL in cluster %x\n",
[623]180        __FUNCTION__ , cxy );
181        return -1;
182    }
[411]183
[624]184#if DEBUG_HAL_VMM
185printk("\n[%s] thread[%x,%x] created vseg %s ( base %x / size %x ) to user VSL\n", 
186__FUNCTION__, this->process->pid, this->trdid,
187vseg_type_str(vseg->type) , vseg->min, (vseg->max - vseg->min) );
188hal_vmm_display( process , true );
189#endif
[411]190
[624]191    return 0;
192
193}  // end hal_vmm_kernel_update()
194
195//////////////////////////////////////////
196void hal_vmm_display( process_t * process,
197                      bool_t      mapping )
198{
[625]199    // get pointer on process VMM
[624]200    vmm_t * vmm = &process->vmm;
201
202    // get pointers on TXT0 chdev
203    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
204    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
205    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
206
[629]207    // build extended pointer on TXT0 lock and VSL lock
[625]208    xptr_t  txt_lock_xp = XPTR( txt0_cxy  , &txt0_ptr->wait_lock );
209    xptr_t  vsl_lock_xp = XPTR( local_cxy , &vmm->vsl_lock );
[624]210
[625]211    // get root of vsegs list
212    xptr_t root_xp = XPTR( local_cxy , &vmm->vsegs_root );
[624]213
[625]214    // get the locks protecting TXT0, VSL, and GPT
215    remote_rwlock_rd_acquire( vsl_lock_xp );
216    remote_busylock_acquire( txt_lock_xp );
[624]217
[625]218    nolock_printk("\n***** VSL and GPT for process %x in cluster %x / PT1 = %x\n",
219    process->pid , local_cxy , vmm->gpt.ptr );
[624]220
[625]221    if( xlist_is_empty( root_xp ) )
[624]222    {
[625]223        nolock_printk("   ... no vsegs registered\n");
224    }
225    else  // scan the list of vsegs
226    {
227        xptr_t         iter_xp;
228        xptr_t         vseg_xp;
229        vseg_t       * vseg;
[624]230
[625]231        XLIST_FOREACH( root_xp , iter_xp )
[624]232        {
[625]233            vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
234            vseg    = GET_PTR( vseg_xp );
[624]235
[625]236            nolock_printk(" - %s : base = %X / size = %X / npages = %d\n",
237            vseg_type_str(vseg->type), vseg->min, vseg->max - vseg->min, vseg->vpn_size );
238
239            if( mapping ) 
[624]240            {
[625]241                vpn_t    vpn     = vseg->vpn_base;
242                vpn_t    vpn_max = vpn + vseg->vpn_size;
243                ppn_t    ppn;
244                uint32_t attr;
[624]245
[625]246                while( vpn < vpn_max )   // scan the PTEs
[624]247                {
[625]248                    hal_gpt_get_pte( XPTR( local_cxy , &vmm->gpt ) , vpn , &attr , &ppn );
249
250                    if( attr & GPT_MAPPED )
[624]251                    {
[625]252                        if( attr & GPT_SMALL )
253                        {
254                            nolock_printk("    . SMALL : vpn = %X / attr = %X / ppn = %X\n",
255                            vpn , attr , ppn );
256                            vpn++;
257                        }
258                        else
259                        {
260                            nolock_printk("    . BIG   : vpn = %X / attr = %X / ppn = %X\n",
261                            vpn , attr , ppn );
262                            vpn += 512;
263                        }
[624]264                    }
265                    else
266                    {
[625]267                        vpn++;
[624]268                    }
269                }
270            }
271        }
272    }
273
[625]274    // release locks
275    remote_busylock_release( txt_lock_xp );
276    remote_rwlock_rd_release( vsl_lock_xp );
[624]277
278}  // hal_vmm_display()
279
Note: See TracBrowser for help on using the repository browser.