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

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

This version is a major evolution: The physical memory allocators,
defined in the kmem.c, ppm.c, and kcm.c files have been modified
to support remote accesses. The RPCs that were previously user
to allocate physical memory in a remote cluster have been removed.
This has been done to cure a dead-lock in case of concurrent page-faults.

This version 2.2 has been tested on a (4 clusters / 2 cores per cluster)
TSAR architecture, for both the "sort" and the "fft" applications.

File size: 10.9 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;
[635]46extern char               * lock_type_str[];
[623]47
48//////////////////////////////////////////////////////////////////////////////////////////
49// This function is called by the process_zero_init() function during kernel_init.
50// It initializes the VMM of the kernel proces_zero (containing all kernel threads)
[635]51// in the local cluster.
52// For TSAR, it registers one "kcode" vseg in kernel VSL, and registers one big page
53// in slot[0] of kernel GPT.
[623]54//////////////////////////////////////////////////////////////////////////////////////////
55error_t  hal_vmm_kernel_init( boot_info_t * info )
[411]56{
[623]57    error_t   error;
[411]58
[623]59    // get pointer on kernel GPT
60    gpt_t * gpt = &process_zero.vmm.gpt;
61
[635]62#if DEBUG_HAL_VMM
63thread_t * this = CURRENT_THREAD;
64printk("\n[%s] thread[%x,%x] enter in cluster %x\n", 
65__FUNCTION__, this->process->pid, this->trdid, local_cxy );
66#endif
[623]67
68    // allocate memory for kernel GPT
69    error = hal_gpt_create( gpt );
70
71    if( error )
[411]72    {
[623]73        printk("\n[PANIC] in %s : cannot allocate kernel GPT in cluster %x\n",
[635]74        __FUNCTION__ , local_cxy );
[623]75        hal_core_sleep();
76    }
[411]77
[624]78#if DEBUG_HAL_VMM
[635]79printk("\n[%s] thread[%x,%x] created GPT PT1 in cluster %x / gpt %x\n", 
[624]80__FUNCTION__, this->process->pid, this->trdid, local_cxy, gpt );
81#endif
82
[623]83    // compute attr and ppn for one PTE1
[624]84    uint32_t attr = GPT_MAPPED | GPT_READABLE | GPT_CACHABLE | GPT_EXECUTABLE | GPT_GLOBAL;
[635]85    uint32_t ppn  = local_cxy << 20;   
[623]86
[629]87    // set PT1[0]
[635]88    hal_gpt_set_pte( XPTR( local_cxy , gpt ) , 0 , attr , ppn );
[623]89
[624]90#if DEBUG_HAL_VMM
[635]91printk("\n[%s] thread[%x,%x] mapped PT1[0] in cluster %d : ppn %x / attr %x\n", 
92__FUNCTION__, this->process->pid, this->trdid, local_cxy, ppn, attr );
[624]93#endif
94
[623]95    // create kcode vseg and register it in kernel VSL
96    vseg_t * vseg = vmm_create_vseg( &process_zero,
[624]97                                     VSEG_TYPE_KCODE,
[623]98                                     info->kcode_base,
99                                     info->kcode_size,
[635]100                                     0, 0,               // file ofset and file size (unused)
101                                     XPTR_NULL,          // no mapper
[623]102                                     local_cxy );
103    if( vseg == NULL )
104    {
105        printk("\n[PANIC] in %s : cannot register vseg to VSL in cluster %x\n",
[635]106        __FUNCTION__ , local_cxy );
[623]107        hal_core_sleep();
108    }
109
[624]110#if DEBUG_HAL_VMM
[635]111printk("\n[%s] thread[%x,%x] registered kcode vseg[%x,%x] in cluster %x\n",
112__FUNCTION__, this->process->pid, this->trdid, info->kcode_base, info->kcode_size, local_cxy );
[624]113hal_vmm_display( &process_zero , true );
114#endif
[623]115
[624]116    return 0;
117
[625]118}  // end hal_vmm_kernel_init()
[624]119
[623]120//////////////////////////////////////////////////////////////////////////////////////////
[625]121// This function registers in the VMM of an user process identified by the <process>
122// argument all required kernel vsegs.
123// For TSAR, it registers in the user VSL the "kcode" vseg, from the local kernel VSL,
124// and register in the user GPT the big page[0] from the local kernel GPT.
[623]125//////////////////////////////////////////////////////////////////////////////////////////
126error_t hal_vmm_kernel_update( process_t * process )
127{
128    uint32_t attr;
129    uint32_t ppn;
130
[625]131    // get cluster identifier
132    cxy_t cxy = local_cxy;
133
[624]134#if DEBUG_HAL_VMM
135thread_t * this = CURRENT_THREAD;
136printk("\n[%s] thread[%x,%x] enter in cluster %x \n", 
[625]137__FUNCTION__, this->process->pid, this->trdid, cxy );
138hal_vmm_display( &process_zero , true );
[624]139hal_vmm_display( process , true );
140#endif
[623]141
[625]142    // get extended pointer on local kernel GPT
[624]143    xptr_t k_gpt_xp = XPTR( cxy , &process_zero.vmm.gpt );
144
145    // get ppn and attributes from slot[0] of kernel GPT
146    hal_gpt_get_pte( k_gpt_xp , 0 , &attr , &ppn );
147
148#if DEBUG_HAL_VMM
149printk("\n[%s] thread[%x,%x] get PT1[0] ( ppn %x / attr %x ) from kernel  GPT\n", 
150__FUNCTION__, this->process->pid, this->trdid, ppn, attr );
151#endif
152
[623]153    // get extended pointer on user GPT
[624]154    xptr_t u_gpt_xp = XPTR( cxy , &process->vmm.gpt );
[623]155
156    // update user GPT : set PTE1 in slot[0]
[629]157    hal_gpt_set_pte( u_gpt_xp , 0 , attr , ppn );
[623]158
[624]159#if DEBUG_HAL_VMM
160printk("\n[%s] thread[%x,%x] registered PT1[0] ( ppn %x / attr %x ) to user GPT\n", 
161__FUNCTION__, this->process->pid, this->trdid, ppn, attr );
162#endif
163
[623]164    // get pointer on the unique vseg registered in kernel VSL
[624]165    xptr_t   root_xp = XPTR( cxy , &process_zero.vmm.vsegs_root );
166    xptr_t   vseg_xp = XLIST_FIRST( root_xp , vseg_t , xlist );
167    vseg_t * vseg    = GET_PTR( vseg_xp );
[411]168
[623]169// check vsegs_nr
[624]170assert( (process_zero.vmm.vsegs_nr == 1 ) ,
171"bad vsegs number in kernel VSL = %d\n", process_zero.vmm.vsegs_nr );
[411]172
[623]173    // update user VSL : register one new vseg for kcode
174    vseg_t * new = vmm_create_vseg( process,
175                                    vseg->type,
176                                    vseg->min,
177                                    vseg->max - vseg->min,
[633]178                                    0, 0,          // file ofset and file size (unused)
179                                    XPTR_NULL,     // no mapper
[623]180                                    local_cxy );
181    if( new == NULL )
182    {
[624]183        printk("\n[ERROR] in %s : cannot update user VSL in cluster %x\n",
[623]184        __FUNCTION__ , cxy );
185        return -1;
186    }
[411]187
[624]188#if DEBUG_HAL_VMM
189printk("\n[%s] thread[%x,%x] created vseg %s ( base %x / size %x ) to user VSL\n", 
190__FUNCTION__, this->process->pid, this->trdid,
191vseg_type_str(vseg->type) , vseg->min, (vseg->max - vseg->min) );
192hal_vmm_display( process , true );
193#endif
[411]194
[624]195    return 0;
196
197}  // end hal_vmm_kernel_update()
198
199//////////////////////////////////////////
[635]200void hal_vmm_display( xptr_t   process_xp,
201                      bool_t   mapping )
[624]202{
[635]203    // get target process cluster and local pointer
204    process_t * process_ptr = GET_PTR( process_xp );
205    cxy_t       process_cxy = GET_CXY( process_xp );
[624]206
[635]207    // get local pointer on target process VMM
208    vmm_t * vmm = &process_ptr->vmm;
209
[624]210    // get pointers on TXT0 chdev
211    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
212    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
213    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
214
[635]215    // build extended pointer on TXT0 lock
[625]216    xptr_t  txt_lock_xp = XPTR( txt0_cxy  , &txt0_ptr->wait_lock );
[624]217
[635]218    // build extended pointers on VSL lock and VSL root
219    xptr_t vsl_root_xp = XPTR( process_cxy , &vmm->vsegs_root );
220    xptr_t vsl_lock_xp = XPTR( process_cxy , &vmm->vsl_lock );
[624]221
[635]222    // get the locks protecting TXT0 and VSL
[625]223    remote_rwlock_rd_acquire( vsl_lock_xp );
224    remote_busylock_acquire( txt_lock_xp );
[624]225
[635]226    // get PID and PT1 values
227    pid_t      pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) );
228    uint32_t * pt1 = hal_remote_lpt( XPTR( process_cxy , &vmm->gpt.ptr ) );
[624]229
[635]230    nolock_printk("\n***** VSL and GPT / process %x / cluster %x / PT1 %x / cycle %d\n",
231    pid , process_cxy , pt1 , (uint32_t)hal_get_cycles() );
232
233    if( xlist_is_empty( vsl_root_xp ) )
[624]234    {
[625]235        nolock_printk("   ... no vsegs registered\n");
236    }
237    else  // scan the list of vsegs
238    {
239        xptr_t         iter_xp;
240        xptr_t         vseg_xp;
[635]241        vseg_t       * vseg_ptr;
242        cxy_t          vseg_cxy;
243        intptr_t       min;
244        intptr_t       max;
245        uint32_t       type;
246        intptr_t       vpn_base;
247        intptr_t       vpn_size;
[624]248
[635]249        XLIST_FOREACH( vsl_root_xp , iter_xp )
[624]250        {
[635]251            vseg_xp  = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
252            vseg_ptr = GET_PTR( vseg_xp );
253            vseg_cxy = GET_CXY( vseg_xp );
[624]254
[635]255            type     =           hal_remote_l32( XPTR( vseg_cxy , &vseg_ptr->type ) );
256            min      = (intptr_t)hal_remote_lpt( XPTR( vseg_cxy , &vseg_ptr->min ) );
257            max      = (intptr_t)hal_remote_lpt( XPTR( vseg_cxy , &vseg_ptr->max ) );
258            vpn_size = (intptr_t)hal_remote_lpt( XPTR( vseg_cxy , &vseg_ptr->vpn_size ) );
259            vpn_base = (intptr_t)hal_remote_lpt( XPTR( vseg_cxy , &vseg_ptr->vpn_base ) );
260
[625]261            nolock_printk(" - %s : base = %X / size = %X / npages = %d\n",
[635]262            vseg_type_str(type), min, max - min, vpn_size );
[625]263
264            if( mapping ) 
[624]265            {
[635]266                vpn_t    vpn     = vpn_base;
267                vpn_t    vpn_max = vpn_base + vpn_size;
[625]268                ppn_t    ppn;
269                uint32_t attr;
[624]270
[625]271                while( vpn < vpn_max )   // scan the PTEs
[624]272                {
[635]273                    hal_gpt_get_pte( XPTR( process_cxy , &vmm->gpt ) , vpn , &attr , &ppn );
[625]274
275                    if( attr & GPT_MAPPED )
[624]276                    {
[625]277                        if( attr & GPT_SMALL )
278                        {
279                            nolock_printk("    . SMALL : vpn = %X / attr = %X / ppn = %X\n",
280                            vpn , attr , ppn );
281                            vpn++;
282                        }
283                        else
284                        {
285                            nolock_printk("    . BIG   : vpn = %X / attr = %X / ppn = %X\n",
286                            vpn , attr , ppn );
287                            vpn += 512;
288                        }
[624]289                    }
290                    else
291                    {
[625]292                        vpn++;
[624]293                    }
294                }
295            }
296        }
297    }
298
[625]299    // release locks
300    remote_busylock_release( txt_lock_xp );
301    remote_rwlock_rd_release( vsl_lock_xp );
[624]302
303}  // hal_vmm_display()
304
Note: See TracBrowser for help on using the repository browser.