Changeset 406 for trunk/kernel/mm/vmm.c
- Timestamp:
- Aug 29, 2017, 12:03:37 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/mm/vmm.c
r401 r406 62 62 intptr_t size; 63 63 64 vmm_dmsg("\n[ INFO] %s : enter for process %x\n", __FUNCTION__ , process->pid );64 vmm_dmsg("\n[DMSG] %s : enter for process %x\n", __FUNCTION__ , process->pid ); 65 65 66 66 // get pointer on VMM … … 83 83 vmm->vsegs_nr = 0; 84 84 list_root_init( &vmm->vsegs_root ); 85 error = grdxt_init( &vmm->grdxt,86 CONFIG_VMM_GRDXT_W1,87 CONFIG_VMM_GRDXT_W2,88 CONFIG_VMM_GRDXT_W3 );89 90 assert( (error == 0) , __FUNCTION__ , "cannot initialize radix tree\n" );91 85 92 86 // register kentry vseg in VMM 93 base = 1<< CONFIG_PPM_PAGE_SHIFT;87 base = CONFIG_VMM_KENTRY_BASE << CONFIG_PPM_PAGE_SHIFT; 94 88 size = CONFIG_VMM_KENTRY_SIZE << CONFIG_PPM_PAGE_SHIFT; 89 95 90 vseg_kentry = vmm_create_vseg( process , base , size , VSEG_TYPE_CODE ); 96 91 97 92 assert( (vseg_kentry != NULL) , __FUNCTION__ , "cannot register kentry vseg\n" ); 98 93 99 vmm->kent_vpn_base = 1; 100 101 // register the args vseg in VMM 102 base = (CONFIG_VMM_KENTRY_SIZE + 1 )<<CONFIG_PPM_PAGE_SHIFT; 94 vmm->kent_vpn_base = base; 95 96 // register args vseg in VMM 97 base = (CONFIG_VMM_KENTRY_BASE + 98 CONFIG_VMM_KENTRY_SIZE ) << CONFIG_PPM_PAGE_SHIFT; 103 99 size = CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_SHIFT; 100 104 101 vseg_args = vmm_create_vseg( process , base , size , VSEG_TYPE_DATA ); 105 102 106 103 assert( (vseg_args != NULL) , __FUNCTION__ , "cannot register args vseg\n" ); 107 104 108 vmm->args_vpn_base = CONFIG_VMM_KENTRY_SIZE + 1;105 vmm->args_vpn_base = base; 109 106 110 107 // register the envs vseg in VMM 111 base = (CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + 1 )<<CONFIG_PPM_PAGE_SHIFT; 108 base = (CONFIG_VMM_KENTRY_BASE + 109 CONFIG_VMM_KENTRY_SIZE + 110 CONFIG_VMM_ARGS_SIZE ) << CONFIG_PPM_PAGE_SHIFT; 112 111 size = CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_SHIFT; 112 113 113 vseg_envs = vmm_create_vseg( process , base , size , VSEG_TYPE_DATA ); 114 114 115 115 assert( (vseg_envs != NULL) , __FUNCTION__ , "cannot register envs vseg\n" ); 116 116 117 vmm->envs_vpn_base = CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + 1;117 vmm->envs_vpn_base = base; 118 118 119 119 // register the heap vseg in VMM 120 120 base = CONFIG_VMM_HEAP_BASE << CONFIG_PPM_PAGE_SHIFT; 121 121 size = (CONFIG_VMM_MMAP_BASE-CONFIG_VMM_HEAP_BASE) << CONFIG_PPM_PAGE_SHIFT; 122 122 123 vseg_heap = vmm_create_vseg( process , base , size , VSEG_TYPE_HEAP ); 123 124 124 125 assert( (vseg_heap != NULL) , __FUNCTION__ , "cannot register heap vseg\n" ); 125 126 126 vmm->heap_vpn_base = CONFIG_VMM_HEAP_BASE;127 vmm->heap_vpn_base = base; 127 128 128 129 // initialize generic page table … … 149 150 hal_fence(); 150 151 151 vmm_dmsg("\n[INFO] %s : exit for process %x\n", __FUNCTION__ , process->pid ); 152 vmm_dmsg("\n[DMSG] %s : exit for process %x / entry_point = %x\n", 153 __FUNCTION__ , process->pid , process->vmm.entry_point ); 152 154 153 155 } // end vmm_init() … … 171 173 dst_vmm->vsegs_nr = 0; 172 174 list_root_init( &dst_vmm->vsegs_root ); 173 error = grdxt_init( &dst_vmm->grdxt,174 CONFIG_VMM_GRDXT_W1,175 CONFIG_VMM_GRDXT_W2,176 CONFIG_VMM_GRDXT_W3 );177 if( error )178 {179 printk("\n[ERROR] in %s : cannot initialize radix tree for process %x\n",180 __FUNCTION__ , dst_process->pid );181 return ENOMEM;182 }183 175 184 176 // loop on src_vmm list of vsegs to create … … 292 284 vseg_free( vseg ); 293 285 } 294 295 // delete vsegs radix_tree296 grdxt_destroy( &vmm->grdxt );297 286 298 287 // release lock … … 456 445 vmm_t * vmm = &process->vmm; 457 446 458 vmm_dmsg("\n[ INFO] %s : enter for process %x / base = %x / size = %x / type = %s\n",447 vmm_dmsg("\n[DMSG] %s : enter for process %x / base = %x / size = %x / type = %s\n", 459 448 __FUNCTION__ , process->pid , base , size , vseg_type_str(type) ); 460 449 … … 527 516 528 517 // update "heap_vseg" in VMM 529 process->vmm.heap_vseg = vseg;518 if( type == VSEG_TYPE_HEAP ) process->vmm.heap_vseg = vseg; 530 519 531 520 // attach vseg to vmm … … 534 523 rwlock_wr_unlock( &vmm->vsegs_lock ); 535 524 536 vmm_dmsg("\n[ INFO] %s : exit for process %x / vseg [%x, %x] has been mapped\n",525 vmm_dmsg("\n[DMSG] %s : exit for process %x / vseg [%x, %x] registered\n", 537 526 __FUNCTION__ , process->pid , vseg->min , vseg->max ); 538 527 539 528 return vseg; 540 } 529 530 } // vmm_create_vseg() 541 531 542 532 ///////////////////////////////////// … … 665 655 } 666 656 657 /////////////////////////////////////////////////////////////////////////////////////// 658 // This low-level static function is called by the vmm_get_vseg() and vmm_resize_vseg() 659 // functions. It scan the list of registered vsegs to find the unique vseg containing 660 // a given virtual address. 661 /////////////////////////////////////////////////////////////////////////////////////// 662 // @ vmm : pointer on the process VMM. 663 // @ vaddr : virtual address. 664 // @ return vseg pointer if success / return NULL if not found. 665 /////////////////////////////////////////////////////////////////////////////////////// 666 static vseg_t * vseg_from_vaddr( vmm_t * vmm, 667 intptr_t vaddr ) 668 { 669 list_entry_t * iter; 670 vseg_t * vseg = NULL; 671 672 // get lock protecting the vseg list 673 rwlock_rd_lock( &vmm->vsegs_lock ); 674 675 // scan the list of vsegs 676 LIST_FOREACH( &vmm->vsegs_root , iter ) 677 { 678 vseg = LIST_ELEMENT( iter , vseg_t , list ); 679 if( (vaddr >= vseg->min) && (vaddr < vseg->max) ) break; 680 } 681 682 // release the lock 683 rwlock_rd_unlock( &vmm->vsegs_lock ); 684 685 return vseg; 686 } 687 667 688 ///////////////////////////////////////////// 668 689 error_t vmm_resize_vseg( process_t * process, … … 670 691 intptr_t size ) 671 692 { 672 error_t error; 693 error_t error; 694 vseg_t * new; 695 vpn_t vpn_min; 696 vpn_t vpn_max; 673 697 674 698 // get pointer on process VMM … … 677 701 intptr_t addr_min = base; 678 702 intptr_t addr_max = base + size; 679 uint32_t shift = CONFIG_PPM_PAGE_SHIFT;680 703 681 704 // get pointer on vseg 682 vseg_t * vseg = grdxt_lookup( &vmm->grdxt , (uint32_t)(base >> shift));705 vseg_t * vseg = vseg_from_vaddr( vmm , base ); 683 706 684 707 if( vseg == NULL) return EINVAL; … … 696 719 error = 0; 697 720 } 698 else if( vseg->min == addr_min ) // vseg must be resized 699 { 700 panic("resize not implemented yet"); 701 error = 0; 702 } 703 else if( vseg->max == addr_max ) // vseg must be resized 704 { 705 panic("resize not implemented yet"); 706 error = 0; 707 } 708 else // vseg cut in three regions => vseg must be resized & new vseg created 709 { 710 panic("resize not implemented yet"); 711 error = 0; 721 else if( vseg->min == addr_min ) // vseg must be resized 722 { 723 // update vseg base address 724 vseg->min = addr_max; 725 726 // update vpn_base and vpn_size 727 vpn_min = vseg->min >> CONFIG_PPM_PAGE_SHIFT; 728 vpn_max = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT; 729 vseg->vpn_base = vpn_min; 730 vseg->vpn_size = vpn_max - vpn_min + 1; 731 error = 0; 732 } 733 else if( vseg->max == addr_max ) // vseg must be resized 734 { 735 // update vseg max address 736 vseg->max = addr_min; 737 738 // update vpn_base and vpn_size 739 vpn_min = vseg->min >> CONFIG_PPM_PAGE_SHIFT; 740 vpn_max = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT; 741 vseg->vpn_base = vpn_min; 742 vseg->vpn_size = vpn_max - vpn_min + 1; 743 error = 0; 744 } 745 else // vseg cut in three regions 746 { 747 // resize existing vseg 748 vseg->max = addr_min; 749 750 // update vpn_base and vpn_size 751 vpn_min = vseg->min >> CONFIG_PPM_PAGE_SHIFT; 752 vpn_max = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT; 753 vseg->vpn_base = vpn_min; 754 vseg->vpn_size = vpn_max - vpn_min + 1; 755 756 // create new vseg 757 new = vmm_create_vseg( process , addr_min , (vseg->max - addr_max) , vseg->type ); 758 if( new == NULL ) error = EINVAL; 759 else error = 0; 712 760 } 713 761 … … 716 764 717 765 return error; 718 } 766 767 } // vmm_resize_vseg() 719 768 720 769 /////////////////////////////////////////// … … 723 772 vseg_t ** found_vseg ) 724 773 { 725 vmm_t * vmm; 726 vseg_t * vseg; 727 728 // get pointer on process VMM 729 vmm = &process->vmm; 730 731 // get lock protecting the vseg list 732 rwlock_rd_lock( &vmm->vsegs_lock ); 733 734 // get pointer on vseg from local radix tree 735 vseg = grdxt_lookup( &vmm->grdxt, (uint32_t)(vaddr >> CONFIG_PPM_PAGE_SHIFT) ); 736 737 // release the lock 738 rwlock_rd_unlock( &vmm->vsegs_lock ); 774 vmm_t * vmm = &process->vmm; 775 776 // get vseg from vaddr 777 vseg_t * vseg = vseg_from_vaddr( vmm , vaddr ); 739 778 740 779 if( vseg == NULL ) // vseg not found in local cluster => try to get it from ref … … 752 791 xptr_t vseg_xp; 753 792 error_t error; 793 754 794 rpc_vmm_get_vseg_client( ref_cxy , ref_ptr , vaddr , &vseg_xp , &error ); 755 795 … … 759 799 vseg = vseg_alloc(); 760 800 761 if( vseg == NULL ) panic("no memory for vseg copy in cluster %x", local_cxy );801 if( vseg == NULL ) return -1; 762 802 763 803 // initialise local vseg from reference … … 765 805 766 806 // register local vseg in local VMM 767 error = vseg_attach( &process->vmm , vseg ); 768 769 if( error ) panic("no memory for vseg registration in cluster %x", local_cxy ); 807 vseg_attach( &process->vmm , vseg ); 770 808 } 771 809 … … 784 822 cxy_t page_cxy; // physical page cluster 785 823 page_t * page_ptr; // local pointer on physical page descriptor 786 787 uint32_t type = vseg->type;788 xptr_t mapper_xp = vseg->mapper_xp; 789 uint32_t flags = vseg->flags;790 791 // get mapper cluster and local pointer 792 cxy_t mapper_cxy = GET_CXY( mapper_xp );793 mapper_t * mapper_ptr = (mapper_t *)GET_PTR( mapper_xp);794 795 // FILE type : simplyget the physical page from the file mapper824 uint32_t index; // missing page index in vseg mapper 825 uint32_t type; // vseg type; 826 827 type = vseg->type; 828 index = vpn - vseg->vpn_base; 829 830 vmm_dmsg("\n[DMSG] %s : core[%x,%d] enter for vpn = %x / type = %s / index = %d\n", 831 __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn, vseg_type_str(type), index ); 832 833 // FILE type : get the physical page from the file mapper 796 834 if( type == VSEG_TYPE_FILE ) 797 835 { 798 // compute index in file mapper 799 uint32_t index = vpn - vseg->vpn_base; 836 // get extended pointer on mapper 837 xptr_t mapper_xp = vseg->mapper_xp; 838 839 assert( (mapper_xp != XPTR_NULL), __FUNCTION__, 840 "mapper not defined for a FILE vseg\n" ); 841 842 // get mapper cluster and local pointer 843 cxy_t mapper_cxy = GET_CXY( mapper_xp ); 844 mapper_t * mapper_ptr = (mapper_t *)GET_PTR( mapper_xp ); 800 845 801 846 // get page descriptor from mapper … … 814 859 } 815 860 816 // all other types : allocate a physical page from target cluster,861 // Other types : allocate a physical page from target cluster, 817 862 else 818 863 { 864 uint32_t flags = vseg->flags; 865 819 866 // get target cluster for physical page 820 867 if( flags & VSEG_DISTRIB ) // depends on VPN LSB 821 868 { 822 uint32_t x_ width = LOCAL_CLUSTER->x_width;823 uint32_t y_ width = LOCAL_CLUSTER->y_width;824 page_cxy = vpn & (( 1<<(x_width + y_width)) - 1);869 uint32_t x_size = LOCAL_CLUSTER->x_size; 870 uint32_t y_size = LOCAL_CLUSTER->y_size; 871 page_cxy = vpn & ((x_size * y_size) - 1); 825 872 } 826 873 else // defined in vseg descriptor … … 831 878 // allocate a physical page in target cluster 832 879 kmem_req_t req; 833 if( page_cxy == local_cxy ) 880 if( page_cxy == local_cxy ) // target cluster is the local cluster 834 881 { 835 882 req.type = KMEM_PAGE; … … 845 892 if( page_ptr == NULL ) return ENOMEM; 846 893 847 // initialise page from .elf file mapper for DATA and CODE types 894 // initialise missing page from .elf file mapper for DATA and CODE types 895 // => the mapper_xp field is an extended pointer on the .elf file mapper 848 896 if( (type == VSEG_TYPE_CODE) || (type == VSEG_TYPE_DATA) ) 849 897 { 850 // compute missing page index in vseg 851 vpn_t page_index = vpn - vseg->vpn_base; 898 // get extended pointer on mapper 899 xptr_t mapper_xp = vseg->mapper_xp; 900 901 assert( (mapper_xp != XPTR_NULL), __FUNCTION__, 902 "mapper not defined for a CODE or DATA vseg\n" ); 903 904 // get mapper cluster and local pointer 905 cxy_t mapper_cxy = GET_CXY( mapper_xp ); 906 mapper_t * mapper_ptr = (mapper_t *)GET_PTR( mapper_xp ); 907 908 // compute missing page offset in vseg 909 uint32_t offset = index << CONFIG_PPM_PAGE_SHIFT; 852 910 853 911 // compute missing page offset in .elf file 854 intptr_t page_offset = vseg->file_offset + 855 (page_index << CONFIG_PPM_PAGE_SHIFT); 856 857 // compute extended pointer on page first byte 858 xptr_t base_xp = ppm_page2base( XPTR( page_cxy , page_ptr ) ); 859 860 // file_size can be smaller than vseg_size for BSS 861 intptr_t file_size = vseg->file_size; 862 863 if( file_size < page_offset ) // fully in BSS 912 uint32_t elf_offset = vseg->file_offset + offset; 913 914 vmm_dmsg("\n[DMSG] %s : core[%x,%d] for vpn = %x / elf_offset = %x\n", 915 __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn, elf_offset ); 916 917 // compute extended pointer on page base 918 xptr_t base_xp = ppm_page2base( XPTR( page_cxy , page_ptr ) ); 919 920 // file_size (in .elf mapper) can be smaller than vseg_size (BSS) 921 uint32_t file_size = vseg->file_size; 922 923 if( file_size < offset ) // missing page fully in BSS 864 924 { 925 vmm_dmsg("\n[DMSG] %s : core[%x,%d] for vpn = %x / fully in BSS\n", 926 __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn ); 927 865 928 if( page_cxy == local_cxy ) 866 929 { … … 872 935 } 873 936 } 874 else if( file_size >= ( page_offset + CONFIG_PPM_PAGE_SIZE) ) // fully in mapper937 else if( file_size >= (offset + CONFIG_PPM_PAGE_SIZE) ) // fully in mapper 875 938 { 939 vmm_dmsg("\n[DMSG] %s : core[%x,%d] for vpn = %x / fully in mapper\n", 940 __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn ); 941 876 942 if( mapper_cxy == local_cxy ) 877 943 { 878 944 error = mapper_move_kernel( mapper_ptr, 879 945 true, // to_buffer 880 page_offset,946 elf_offset, 881 947 base_xp, 882 948 CONFIG_PPM_PAGE_SIZE ); … … 888 954 true, // to buffer 889 955 false, // kernel buffer 890 page_offset,891 (uint64_t)base_xp,956 elf_offset, 957 base_xp, 892 958 CONFIG_PPM_PAGE_SIZE, 893 959 &error ); … … 895 961 if( error ) return EINVAL; 896 962 } 897 else // in mapper : from page_offset -> (file_size - page_offset) 898 // in BSS : from file_size -> (page_offset + page_size) 963 else // both in mapper and in BSS : 964 // - (file_size - offset) bytes from mapper 965 // - (page_size + offset - file_size) bytes from BSS 899 966 { 967 vmm_dmsg("\n[DMSG] %s : core[%x,%d] for vpn = %x / both mapper & BSS\n" 968 " %d bytes from mapper / %d bytes from BSS\n", 969 __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn, 970 file_size - offset , offset + CONFIG_PPM_PAGE_SIZE - file_size ); 971 900 972 // initialize mapper part 901 973 if( mapper_cxy == local_cxy ) … … 903 975 error = mapper_move_kernel( mapper_ptr, 904 976 true, // to buffer 905 page_offset,977 elf_offset, 906 978 base_xp, 907 file_size - page_offset );979 file_size - offset ); 908 980 } 909 981 else … … 913 985 true, // to buffer 914 986 false, // kernel buffer 915 page_offset,916 (uint64_t)base_xp,917 file_size - page_offset,987 elf_offset, 988 base_xp, 989 file_size - offset, 918 990 &error ); 919 991 } … … 923 995 if( page_cxy == local_cxy ) 924 996 { 925 memset( GET_PTR( base_xp ) + file_size - page_offset , 0 ,926 page_offset + CONFIG_PPM_PAGE_SIZE - file_size );997 memset( GET_PTR( base_xp ) + file_size - offset , 0 , 998 offset + CONFIG_PPM_PAGE_SIZE - file_size ); 927 999 } 928 1000 else 929 1001 { 930 hal_remote_memset( base_xp + file_size - page_offset , 0 ,931 page_offset + CONFIG_PPM_PAGE_SIZE - file_size );1002 hal_remote_memset( base_xp + file_size - offset , 0 , 1003 offset + CONFIG_PPM_PAGE_SIZE - file_size ); 932 1004 } 933 1005 } … … 937 1009 // return ppn 938 1010 *ppn = ppm_page2ppn( XPTR( page_cxy , page_ptr ) ); 1011 1012 vmm_dmsg("\n[DMSG] %s : core[%x,%d] exit for vpn = %x / ppn = %x\n", 1013 __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn , *ppn ); 1014 939 1015 return 0; 940 1016 … … 954 1030 // this function must be called by a thread running in the reference cluster 955 1031 assert( (GET_CXY( process->ref_xp ) == local_cxy ) , __FUNCTION__ , 956 " not called in the reference cluster\n" ); 1032 "not called in the reference cluster\n" ); 1033 1034 vmm_dmsg("\n[DMSG] %s : core[%x,%d] enter for vpn = %x in process %x\n", 1035 __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn , process->pid ); 957 1036 958 1037 // get VMM pointer … … 968 1047 if( (attr & GPT_MAPPED) == 0 ) 969 1048 { 1049 vmm_dmsg("\n[DMSG] %s : core[%x,%d] page %x unmapped => try to map it\n", 1050 __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn ); 1051 970 1052 // 1. get vseg pointer 971 1053 error = vmm_get_vseg( process , vpn<<CONFIG_PPM_PAGE_SHIFT , &vseg ); … … 977 1059 return error; 978 1060 } 1061 1062 vmm_dmsg("\n[DMSG] %s : core[%x,%d] found vseg %s / vpn_base = %x / vpn_size = %x\n", 1063 __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , 1064 vseg_type_str(vseg->type) , vseg->vpn_base , vseg->vpn_size ); 979 1065 980 1066 // 2. get physical page number, depending on vseg type … … 1005 1091 } // end new PTE 1006 1092 1093 vmm_dmsg("\n[DMSG] %s : core[%x,%d] exit for vpn = %x / ppn = %x\n", 1094 __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn , ppn ); 1095 1007 1096 *ret_ppn = ppn; 1008 1097 *ret_attr = attr;
Note: See TracChangeset
for help on using the changeset viewer.