534 if (new_entry == NULL) 535 panic("vm_map_entry_create: kernel resources exhausted"); 536 return (new_entry); 537} 538 539/* 540 * vm_map_entry_{un,}link: 541 * 542 * Insert/remove entries from maps. 543 */ 544static __inline void 545vm_map_entry_link(vm_map_t map, 546 vm_map_entry_t after_where, 547 vm_map_entry_t entry) 548{ 549 550 CTR4(KTR_VM, 551 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map, 552 map->nentries, entry, after_where); 553 map->nentries++; 554 entry->prev = after_where; 555 entry->next = after_where->next; 556 entry->next->prev = entry; 557 after_where->next = entry; 558} 559 560static __inline void 561vm_map_entry_unlink(vm_map_t map, 562 vm_map_entry_t entry) 563{ 564 vm_map_entry_t prev = entry->prev; 565 vm_map_entry_t next = entry->next; 566 567 next->prev = prev; 568 prev->next = next; 569 map->nentries--; 570 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map, 571 map->nentries, entry); 572} 573 574/* 575 * SAVE_HINT: 576 * 577 * Saves the specified entry as the hint for 578 * future lookups. 579 */ 580#define SAVE_HINT(map,value) \ 581 (map)->hint = (value); 582 583/* 584 * vm_map_lookup_entry: [ internal use only ] 585 * 586 * Finds the map entry containing (or 587 * immediately preceding) the specified address 588 * in the given map; the entry is returned 589 * in the "entry" parameter. The boolean 590 * result indicates whether the address is 591 * actually contained in the map. 592 */ 593boolean_t 594vm_map_lookup_entry( 595 vm_map_t map, 596 vm_offset_t address, 597 vm_map_entry_t *entry) /* OUT */ 598{ 599 vm_map_entry_t cur; 600 vm_map_entry_t last; 601 602 GIANT_REQUIRED; 603 /* 604 * Start looking either from the head of the list, or from the hint. 605 */ 606 cur = map->hint; 607 608 if (cur == &map->header) 609 cur = cur->next; 610 611 if (address >= cur->start) { 612 /* 613 * Go from hint to end of list. 614 * 615 * But first, make a quick check to see if we are already looking 616 * at the entry we want (which is usually the case). Note also 617 * that we don't need to save the hint here... it is the same 618 * hint (unless we are at the header, in which case the hint 619 * didn't buy us anything anyway). 620 */ 621 last = &map->header; 622 if ((cur != last) && (cur->end > address)) { 623 *entry = cur; 624 return (TRUE); 625 } 626 } else { 627 /* 628 * Go from start to hint, *inclusively* 629 */ 630 last = cur->next; 631 cur = map->header.next; 632 } 633 634 /* 635 * Search linearly 636 */ 637 while (cur != last) { 638 if (cur->end > address) { 639 if (address >= cur->start) { 640 /* 641 * Save this lookup for future hints, and 642 * return 643 */ 644 *entry = cur; 645 SAVE_HINT(map, cur); 646 return (TRUE); 647 } 648 break; 649 } 650 cur = cur->next; 651 } 652 *entry = cur->prev; 653 SAVE_HINT(map, *entry); 654 return (FALSE); 655} 656 657/* 658 * vm_map_insert: 659 * 660 * Inserts the given whole VM object into the target 661 * map at the specified address range. The object's 662 * size should match that of the address range. 663 * 664 * Requires that the map be locked, and leaves it so. 665 * 666 * If object is non-NULL, ref count must be bumped by caller 667 * prior to making call to account for the new entry. 668 */ 669int 670vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 671 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, 672 int cow) 673{ 674 vm_map_entry_t new_entry; 675 vm_map_entry_t prev_entry; 676 vm_map_entry_t temp_entry; 677 vm_eflags_t protoeflags; 678 679 GIANT_REQUIRED; 680 681 /* 682 * Check that the start and end points are not bogus. 683 */ 684 if ((start < map->min_offset) || (end > map->max_offset) || 685 (start >= end)) 686 return (KERN_INVALID_ADDRESS); 687 688 /* 689 * Find the entry prior to the proposed starting address; if it's part 690 * of an existing entry, this range is bogus. 691 */ 692 if (vm_map_lookup_entry(map, start, &temp_entry)) 693 return (KERN_NO_SPACE); 694 695 prev_entry = temp_entry; 696 697 /* 698 * Assert that the next entry doesn't overlap the end point. 699 */ 700 if ((prev_entry->next != &map->header) && 701 (prev_entry->next->start < end)) 702 return (KERN_NO_SPACE); 703 704 protoeflags = 0; 705 706 if (cow & MAP_COPY_ON_WRITE) 707 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY; 708 709 if (cow & MAP_NOFAULT) { 710 protoeflags |= MAP_ENTRY_NOFAULT; 711 712 KASSERT(object == NULL, 713 ("vm_map_insert: paradoxical MAP_NOFAULT request")); 714 } 715 if (cow & MAP_DISABLE_SYNCER) 716 protoeflags |= MAP_ENTRY_NOSYNC; 717 if (cow & MAP_DISABLE_COREDUMP) 718 protoeflags |= MAP_ENTRY_NOCOREDUMP; 719 720 if (object) { 721 /* 722 * When object is non-NULL, it could be shared with another 723 * process. We have to set or clear OBJ_ONEMAPPING 724 * appropriately. 725 */ 726 if ((object->ref_count > 1) || (object->shadow_count != 0)) { 727 vm_object_clear_flag(object, OBJ_ONEMAPPING); 728 } 729 } 730 else if ((prev_entry != &map->header) && 731 (prev_entry->eflags == protoeflags) && 732 (prev_entry->end == start) && 733 (prev_entry->wired_count == 0) && 734 ((prev_entry->object.vm_object == NULL) || 735 vm_object_coalesce(prev_entry->object.vm_object, 736 OFF_TO_IDX(prev_entry->offset), 737 (vm_size_t)(prev_entry->end - prev_entry->start), 738 (vm_size_t)(end - prev_entry->end)))) { 739 /* 740 * We were able to extend the object. Determine if we 741 * can extend the previous map entry to include the 742 * new range as well. 743 */ 744 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) && 745 (prev_entry->protection == prot) && 746 (prev_entry->max_protection == max)) { 747 map->size += (end - prev_entry->end); 748 prev_entry->end = end; 749 vm_map_simplify_entry(map, prev_entry); 750 return (KERN_SUCCESS); 751 } 752 753 /* 754 * If we can extend the object but cannot extend the 755 * map entry, we have to create a new map entry. We 756 * must bump the ref count on the extended object to 757 * account for it. object may be NULL. 758 */ 759 object = prev_entry->object.vm_object; 760 offset = prev_entry->offset + 761 (prev_entry->end - prev_entry->start); 762 vm_object_reference(object); 763 } 764 765 /* 766 * NOTE: if conditionals fail, object can be NULL here. This occurs 767 * in things like the buffer map where we manage kva but do not manage 768 * backing objects. 769 */ 770 771 /* 772 * Create a new entry 773 */ 774 new_entry = vm_map_entry_create(map); 775 new_entry->start = start; 776 new_entry->end = end; 777 778 new_entry->eflags = protoeflags; 779 new_entry->object.vm_object = object; 780 new_entry->offset = offset; 781 new_entry->avail_ssize = 0; 782 783 new_entry->inheritance = VM_INHERIT_DEFAULT; 784 new_entry->protection = prot; 785 new_entry->max_protection = max; 786 new_entry->wired_count = 0; 787 788 /* 789 * Insert the new entry into the list 790 */ 791 vm_map_entry_link(map, prev_entry, new_entry); 792 map->size += new_entry->end - new_entry->start; 793 794 /* 795 * Update the free space hint 796 */ 797 if ((map->first_free == prev_entry) && 798 (prev_entry->end >= new_entry->start)) { 799 map->first_free = new_entry; 800 } 801 802#if 0 803 /* 804 * Temporarily removed to avoid MAP_STACK panic, due to 805 * MAP_STACK being a huge hack. Will be added back in 806 * when MAP_STACK (and the user stack mapping) is fixed. 807 */ 808 /* 809 * It may be possible to simplify the entry 810 */ 811 vm_map_simplify_entry(map, new_entry); 812#endif 813 814 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) { 815 pmap_object_init_pt(map->pmap, start, 816 object, OFF_TO_IDX(offset), end - start, 817 cow & MAP_PREFAULT_PARTIAL); 818 } 819 820 return (KERN_SUCCESS); 821} 822 823/* 824 * Find sufficient space for `length' bytes in the given map, starting at 825 * `start'. The map must be locked. Returns 0 on success, 1 on no space. 826 */ 827int 828vm_map_findspace( 829 vm_map_t map, 830 vm_offset_t start, 831 vm_size_t length, 832 vm_offset_t *addr) 833{ 834 vm_map_entry_t entry, next; 835 vm_offset_t end; 836 837 GIANT_REQUIRED; 838 if (start < map->min_offset) 839 start = map->min_offset; 840 if (start > map->max_offset) 841 return (1); 842 843 /* 844 * Look for the first possible address; if there's already something 845 * at this address, we have to start after it. 846 */ 847 if (start == map->min_offset) { 848 if ((entry = map->first_free) != &map->header) 849 start = entry->end; 850 } else { 851 vm_map_entry_t tmp; 852 853 if (vm_map_lookup_entry(map, start, &tmp)) 854 start = tmp->end; 855 entry = tmp; 856 } 857 858 /* 859 * Look through the rest of the map, trying to fit a new region in the 860 * gap between existing regions, or after the very last region. 861 */ 862 for (;; start = (entry = next)->end) { 863 /* 864 * Find the end of the proposed new region. Be sure we didn't 865 * go beyond the end of the map, or wrap around the address; 866 * if so, we lose. Otherwise, if this is the last entry, or 867 * if the proposed new region fits before the next entry, we 868 * win. 869 */ 870 end = start + length; 871 if (end > map->max_offset || end < start) 872 return (1); 873 next = entry->next; 874 if (next == &map->header || next->start >= end) 875 break; 876 } 877 SAVE_HINT(map, entry); 878 *addr = start; 879 if (map == kernel_map) { 880 vm_offset_t ksize; 881 if ((ksize = round_page(start + length)) > kernel_vm_end) { 882 pmap_growkernel(ksize); 883 } 884 } 885 return (0); 886} 887 888/* 889 * vm_map_find finds an unallocated region in the target address 890 * map with the given length. The search is defined to be 891 * first-fit from the specified address; the region found is 892 * returned in the same parameter. 893 * 894 * If object is non-NULL, ref count must be bumped by caller 895 * prior to making call to account for the new entry. 896 */ 897int 898vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 899 vm_offset_t *addr, /* IN/OUT */ 900 vm_size_t length, boolean_t find_space, vm_prot_t prot, 901 vm_prot_t max, int cow) 902{ 903 vm_offset_t start; 904 int result, s = 0; 905 906 GIANT_REQUIRED; 907 908 start = *addr; 909 910 if (map == kmem_map) 911 s = splvm(); 912 913 vm_map_lock(map); 914 if (find_space) { 915 if (vm_map_findspace(map, start, length, addr)) { 916 vm_map_unlock(map); 917 if (map == kmem_map) 918 splx(s); 919 return (KERN_NO_SPACE); 920 } 921 start = *addr; 922 } 923 result = vm_map_insert(map, object, offset, 924 start, start + length, prot, max, cow); 925 vm_map_unlock(map); 926 927 if (map == kmem_map) 928 splx(s); 929 930 return (result); 931} 932 933/* 934 * vm_map_simplify_entry: 935 * 936 * Simplify the given map entry by merging with either neighbor. This 937 * routine also has the ability to merge with both neighbors. 938 * 939 * The map must be locked. 940 * 941 * This routine guarentees that the passed entry remains valid (though 942 * possibly extended). When merging, this routine may delete one or 943 * both neighbors. 944 */ 945void 946vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry) 947{ 948 vm_map_entry_t next, prev; 949 vm_size_t prevsize, esize; 950 951 GIANT_REQUIRED; 952 953 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 954 return; 955 956 prev = entry->prev; 957 if (prev != &map->header) { 958 prevsize = prev->end - prev->start; 959 if ( (prev->end == entry->start) && 960 (prev->object.vm_object == entry->object.vm_object) && 961 (!prev->object.vm_object || 962 (prev->offset + prevsize == entry->offset)) && 963 (prev->eflags == entry->eflags) && 964 (prev->protection == entry->protection) && 965 (prev->max_protection == entry->max_protection) && 966 (prev->inheritance == entry->inheritance) && 967 (prev->wired_count == entry->wired_count)) { 968 if (map->first_free == prev) 969 map->first_free = entry; 970 if (map->hint == prev) 971 map->hint = entry; 972 vm_map_entry_unlink(map, prev); 973 entry->start = prev->start; 974 entry->offset = prev->offset; 975 if (prev->object.vm_object) 976 vm_object_deallocate(prev->object.vm_object); 977 vm_map_entry_dispose(map, prev); 978 } 979 } 980 981 next = entry->next; 982 if (next != &map->header) { 983 esize = entry->end - entry->start; 984 if ((entry->end == next->start) && 985 (next->object.vm_object == entry->object.vm_object) && 986 (!entry->object.vm_object || 987 (entry->offset + esize == next->offset)) && 988 (next->eflags == entry->eflags) && 989 (next->protection == entry->protection) && 990 (next->max_protection == entry->max_protection) && 991 (next->inheritance == entry->inheritance) && 992 (next->wired_count == entry->wired_count)) { 993 if (map->first_free == next) 994 map->first_free = entry; 995 if (map->hint == next) 996 map->hint = entry; 997 vm_map_entry_unlink(map, next); 998 entry->end = next->end; 999 if (next->object.vm_object) 1000 vm_object_deallocate(next->object.vm_object); 1001 vm_map_entry_dispose(map, next); 1002 } 1003 } 1004} 1005/* 1006 * vm_map_clip_start: [ internal use only ] 1007 * 1008 * Asserts that the given entry begins at or after 1009 * the specified address; if necessary, 1010 * it splits the entry into two. 1011 */ 1012#define vm_map_clip_start(map, entry, startaddr) \ 1013{ \ 1014 if (startaddr > entry->start) \ 1015 _vm_map_clip_start(map, entry, startaddr); \ 1016} 1017 1018/* 1019 * This routine is called only when it is known that 1020 * the entry must be split. 1021 */ 1022static void 1023_vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start) 1024{ 1025 vm_map_entry_t new_entry; 1026 1027 /* 1028 * Split off the front portion -- note that we must insert the new 1029 * entry BEFORE this one, so that this entry has the specified 1030 * starting address. 1031 */ 1032 vm_map_simplify_entry(map, entry); 1033 1034 /* 1035 * If there is no object backing this entry, we might as well create 1036 * one now. If we defer it, an object can get created after the map 1037 * is clipped, and individual objects will be created for the split-up 1038 * map. This is a bit of a hack, but is also about the best place to 1039 * put this improvement. 1040 */ 1041 if (entry->object.vm_object == NULL && !map->system_map) { 1042 vm_object_t object; 1043 object = vm_object_allocate(OBJT_DEFAULT, 1044 atop(entry->end - entry->start)); 1045 entry->object.vm_object = object; 1046 entry->offset = 0; 1047 } 1048 1049 new_entry = vm_map_entry_create(map); 1050 *new_entry = *entry; 1051 1052 new_entry->end = start; 1053 entry->offset += (start - entry->start); 1054 entry->start = start; 1055 1056 vm_map_entry_link(map, entry->prev, new_entry); 1057 1058 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1059 vm_object_reference(new_entry->object.vm_object); 1060 } 1061} 1062 1063/* 1064 * vm_map_clip_end: [ internal use only ] 1065 * 1066 * Asserts that the given entry ends at or before 1067 * the specified address; if necessary, 1068 * it splits the entry into two. 1069 */ 1070#define vm_map_clip_end(map, entry, endaddr) \ 1071{ \ 1072 if (endaddr < entry->end) \ 1073 _vm_map_clip_end(map, entry, endaddr); \ 1074} 1075 1076/* 1077 * This routine is called only when it is known that 1078 * the entry must be split. 1079 */ 1080static void 1081_vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end) 1082{ 1083 vm_map_entry_t new_entry; 1084 1085 /* 1086 * If there is no object backing this entry, we might as well create 1087 * one now. If we defer it, an object can get created after the map 1088 * is clipped, and individual objects will be created for the split-up 1089 * map. This is a bit of a hack, but is also about the best place to 1090 * put this improvement. 1091 */ 1092 if (entry->object.vm_object == NULL && !map->system_map) { 1093 vm_object_t object; 1094 object = vm_object_allocate(OBJT_DEFAULT, 1095 atop(entry->end - entry->start)); 1096 entry->object.vm_object = object; 1097 entry->offset = 0; 1098 } 1099 1100 /* 1101 * Create a new entry and insert it AFTER the specified entry 1102 */ 1103 new_entry = vm_map_entry_create(map); 1104 *new_entry = *entry; 1105 1106 new_entry->start = entry->end = end; 1107 new_entry->offset += (end - entry->start); 1108 1109 vm_map_entry_link(map, entry, new_entry); 1110 1111 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1112 vm_object_reference(new_entry->object.vm_object); 1113 } 1114} 1115 1116/* 1117 * VM_MAP_RANGE_CHECK: [ internal use only ] 1118 * 1119 * Asserts that the starting and ending region 1120 * addresses fall within the valid range of the map. 1121 */ 1122#define VM_MAP_RANGE_CHECK(map, start, end) \ 1123 { \ 1124 if (start < vm_map_min(map)) \ 1125 start = vm_map_min(map); \ 1126 if (end > vm_map_max(map)) \ 1127 end = vm_map_max(map); \ 1128 if (start > end) \ 1129 start = end; \ 1130 } 1131 1132/* 1133 * vm_map_submap: [ kernel use only ] 1134 * 1135 * Mark the given range as handled by a subordinate map. 1136 * 1137 * This range must have been created with vm_map_find, 1138 * and no other operations may have been performed on this 1139 * range prior to calling vm_map_submap. 1140 * 1141 * Only a limited number of operations can be performed 1142 * within this rage after calling vm_map_submap: 1143 * vm_fault 1144 * [Don't try vm_map_copy!] 1145 * 1146 * To remove a submapping, one must first remove the 1147 * range from the superior map, and then destroy the 1148 * submap (if desired). [Better yet, don't try it.] 1149 */ 1150int 1151vm_map_submap( 1152 vm_map_t map, 1153 vm_offset_t start, 1154 vm_offset_t end, 1155 vm_map_t submap) 1156{ 1157 vm_map_entry_t entry; 1158 int result = KERN_INVALID_ARGUMENT; 1159 1160 GIANT_REQUIRED; 1161 1162 vm_map_lock(map); 1163 1164 VM_MAP_RANGE_CHECK(map, start, end); 1165 1166 if (vm_map_lookup_entry(map, start, &entry)) { 1167 vm_map_clip_start(map, entry, start); 1168 } else 1169 entry = entry->next; 1170 1171 vm_map_clip_end(map, entry, end); 1172 1173 if ((entry->start == start) && (entry->end == end) && 1174 ((entry->eflags & MAP_ENTRY_COW) == 0) && 1175 (entry->object.vm_object == NULL)) { 1176 entry->object.sub_map = submap; 1177 entry->eflags |= MAP_ENTRY_IS_SUB_MAP; 1178 result = KERN_SUCCESS; 1179 } 1180 vm_map_unlock(map); 1181 1182 return (result); 1183} 1184 1185/* 1186 * vm_map_protect: 1187 * 1188 * Sets the protection of the specified address 1189 * region in the target map. If "set_max" is 1190 * specified, the maximum protection is to be set; 1191 * otherwise, only the current protection is affected. 1192 */ 1193int 1194vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 1195 vm_prot_t new_prot, boolean_t set_max) 1196{ 1197 vm_map_entry_t current; 1198 vm_map_entry_t entry; 1199 1200 GIANT_REQUIRED; 1201 vm_map_lock(map); 1202 1203 VM_MAP_RANGE_CHECK(map, start, end); 1204 1205 if (vm_map_lookup_entry(map, start, &entry)) { 1206 vm_map_clip_start(map, entry, start); 1207 } else { 1208 entry = entry->next; 1209 } 1210 1211 /* 1212 * Make a first pass to check for protection violations. 1213 */ 1214 current = entry; 1215 while ((current != &map->header) && (current->start < end)) { 1216 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1217 vm_map_unlock(map); 1218 return (KERN_INVALID_ARGUMENT); 1219 } 1220 if ((new_prot & current->max_protection) != new_prot) { 1221 vm_map_unlock(map); 1222 return (KERN_PROTECTION_FAILURE); 1223 } 1224 current = current->next; 1225 } 1226 1227 /* 1228 * Go back and fix up protections. [Note that clipping is not 1229 * necessary the second time.] 1230 */ 1231 current = entry; 1232 while ((current != &map->header) && (current->start < end)) { 1233 vm_prot_t old_prot; 1234 1235 vm_map_clip_end(map, current, end); 1236 1237 old_prot = current->protection; 1238 if (set_max) 1239 current->protection = 1240 (current->max_protection = new_prot) & 1241 old_prot; 1242 else 1243 current->protection = new_prot; 1244 1245 /* 1246 * Update physical map if necessary. Worry about copy-on-write 1247 * here -- CHECK THIS XXX 1248 */ 1249 if (current->protection != old_prot) { 1250#define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 1251 VM_PROT_ALL) 1252 pmap_protect(map->pmap, current->start, 1253 current->end, 1254 current->protection & MASK(current)); 1255#undef MASK 1256 } 1257 vm_map_simplify_entry(map, current); 1258 current = current->next; 1259 } 1260 vm_map_unlock(map); 1261 return (KERN_SUCCESS); 1262} 1263 1264/* 1265 * vm_map_madvise: 1266 * 1267 * This routine traverses a processes map handling the madvise 1268 * system call. Advisories are classified as either those effecting 1269 * the vm_map_entry structure, or those effecting the underlying 1270 * objects. 1271 */ 1272int 1273vm_map_madvise( 1274 vm_map_t map, 1275 vm_offset_t start, 1276 vm_offset_t end, 1277 int behav) 1278{ 1279 vm_map_entry_t current, entry; 1280 int modify_map = 0; 1281 1282 GIANT_REQUIRED; 1283 1284 /* 1285 * Some madvise calls directly modify the vm_map_entry, in which case 1286 * we need to use an exclusive lock on the map and we need to perform 1287 * various clipping operations. Otherwise we only need a read-lock 1288 * on the map. 1289 */ 1290 switch(behav) { 1291 case MADV_NORMAL: 1292 case MADV_SEQUENTIAL: 1293 case MADV_RANDOM: 1294 case MADV_NOSYNC: 1295 case MADV_AUTOSYNC: 1296 case MADV_NOCORE: 1297 case MADV_CORE: 1298 modify_map = 1; 1299 vm_map_lock(map); 1300 break; 1301 case MADV_WILLNEED: 1302 case MADV_DONTNEED: 1303 case MADV_FREE: 1304 vm_map_lock_read(map); 1305 break; 1306 default: 1307 return (KERN_INVALID_ARGUMENT); 1308 } 1309 1310 /* 1311 * Locate starting entry and clip if necessary. 1312 */ 1313 VM_MAP_RANGE_CHECK(map, start, end); 1314 1315 if (vm_map_lookup_entry(map, start, &entry)) { 1316 if (modify_map) 1317 vm_map_clip_start(map, entry, start); 1318 } else { 1319 entry = entry->next; 1320 } 1321 1322 if (modify_map) { 1323 /* 1324 * madvise behaviors that are implemented in the vm_map_entry. 1325 * 1326 * We clip the vm_map_entry so that behavioral changes are 1327 * limited to the specified address range. 1328 */ 1329 for (current = entry; 1330 (current != &map->header) && (current->start < end); 1331 current = current->next 1332 ) { 1333 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1334 continue; 1335 1336 vm_map_clip_end(map, current, end); 1337 1338 switch (behav) { 1339 case MADV_NORMAL: 1340 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 1341 break; 1342 case MADV_SEQUENTIAL: 1343 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 1344 break; 1345 case MADV_RANDOM: 1346 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 1347 break; 1348 case MADV_NOSYNC: 1349 current->eflags |= MAP_ENTRY_NOSYNC; 1350 break; 1351 case MADV_AUTOSYNC: 1352 current->eflags &= ~MAP_ENTRY_NOSYNC; 1353 break; 1354 case MADV_NOCORE: 1355 current->eflags |= MAP_ENTRY_NOCOREDUMP; 1356 break; 1357 case MADV_CORE: 1358 current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 1359 break; 1360 default: 1361 break; 1362 } 1363 vm_map_simplify_entry(map, current); 1364 } 1365 vm_map_unlock(map); 1366 } else { 1367 vm_pindex_t pindex; 1368 int count; 1369 1370 /* 1371 * madvise behaviors that are implemented in the underlying 1372 * vm_object. 1373 * 1374 * Since we don't clip the vm_map_entry, we have to clip 1375 * the vm_object pindex and count. 1376 */ 1377 for (current = entry; 1378 (current != &map->header) && (current->start < end); 1379 current = current->next 1380 ) { 1381 vm_offset_t useStart; 1382 1383 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1384 continue; 1385 1386 pindex = OFF_TO_IDX(current->offset); 1387 count = atop(current->end - current->start); 1388 useStart = current->start; 1389 1390 if (current->start < start) { 1391 pindex += atop(start - current->start); 1392 count -= atop(start - current->start); 1393 useStart = start; 1394 } 1395 if (current->end > end) 1396 count -= atop(current->end - end); 1397 1398 if (count <= 0) 1399 continue; 1400 1401 vm_object_madvise(current->object.vm_object, 1402 pindex, count, behav); 1403 if (behav == MADV_WILLNEED) { 1404 pmap_object_init_pt( 1405 map->pmap, 1406 useStart, 1407 current->object.vm_object, 1408 pindex, 1409 (count << PAGE_SHIFT), 1410 MAP_PREFAULT_MADVISE 1411 ); 1412 } 1413 } 1414 vm_map_unlock_read(map); 1415 } 1416 return (0); 1417} 1418 1419 1420/* 1421 * vm_map_inherit: 1422 * 1423 * Sets the inheritance of the specified address 1424 * range in the target map. Inheritance 1425 * affects how the map will be shared with 1426 * child maps at the time of vm_map_fork. 1427 */ 1428int 1429vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 1430 vm_inherit_t new_inheritance) 1431{ 1432 vm_map_entry_t entry; 1433 vm_map_entry_t temp_entry; 1434 1435 GIANT_REQUIRED; 1436 1437 switch (new_inheritance) { 1438 case VM_INHERIT_NONE: 1439 case VM_INHERIT_COPY: 1440 case VM_INHERIT_SHARE: 1441 break; 1442 default: 1443 return (KERN_INVALID_ARGUMENT); 1444 } 1445 1446 vm_map_lock(map); 1447 1448 VM_MAP_RANGE_CHECK(map, start, end); 1449 1450 if (vm_map_lookup_entry(map, start, &temp_entry)) { 1451 entry = temp_entry; 1452 vm_map_clip_start(map, entry, start); 1453 } else 1454 entry = temp_entry->next; 1455 1456 while ((entry != &map->header) && (entry->start < end)) { 1457 vm_map_clip_end(map, entry, end); 1458 1459 entry->inheritance = new_inheritance; 1460 1461 vm_map_simplify_entry(map, entry); 1462 1463 entry = entry->next; 1464 } 1465 1466 vm_map_unlock(map); 1467 return (KERN_SUCCESS); 1468} 1469 1470/* 1471 * Implement the semantics of mlock 1472 */ 1473int 1474vm_map_user_pageable( 1475 vm_map_t map, 1476 vm_offset_t start, 1477 vm_offset_t end, 1478 boolean_t new_pageable) 1479{ 1480 vm_map_entry_t entry; 1481 vm_map_entry_t start_entry; 1482 vm_offset_t estart; 1483 vm_offset_t eend; 1484 int rv; 1485 1486 vm_map_lock(map); 1487 VM_MAP_RANGE_CHECK(map, start, end); 1488 1489 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 1490 vm_map_unlock(map); 1491 return (KERN_INVALID_ADDRESS); 1492 } 1493 1494 if (new_pageable) { 1495 1496 entry = start_entry; 1497 vm_map_clip_start(map, entry, start); 1498 1499 /* 1500 * Now decrement the wiring count for each region. If a region 1501 * becomes completely unwired, unwire its physical pages and 1502 * mappings. 1503 */ 1504 while ((entry != &map->header) && (entry->start < end)) { 1505 if (entry->eflags & MAP_ENTRY_USER_WIRED) { 1506 vm_map_clip_end(map, entry, end); 1507 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 1508 entry->wired_count--; 1509 if (entry->wired_count == 0) 1510 vm_fault_unwire(map, entry->start, entry->end); 1511 } 1512 vm_map_simplify_entry(map,entry); 1513 entry = entry->next; 1514 } 1515 } else { 1516 1517 entry = start_entry; 1518 1519 while ((entry != &map->header) && (entry->start < end)) { 1520 1521 if (entry->eflags & MAP_ENTRY_USER_WIRED) { 1522 entry = entry->next; 1523 continue; 1524 } 1525 1526 if (entry->wired_count != 0) { 1527 entry->wired_count++; 1528 entry->eflags |= MAP_ENTRY_USER_WIRED; 1529 entry = entry->next; 1530 continue; 1531 } 1532 1533 /* Here on entry being newly wired */ 1534 1535 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1536 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY; 1537 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) { 1538 1539 vm_object_shadow(&entry->object.vm_object, 1540 &entry->offset, 1541 atop(entry->end - entry->start)); 1542 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 1543 1544 } else if (entry->object.vm_object == NULL && 1545 !map->system_map) { 1546 1547 entry->object.vm_object = 1548 vm_object_allocate(OBJT_DEFAULT, 1549 atop(entry->end - entry->start)); 1550 entry->offset = (vm_offset_t) 0; 1551 1552 } 1553 } 1554 1555 vm_map_clip_start(map, entry, start); 1556 vm_map_clip_end(map, entry, end); 1557 1558 entry->wired_count++; 1559 entry->eflags |= MAP_ENTRY_USER_WIRED; 1560 estart = entry->start; 1561 eend = entry->end; 1562 1563 /* First we need to allow map modifications */ 1564 vm_map_set_recursive(map); 1565 vm_map_lock_downgrade(map); 1566 map->timestamp++; 1567 1568 rv = vm_fault_user_wire(map, entry->start, entry->end); 1569 if (rv) { 1570 1571 entry->wired_count--; 1572 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 1573 1574 vm_map_clear_recursive(map); 1575 vm_map_unlock(map); 1576 1577 /* 1578 * At this point, the map is unlocked, and 1579 * entry might no longer be valid. Use copy 1580 * of entry start value obtained while entry 1581 * was valid. 1582 */ 1583 (void) vm_map_user_pageable(map, start, estart, 1584 TRUE); 1585 return rv; 1586 } 1587 1588 vm_map_clear_recursive(map); 1589 if (vm_map_lock_upgrade(map)) { 1590 vm_map_lock(map); 1591 if (vm_map_lookup_entry(map, estart, &entry) 1592 == FALSE) { 1593 vm_map_unlock(map); 1594 /* 1595 * vm_fault_user_wire succeded, thus 1596 * the area between start and eend 1597 * is wired and has to be unwired 1598 * here as part of the cleanup. 1599 */ 1600 (void) vm_map_user_pageable(map, 1601 start, 1602 eend, 1603 TRUE); 1604 return (KERN_INVALID_ADDRESS); 1605 } 1606 } 1607 vm_map_simplify_entry(map,entry); 1608 } 1609 } 1610 map->timestamp++; 1611 vm_map_unlock(map); 1612 return KERN_SUCCESS; 1613} 1614 1615/* 1616 * vm_map_pageable: 1617 * 1618 * Sets the pageability of the specified address 1619 * range in the target map. Regions specified 1620 * as not pageable require locked-down physical 1621 * memory and physical page maps. 1622 * 1623 * The map must not be locked, but a reference 1624 * must remain to the map throughout the call. 1625 */ 1626int 1627vm_map_pageable( 1628 vm_map_t map, 1629 vm_offset_t start, 1630 vm_offset_t end, 1631 boolean_t new_pageable) 1632{ 1633 vm_map_entry_t entry; 1634 vm_map_entry_t start_entry; 1635 vm_offset_t failed = 0; 1636 int rv; 1637 1638 GIANT_REQUIRED; 1639 1640 vm_map_lock(map); 1641 1642 VM_MAP_RANGE_CHECK(map, start, end); 1643 1644 /* 1645 * Only one pageability change may take place at one time, since 1646 * vm_fault assumes it will be called only once for each 1647 * wiring/unwiring. Therefore, we have to make sure we're actually 1648 * changing the pageability for the entire region. We do so before 1649 * making any changes. 1650 */ 1651 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 1652 vm_map_unlock(map); 1653 return (KERN_INVALID_ADDRESS); 1654 } 1655 entry = start_entry; 1656 1657 /* 1658 * Actions are rather different for wiring and unwiring, so we have 1659 * two separate cases. 1660 */ 1661 if (new_pageable) { 1662 vm_map_clip_start(map, entry, start); 1663 1664 /* 1665 * Unwiring. First ensure that the range to be unwired is 1666 * really wired down and that there are no holes. 1667 */ 1668 while ((entry != &map->header) && (entry->start < end)) { 1669 if (entry->wired_count == 0 || 1670 (entry->end < end && 1671 (entry->next == &map->header || 1672 entry->next->start > entry->end))) { 1673 vm_map_unlock(map); 1674 return (KERN_INVALID_ARGUMENT); 1675 } 1676 entry = entry->next; 1677 } 1678 1679 /* 1680 * Now decrement the wiring count for each region. If a region 1681 * becomes completely unwired, unwire its physical pages and 1682 * mappings. 1683 */ 1684 entry = start_entry; 1685 while ((entry != &map->header) && (entry->start < end)) { 1686 vm_map_clip_end(map, entry, end); 1687 1688 entry->wired_count--; 1689 if (entry->wired_count == 0) 1690 vm_fault_unwire(map, entry->start, entry->end); 1691 1692 vm_map_simplify_entry(map, entry); 1693 1694 entry = entry->next; 1695 } 1696 } else { 1697 /* 1698 * Wiring. We must do this in two passes: 1699 * 1700 * 1. Holding the write lock, we create any shadow or zero-fill 1701 * objects that need to be created. Then we clip each map 1702 * entry to the region to be wired and increment its wiring 1703 * count. We create objects before clipping the map entries 1704 * to avoid object proliferation. 1705 * 1706 * 2. We downgrade to a read lock, and call vm_fault_wire to 1707 * fault in the pages for any newly wired area (wired_count is 1708 * 1). 1709 * 1710 * Downgrading to a read lock for vm_fault_wire avoids a possible 1711 * deadlock with another process that may have faulted on one 1712 * of the pages to be wired (it would mark the page busy, 1713 * blocking us, then in turn block on the map lock that we 1714 * hold). Because of problems in the recursive lock package, 1715 * we cannot upgrade to a write lock in vm_map_lookup. Thus, 1716 * any actions that require the write lock must be done 1717 * beforehand. Because we keep the read lock on the map, the 1718 * copy-on-write status of the entries we modify here cannot 1719 * change. 1720 */ 1721 1722 /* 1723 * Pass 1. 1724 */ 1725 while ((entry != &map->header) && (entry->start < end)) { 1726 if (entry->wired_count == 0) { 1727 1728 /* 1729 * Perform actions of vm_map_lookup that need 1730 * the write lock on the map: create a shadow 1731 * object for a copy-on-write region, or an 1732 * object for a zero-fill region. 1733 * 1734 * We don't have to do this for entries that 1735 * point to sub maps, because we won't 1736 * hold the lock on the sub map. 1737 */ 1738 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1739 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY; 1740 if (copyflag && 1741 ((entry->protection & VM_PROT_WRITE) != 0)) { 1742 1743 vm_object_shadow(&entry->object.vm_object, 1744 &entry->offset, 1745 atop(entry->end - entry->start)); 1746 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 1747 } else if (entry->object.vm_object == NULL && 1748 !map->system_map) { 1749 entry->object.vm_object = 1750 vm_object_allocate(OBJT_DEFAULT, 1751 atop(entry->end - entry->start)); 1752 entry->offset = (vm_offset_t) 0; 1753 } 1754 } 1755 } 1756 vm_map_clip_start(map, entry, start); 1757 vm_map_clip_end(map, entry, end); 1758 entry->wired_count++; 1759 1760 /* 1761 * Check for holes 1762 */ 1763 if (entry->end < end && 1764 (entry->next == &map->header || 1765 entry->next->start > entry->end)) { 1766 /* 1767 * Found one. Object creation actions do not 1768 * need to be undone, but the wired counts 1769 * need to be restored. 1770 */ 1771 while (entry != &map->header && entry->end > start) { 1772 entry->wired_count--; 1773 entry = entry->prev; 1774 } 1775 vm_map_unlock(map); 1776 return (KERN_INVALID_ARGUMENT); 1777 } 1778 entry = entry->next; 1779 } 1780 1781 /* 1782 * Pass 2. 1783 */ 1784 1785 /* 1786 * HACK HACK HACK HACK 1787 * 1788 * If we are wiring in the kernel map or a submap of it, 1789 * unlock the map to avoid deadlocks. We trust that the 1790 * kernel is well-behaved, and therefore will not do 1791 * anything destructive to this region of the map while 1792 * we have it unlocked. We cannot trust user processes 1793 * to do the same. 1794 * 1795 * HACK HACK HACK HACK 1796 */ 1797 if (vm_map_pmap(map) == kernel_pmap) { 1798 vm_map_unlock(map); /* trust me ... */ 1799 } else { 1800 vm_map_lock_downgrade(map); 1801 } 1802 1803 rv = 0; 1804 entry = start_entry; 1805 while (entry != &map->header && entry->start < end) { 1806 /* 1807 * If vm_fault_wire fails for any page we need to undo 1808 * what has been done. We decrement the wiring count 1809 * for those pages which have not yet been wired (now) 1810 * and unwire those that have (later). 1811 * 1812 * XXX this violates the locking protocol on the map, 1813 * needs to be fixed. 1814 */ 1815 if (rv) 1816 entry->wired_count--; 1817 else if (entry->wired_count == 1) { 1818 rv = vm_fault_wire(map, entry->start, entry->end); 1819 if (rv) { 1820 failed = entry->start; 1821 entry->wired_count--; 1822 } 1823 } 1824 entry = entry->next; 1825 } 1826 1827 if (vm_map_pmap(map) == kernel_pmap) { 1828 vm_map_lock(map); 1829 } 1830 if (rv) { 1831 vm_map_unlock(map); 1832 (void) vm_map_pageable(map, start, failed, TRUE); 1833 return (rv); 1834 } 1835 /* 1836 * An exclusive lock on the map is needed in order to call 1837 * vm_map_simplify_entry(). If the current lock on the map 1838 * is only a shared lock, an upgrade is needed. 1839 */ 1840 if (vm_map_pmap(map) != kernel_pmap && 1841 vm_map_lock_upgrade(map)) { 1842 vm_map_lock(map); 1843 if (vm_map_lookup_entry(map, start, &start_entry) == 1844 FALSE) { 1845 vm_map_unlock(map); 1846 return KERN_SUCCESS; 1847 } 1848 } 1849 vm_map_simplify_entry(map, start_entry); 1850 } 1851 1852 vm_map_unlock(map); 1853 1854 return (KERN_SUCCESS); 1855} 1856 1857/* 1858 * vm_map_clean 1859 * 1860 * Push any dirty cached pages in the address range to their pager. 1861 * If syncio is TRUE, dirty pages are written synchronously. 1862 * If invalidate is TRUE, any cached pages are freed as well. 1863 * 1864 * Returns an error if any part of the specified range is not mapped. 1865 */ 1866int 1867vm_map_clean( 1868 vm_map_t map, 1869 vm_offset_t start, 1870 vm_offset_t end, 1871 boolean_t syncio, 1872 boolean_t invalidate) 1873{ 1874 vm_map_entry_t current; 1875 vm_map_entry_t entry; 1876 vm_size_t size; 1877 vm_object_t object; 1878 vm_ooffset_t offset; 1879 1880 GIANT_REQUIRED; 1881 1882 vm_map_lock_read(map); 1883 VM_MAP_RANGE_CHECK(map, start, end); 1884 if (!vm_map_lookup_entry(map, start, &entry)) { 1885 vm_map_unlock_read(map); 1886 return (KERN_INVALID_ADDRESS); 1887 } 1888 /* 1889 * Make a first pass to check for holes. 1890 */ 1891 for (current = entry; current->start < end; current = current->next) { 1892 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1893 vm_map_unlock_read(map); 1894 return (KERN_INVALID_ARGUMENT); 1895 } 1896 if (end > current->end && 1897 (current->next == &map->header || 1898 current->end != current->next->start)) { 1899 vm_map_unlock_read(map); 1900 return (KERN_INVALID_ADDRESS); 1901 } 1902 } 1903 1904 if (invalidate) 1905 pmap_remove(vm_map_pmap(map), start, end); 1906 /* 1907 * Make a second pass, cleaning/uncaching pages from the indicated 1908 * objects as we go. 1909 */ 1910 for (current = entry; current->start < end; current = current->next) { 1911 offset = current->offset + (start - current->start); 1912 size = (end <= current->end ? end : current->end) - start; 1913 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1914 vm_map_t smap; 1915 vm_map_entry_t tentry; 1916 vm_size_t tsize; 1917 1918 smap = current->object.sub_map; 1919 vm_map_lock_read(smap); 1920 (void) vm_map_lookup_entry(smap, offset, &tentry); 1921 tsize = tentry->end - offset; 1922 if (tsize < size) 1923 size = tsize; 1924 object = tentry->object.vm_object; 1925 offset = tentry->offset + (offset - tentry->start); 1926 vm_map_unlock_read(smap); 1927 } else { 1928 object = current->object.vm_object; 1929 } 1930 /* 1931 * Note that there is absolutely no sense in writing out 1932 * anonymous objects, so we track down the vnode object 1933 * to write out. 1934 * We invalidate (remove) all pages from the address space 1935 * anyway, for semantic correctness. 1936 * 1937 * note: certain anonymous maps, such as MAP_NOSYNC maps, 1938 * may start out with a NULL object. 1939 */ 1940 while (object && object->backing_object) { 1941 object = object->backing_object; 1942 offset += object->backing_object_offset; 1943 if (object->size < OFF_TO_IDX(offset + size)) 1944 size = IDX_TO_OFF(object->size) - offset; 1945 } 1946 if (object && (object->type == OBJT_VNODE) && 1947 (current->protection & VM_PROT_WRITE)) { 1948 /* 1949 * Flush pages if writing is allowed, invalidate them 1950 * if invalidation requested. Pages undergoing I/O 1951 * will be ignored by vm_object_page_remove(). 1952 * 1953 * We cannot lock the vnode and then wait for paging 1954 * to complete without deadlocking against vm_fault. 1955 * Instead we simply call vm_object_page_remove() and 1956 * allow it to block internally on a page-by-page 1957 * basis when it encounters pages undergoing async 1958 * I/O. 1959 */ 1960 int flags; 1961 1962 vm_object_reference(object); 1963 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curthread); 1964 flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 1965 flags |= invalidate ? OBJPC_INVAL : 0; 1966 vm_object_page_clean(object, 1967 OFF_TO_IDX(offset), 1968 OFF_TO_IDX(offset + size + PAGE_MASK), 1969 flags); 1970 if (invalidate) { 1971 /*vm_object_pip_wait(object, "objmcl");*/ 1972 vm_object_page_remove(object, 1973 OFF_TO_IDX(offset), 1974 OFF_TO_IDX(offset + size + PAGE_MASK), 1975 FALSE); 1976 } 1977 VOP_UNLOCK(object->handle, 0, curthread); 1978 vm_object_deallocate(object); 1979 } 1980 start += size; 1981 } 1982 1983 vm_map_unlock_read(map); 1984 return (KERN_SUCCESS); 1985} 1986 1987/* 1988 * vm_map_entry_unwire: [ internal use only ] 1989 * 1990 * Make the region specified by this entry pageable. 1991 * 1992 * The map in question should be locked. 1993 * [This is the reason for this routine's existence.] 1994 */ 1995static void 1996vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry) 1997{ 1998 vm_fault_unwire(map, entry->start, entry->end); 1999 entry->wired_count = 0; 2000} 2001 2002/* 2003 * vm_map_entry_delete: [ internal use only ] 2004 * 2005 * Deallocate the given entry from the target map. 2006 */ 2007static void 2008vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry) 2009{ 2010 vm_map_entry_unlink(map, entry); 2011 map->size -= entry->end - entry->start; 2012 2013 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 2014 vm_object_deallocate(entry->object.vm_object); 2015 } 2016 2017 vm_map_entry_dispose(map, entry); 2018} 2019 2020/* 2021 * vm_map_delete: [ internal use only ] 2022 * 2023 * Deallocates the given address range from the target 2024 * map. 2025 */ 2026int 2027vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end) 2028{ 2029 vm_object_t object; 2030 vm_map_entry_t entry; 2031 vm_map_entry_t first_entry; 2032 2033 GIANT_REQUIRED; 2034 2035 /* 2036 * Find the start of the region, and clip it 2037 */ 2038 if (!vm_map_lookup_entry(map, start, &first_entry)) 2039 entry = first_entry->next; 2040 else { 2041 entry = first_entry; 2042 vm_map_clip_start(map, entry, start); 2043 /* 2044 * Fix the lookup hint now, rather than each time though the 2045 * loop. 2046 */ 2047 SAVE_HINT(map, entry->prev); 2048 } 2049 2050 /* 2051 * Save the free space hint 2052 */ 2053 if (entry == &map->header) { 2054 map->first_free = &map->header; 2055 } else if (map->first_free->start >= start) { 2056 map->first_free = entry->prev; 2057 } 2058 2059 /* 2060 * Step through all entries in this region 2061 */ 2062 while ((entry != &map->header) && (entry->start < end)) { 2063 vm_map_entry_t next; 2064 vm_offset_t s, e; 2065 vm_pindex_t offidxstart, offidxend, count; 2066 2067 vm_map_clip_end(map, entry, end); 2068 2069 s = entry->start; 2070 e = entry->end; 2071 next = entry->next; 2072 2073 offidxstart = OFF_TO_IDX(entry->offset); 2074 count = OFF_TO_IDX(e - s); 2075 object = entry->object.vm_object; 2076 2077 /* 2078 * Unwire before removing addresses from the pmap; otherwise, 2079 * unwiring will put the entries back in the pmap. 2080 */ 2081 if (entry->wired_count != 0) { 2082 vm_map_entry_unwire(map, entry); 2083 } 2084 2085 offidxend = offidxstart + count; 2086 2087 if ((object == kernel_object) || (object == kmem_object)) { 2088 vm_object_page_remove(object, offidxstart, offidxend, FALSE); 2089 } else { 2090 pmap_remove(map->pmap, s, e); 2091 if (object != NULL && 2092 object->ref_count != 1 && 2093 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING && 2094 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 2095 vm_object_collapse(object); 2096 vm_object_page_remove(object, offidxstart, offidxend, FALSE); 2097 if (object->type == OBJT_SWAP) { 2098 swap_pager_freespace(object, offidxstart, count); 2099 } 2100 if (offidxend >= object->size && 2101 offidxstart < object->size) { 2102 object->size = offidxstart; 2103 } 2104 } 2105 } 2106 2107 /* 2108 * Delete the entry (which may delete the object) only after 2109 * removing all pmap entries pointing to its pages. 2110 * (Otherwise, its page frames may be reallocated, and any 2111 * modify bits will be set in the wrong object!) 2112 */ 2113 vm_map_entry_delete(map, entry); 2114 entry = next; 2115 } 2116 return (KERN_SUCCESS); 2117} 2118 2119/* 2120 * vm_map_remove: 2121 * 2122 * Remove the given address range from the target map. 2123 * This is the exported form of vm_map_delete. 2124 */ 2125int 2126vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end) 2127{ 2128 int result, s = 0; 2129 2130 GIANT_REQUIRED; 2131 2132 if (map == kmem_map) 2133 s = splvm(); 2134 2135 vm_map_lock(map); 2136 VM_MAP_RANGE_CHECK(map, start, end); 2137 result = vm_map_delete(map, start, end); 2138 vm_map_unlock(map); 2139 2140 if (map == kmem_map) 2141 splx(s); 2142 2143 return (result); 2144} 2145 2146/* 2147 * vm_map_check_protection: 2148 * 2149 * Assert that the target map allows the specified 2150 * privilege on the entire address region given. 2151 * The entire region must be allocated. 2152 */ 2153boolean_t 2154vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 2155 vm_prot_t protection) 2156{ 2157 vm_map_entry_t entry; 2158 vm_map_entry_t tmp_entry; 2159 2160 GIANT_REQUIRED; 2161 2162 vm_map_lock_read(map); 2163 if (!vm_map_lookup_entry(map, start, &tmp_entry)) { 2164 vm_map_unlock_read(map); 2165 return (FALSE); 2166 } 2167 entry = tmp_entry; 2168 2169 while (start < end) { 2170 if (entry == &map->header) { 2171 vm_map_unlock_read(map); 2172 return (FALSE); 2173 } 2174 /* 2175 * No holes allowed! 2176 */ 2177 if (start < entry->start) { 2178 vm_map_unlock_read(map); 2179 return (FALSE); 2180 } 2181 /* 2182 * Check protection associated with entry. 2183 */ 2184 if ((entry->protection & protection) != protection) { 2185 vm_map_unlock_read(map); 2186 return (FALSE); 2187 } 2188 /* go to next entry */ 2189 start = entry->end; 2190 entry = entry->next; 2191 } 2192 vm_map_unlock_read(map); 2193 return (TRUE); 2194} 2195 2196/* 2197 * Split the pages in a map entry into a new object. This affords 2198 * easier removal of unused pages, and keeps object inheritance from 2199 * being a negative impact on memory usage. 2200 */ 2201static void 2202vm_map_split(vm_map_entry_t entry) 2203{ 2204 vm_page_t m; 2205 vm_object_t orig_object, new_object, source; 2206 vm_offset_t s, e; 2207 vm_pindex_t offidxstart, offidxend, idx; 2208 vm_size_t size; 2209 vm_ooffset_t offset; 2210 2211 GIANT_REQUIRED; 2212 2213 orig_object = entry->object.vm_object; 2214 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 2215 return; 2216 if (orig_object->ref_count <= 1) 2217 return; 2218 2219 offset = entry->offset; 2220 s = entry->start; 2221 e = entry->end; 2222 2223 offidxstart = OFF_TO_IDX(offset); 2224 offidxend = offidxstart + OFF_TO_IDX(e - s); 2225 size = offidxend - offidxstart; 2226 2227 new_object = vm_pager_allocate(orig_object->type, 2228 NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL); 2229 if (new_object == NULL) 2230 return; 2231 2232 source = orig_object->backing_object; 2233 if (source != NULL) { 2234 vm_object_reference(source); /* Referenced by new_object */ 2235 TAILQ_INSERT_TAIL(&source->shadow_head, 2236 new_object, shadow_list); 2237 vm_object_clear_flag(source, OBJ_ONEMAPPING); 2238 new_object->backing_object_offset = 2239 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart); 2240 new_object->backing_object = source; 2241 source->shadow_count++; 2242 source->generation++; 2243 } 2244 2245 for (idx = 0; idx < size; idx++) { 2246 vm_page_t m; 2247 2248 retry: 2249 m = vm_page_lookup(orig_object, offidxstart + idx); 2250 if (m == NULL) 2251 continue; 2252 2253 /* 2254 * We must wait for pending I/O to complete before we can 2255 * rename the page. 2256 * 2257 * We do not have to VM_PROT_NONE the page as mappings should 2258 * not be changed by this operation. 2259 */ 2260 if (vm_page_sleep_busy(m, TRUE, "spltwt")) 2261 goto retry; 2262 2263 vm_page_busy(m); 2264 vm_page_rename(m, new_object, idx); 2265 /* page automatically made dirty by rename and cache handled */ 2266 vm_page_busy(m); 2267 } 2268 2269 if (orig_object->type == OBJT_SWAP) { 2270 vm_object_pip_add(orig_object, 1); 2271 /* 2272 * copy orig_object pages into new_object 2273 * and destroy unneeded pages in 2274 * shadow object. 2275 */ 2276 swap_pager_copy(orig_object, new_object, offidxstart, 0); 2277 vm_object_pip_wakeup(orig_object); 2278 } 2279 2280 for (idx = 0; idx < size; idx++) { 2281 m = vm_page_lookup(new_object, idx); 2282 if (m) { 2283 vm_page_wakeup(m); 2284 } 2285 } 2286 2287 entry->object.vm_object = new_object; 2288 entry->offset = 0LL; 2289 vm_object_deallocate(orig_object); 2290} 2291 2292/* 2293 * vm_map_copy_entry: 2294 * 2295 * Copies the contents of the source entry to the destination 2296 * entry. The entries *must* be aligned properly. 2297 */ 2298static void 2299vm_map_copy_entry( 2300 vm_map_t src_map, 2301 vm_map_t dst_map, 2302 vm_map_entry_t src_entry, 2303 vm_map_entry_t dst_entry) 2304{ 2305 vm_object_t src_object; 2306 2307 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 2308 return; 2309 2310 if (src_entry->wired_count == 0) { 2311 2312 /* 2313 * If the source entry is marked needs_copy, it is already 2314 * write-protected. 2315 */ 2316 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 2317 pmap_protect(src_map->pmap, 2318 src_entry->start, 2319 src_entry->end, 2320 src_entry->protection & ~VM_PROT_WRITE); 2321 } 2322 2323 /* 2324 * Make a copy of the object. 2325 */ 2326 if ((src_object = src_entry->object.vm_object) != NULL) { 2327 2328 if ((src_object->handle == NULL) && 2329 (src_object->type == OBJT_DEFAULT || 2330 src_object->type == OBJT_SWAP)) { 2331 vm_object_collapse(src_object); 2332 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 2333 vm_map_split(src_entry); 2334 src_object = src_entry->object.vm_object; 2335 } 2336 } 2337 2338 vm_object_reference(src_object); 2339 vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 2340 dst_entry->object.vm_object = src_object; 2341 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2342 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2343 dst_entry->offset = src_entry->offset; 2344 } else { 2345 dst_entry->object.vm_object = NULL; 2346 dst_entry->offset = 0; 2347 } 2348 2349 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 2350 dst_entry->end - dst_entry->start, src_entry->start); 2351 } else { 2352 /* 2353 * Of course, wired down pages can't be set copy-on-write. 2354 * Cause wired pages to be copied into the new map by 2355 * simulating faults (the new pages are pageable) 2356 */ 2357 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 2358 } 2359} 2360 2361/* 2362 * vmspace_fork: 2363 * Create a new process vmspace structure and vm_map 2364 * based on those of an existing process. The new map 2365 * is based on the old map, according to the inheritance 2366 * values on the regions in that map. 2367 * 2368 * The source map must not be locked. 2369 */ 2370struct vmspace * 2371vmspace_fork(struct vmspace *vm1) 2372{ 2373 struct vmspace *vm2; 2374 vm_map_t old_map = &vm1->vm_map; 2375 vm_map_t new_map; 2376 vm_map_entry_t old_entry; 2377 vm_map_entry_t new_entry; 2378 vm_object_t object; 2379 2380 GIANT_REQUIRED; 2381 2382 vm_map_lock(old_map); 2383 old_map->infork = 1; 2384 2385 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset); 2386 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy, 2387 (caddr_t) &vm1->vm_endcopy - (caddr_t) &vm1->vm_startcopy); 2388 new_map = &vm2->vm_map; /* XXX */ 2389 new_map->timestamp = 1; 2390 2391 old_entry = old_map->header.next; 2392 2393 while (old_entry != &old_map->header) { 2394 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2395 panic("vm_map_fork: encountered a submap"); 2396 2397 switch (old_entry->inheritance) { 2398 case VM_INHERIT_NONE: 2399 break; 2400 2401 case VM_INHERIT_SHARE: 2402 /* 2403 * Clone the entry, creating the shared object if necessary. 2404 */ 2405 object = old_entry->object.vm_object; 2406 if (object == NULL) { 2407 object = vm_object_allocate(OBJT_DEFAULT, 2408 atop(old_entry->end - old_entry->start)); 2409 old_entry->object.vm_object = object; 2410 old_entry->offset = (vm_offset_t) 0; 2411 } 2412 2413 /* 2414 * Add the reference before calling vm_object_shadow 2415 * to insure that a shadow object is created. 2416 */ 2417 vm_object_reference(object); 2418 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2419 vm_object_shadow(&old_entry->object.vm_object, 2420 &old_entry->offset, 2421 atop(old_entry->end - old_entry->start)); 2422 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2423 /* Transfer the second reference too. */ 2424 vm_object_reference( 2425 old_entry->object.vm_object); 2426 vm_object_deallocate(object); 2427 object = old_entry->object.vm_object; 2428 } 2429 vm_object_clear_flag(object, OBJ_ONEMAPPING); 2430 2431 /* 2432 * Clone the entry, referencing the shared object. 2433 */ 2434 new_entry = vm_map_entry_create(new_map); 2435 *new_entry = *old_entry; 2436 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2437 new_entry->wired_count = 0; 2438 2439 /* 2440 * Insert the entry into the new map -- we know we're 2441 * inserting at the end of the new map. 2442 */ 2443 vm_map_entry_link(new_map, new_map->header.prev, 2444 new_entry); 2445 2446 /* 2447 * Update the physical map 2448 */ 2449 pmap_copy(new_map->pmap, old_map->pmap, 2450 new_entry->start, 2451 (old_entry->end - old_entry->start), 2452 old_entry->start); 2453 break; 2454 2455 case VM_INHERIT_COPY: 2456 /* 2457 * Clone the entry and link into the map. 2458 */ 2459 new_entry = vm_map_entry_create(new_map); 2460 *new_entry = *old_entry; 2461 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2462 new_entry->wired_count = 0; 2463 new_entry->object.vm_object = NULL; 2464 vm_map_entry_link(new_map, new_map->header.prev, 2465 new_entry); 2466 vm_map_copy_entry(old_map, new_map, old_entry, 2467 new_entry); 2468 break; 2469 } 2470 old_entry = old_entry->next; 2471 } 2472 2473 new_map->size = old_map->size; 2474 old_map->infork = 0; 2475 vm_map_unlock(old_map); 2476 2477 return (vm2); 2478} 2479 2480int 2481vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 2482 vm_prot_t prot, vm_prot_t max, int cow) 2483{ 2484 vm_map_entry_t prev_entry; 2485 vm_map_entry_t new_stack_entry; 2486 vm_size_t init_ssize; 2487 int rv; 2488 2489 GIANT_REQUIRED; 2490 2491 if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS) 2492 return (KERN_NO_SPACE); 2493 2494 if (max_ssize < sgrowsiz) 2495 init_ssize = max_ssize; 2496 else 2497 init_ssize = sgrowsiz; 2498 2499 vm_map_lock(map); 2500 2501 /* If addr is already mapped, no go */ 2502 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) { 2503 vm_map_unlock(map); 2504 return (KERN_NO_SPACE); 2505 } 2506 2507 /* If we can't accomodate max_ssize in the current mapping, 2508 * no go. However, we need to be aware that subsequent user 2509 * mappings might map into the space we have reserved for 2510 * stack, and currently this space is not protected. 2511 * 2512 * Hopefully we will at least detect this condition 2513 * when we try to grow the stack. 2514 */ 2515 if ((prev_entry->next != &map->header) && 2516 (prev_entry->next->start < addrbos + max_ssize)) { 2517 vm_map_unlock(map); 2518 return (KERN_NO_SPACE); 2519 } 2520 2521 /* We initially map a stack of only init_ssize. We will 2522 * grow as needed later. Since this is to be a grow 2523 * down stack, we map at the top of the range. 2524 * 2525 * Note: we would normally expect prot and max to be 2526 * VM_PROT_ALL, and cow to be 0. Possibly we should 2527 * eliminate these as input parameters, and just 2528 * pass these values here in the insert call. 2529 */ 2530 rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize, 2531 addrbos + max_ssize, prot, max, cow); 2532 2533 /* Now set the avail_ssize amount */ 2534 if (rv == KERN_SUCCESS){ 2535 if (prev_entry != &map->header) 2536 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize); 2537 new_stack_entry = prev_entry->next; 2538 if (new_stack_entry->end != addrbos + max_ssize || 2539 new_stack_entry->start != addrbos + max_ssize - init_ssize) 2540 panic ("Bad entry start/end for new stack entry"); 2541 else 2542 new_stack_entry->avail_ssize = max_ssize - init_ssize; 2543 } 2544 2545 vm_map_unlock(map); 2546 return (rv); 2547} 2548 2549/* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the 2550 * desired address is already mapped, or if we successfully grow 2551 * the stack. Also returns KERN_SUCCESS if addr is outside the 2552 * stack range (this is strange, but preserves compatibility with 2553 * the grow function in vm_machdep.c). 2554 */ 2555int 2556vm_map_growstack (struct proc *p, vm_offset_t addr) 2557{ 2558 vm_map_entry_t prev_entry; 2559 vm_map_entry_t stack_entry; 2560 vm_map_entry_t new_stack_entry; 2561 struct vmspace *vm = p->p_vmspace; 2562 vm_map_t map = &vm->vm_map; 2563 vm_offset_t end; 2564 int grow_amount; 2565 int rv; 2566 int is_procstack; 2567 2568 GIANT_REQUIRED; 2569 2570Retry: 2571 vm_map_lock_read(map); 2572 2573 /* If addr is already in the entry range, no need to grow.*/ 2574 if (vm_map_lookup_entry(map, addr, &prev_entry)) { 2575 vm_map_unlock_read(map); 2576 return (KERN_SUCCESS); 2577 } 2578 2579 if ((stack_entry = prev_entry->next) == &map->header) { 2580 vm_map_unlock_read(map); 2581 return (KERN_SUCCESS); 2582 } 2583 if (prev_entry == &map->header) 2584 end = stack_entry->start - stack_entry->avail_ssize; 2585 else 2586 end = prev_entry->end; 2587 2588 /* This next test mimics the old grow function in vm_machdep.c. 2589 * It really doesn't quite make sense, but we do it anyway 2590 * for compatibility. 2591 * 2592 * If not growable stack, return success. This signals the 2593 * caller to proceed as he would normally with normal vm. 2594 */ 2595 if (stack_entry->avail_ssize < 1 || 2596 addr >= stack_entry->start || 2597 addr < stack_entry->start - stack_entry->avail_ssize) { 2598 vm_map_unlock_read(map); 2599 return (KERN_SUCCESS); 2600 } 2601 2602 /* Find the minimum grow amount */ 2603 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE); 2604 if (grow_amount > stack_entry->avail_ssize) { 2605 vm_map_unlock_read(map); 2606 return (KERN_NO_SPACE); 2607 } 2608 2609 /* If there is no longer enough space between the entries 2610 * nogo, and adjust the available space. Note: this 2611 * should only happen if the user has mapped into the 2612 * stack area after the stack was created, and is 2613 * probably an error. 2614 * 2615 * This also effectively destroys any guard page the user 2616 * might have intended by limiting the stack size. 2617 */ 2618 if (grow_amount > stack_entry->start - end) { 2619 if (vm_map_lock_upgrade(map)) 2620 goto Retry; 2621 2622 stack_entry->avail_ssize = stack_entry->start - end; 2623 2624 vm_map_unlock(map); 2625 return (KERN_NO_SPACE); 2626 } 2627 2628 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr; 2629 2630 /* If this is the main process stack, see if we're over the 2631 * stack limit. 2632 */ 2633 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > 2634 p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 2635 vm_map_unlock_read(map); 2636 return (KERN_NO_SPACE); 2637 } 2638 2639 /* Round up the grow amount modulo SGROWSIZ */ 2640 grow_amount = roundup (grow_amount, sgrowsiz); 2641 if (grow_amount > stack_entry->avail_ssize) { 2642 grow_amount = stack_entry->avail_ssize; 2643 } 2644 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > 2645 p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 2646 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur - 2647 ctob(vm->vm_ssize); 2648 } 2649 2650 if (vm_map_lock_upgrade(map)) 2651 goto Retry; 2652 2653 /* Get the preliminary new entry start value */ 2654 addr = stack_entry->start - grow_amount; 2655 2656 /* If this puts us into the previous entry, cut back our growth 2657 * to the available space. Also, see the note above. 2658 */ 2659 if (addr < end) { 2660 stack_entry->avail_ssize = stack_entry->start - end; 2661 addr = end; 2662 } 2663 2664 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start, 2665 VM_PROT_ALL, 2666 VM_PROT_ALL, 2667 0); 2668 2669 /* Adjust the available stack space by the amount we grew. */ 2670 if (rv == KERN_SUCCESS) { 2671 if (prev_entry != &map->header) 2672 vm_map_clip_end(map, prev_entry, addr); 2673 new_stack_entry = prev_entry->next; 2674 if (new_stack_entry->end != stack_entry->start || 2675 new_stack_entry->start != addr) 2676 panic ("Bad stack grow start/end in new stack entry"); 2677 else { 2678 new_stack_entry->avail_ssize = stack_entry->avail_ssize - 2679 (new_stack_entry->end - 2680 new_stack_entry->start); 2681 if (is_procstack) 2682 vm->vm_ssize += btoc(new_stack_entry->end - 2683 new_stack_entry->start); 2684 } 2685 } 2686 2687 vm_map_unlock(map); 2688 return (rv); 2689} 2690 2691/* 2692 * Unshare the specified VM space for exec. If other processes are 2693 * mapped to it, then create a new one. The new vmspace is null. 2694 */ 2695void 2696vmspace_exec(struct proc *p) 2697{ 2698 struct vmspace *oldvmspace = p->p_vmspace; 2699 struct vmspace *newvmspace; 2700 vm_map_t map = &p->p_vmspace->vm_map; 2701 2702 GIANT_REQUIRED; 2703 newvmspace = vmspace_alloc(map->min_offset, map->max_offset); 2704 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy, 2705 (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy); 2706 /* 2707 * This code is written like this for prototype purposes. The 2708 * goal is to avoid running down the vmspace here, but let the 2709 * other process's that are still using the vmspace to finally 2710 * run it down. Even though there is little or no chance of blocking 2711 * here, it is a good idea to keep this form for future mods. 2712 */ 2713 p->p_vmspace = newvmspace; 2714 pmap_pinit2(vmspace_pmap(newvmspace)); 2715 vmspace_free(oldvmspace); 2716 if (p == curthread->td_proc) /* XXXKSE ? */ 2717 pmap_activate(curthread); 2718} 2719 2720/* 2721 * Unshare the specified VM space for forcing COW. This 2722 * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 2723 */ 2724void 2725vmspace_unshare(struct proc *p) 2726{ 2727 struct vmspace *oldvmspace = p->p_vmspace; 2728 struct vmspace *newvmspace; 2729 2730 GIANT_REQUIRED; 2731 if (oldvmspace->vm_refcnt == 1) 2732 return; 2733 newvmspace = vmspace_fork(oldvmspace); 2734 p->p_vmspace = newvmspace; 2735 pmap_pinit2(vmspace_pmap(newvmspace)); 2736 vmspace_free(oldvmspace); 2737 if (p == curthread->td_proc) /* XXXKSE ? */ 2738 pmap_activate(curthread); 2739} 2740 2741/* 2742 * vm_map_lookup: 2743 * 2744 * Finds the VM object, offset, and 2745 * protection for a given virtual address in the 2746 * specified map, assuming a page fault of the 2747 * type specified. 2748 * 2749 * Leaves the map in question locked for read; return 2750 * values are guaranteed until a vm_map_lookup_done 2751 * call is performed. Note that the map argument 2752 * is in/out; the returned map must be used in 2753 * the call to vm_map_lookup_done. 2754 * 2755 * A handle (out_entry) is returned for use in 2756 * vm_map_lookup_done, to make that fast. 2757 * 2758 * If a lookup is requested with "write protection" 2759 * specified, the map may be changed to perform virtual 2760 * copying operations, although the data referenced will 2761 * remain the same. 2762 */ 2763int 2764vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 2765 vm_offset_t vaddr, 2766 vm_prot_t fault_typea, 2767 vm_map_entry_t *out_entry, /* OUT */ 2768 vm_object_t *object, /* OUT */ 2769 vm_pindex_t *pindex, /* OUT */ 2770 vm_prot_t *out_prot, /* OUT */ 2771 boolean_t *wired) /* OUT */ 2772{ 2773 vm_map_entry_t entry; 2774 vm_map_t map = *var_map; 2775 vm_prot_t prot; 2776 vm_prot_t fault_type = fault_typea; 2777 2778 GIANT_REQUIRED; 2779RetryLookup:; 2780 /* 2781 * Lookup the faulting address. 2782 */ 2783 2784 vm_map_lock_read(map); 2785#define RETURN(why) \ 2786 { \ 2787 vm_map_unlock_read(map); \ 2788 return (why); \ 2789 } 2790 2791 /* 2792 * If the map has an interesting hint, try it before calling full 2793 * blown lookup routine. 2794 */ 2795 entry = map->hint; 2796 *out_entry = entry; 2797 if ((entry == &map->header) || 2798 (vaddr < entry->start) || (vaddr >= entry->end)) { 2799 vm_map_entry_t tmp_entry; 2800 2801 /* 2802 * Entry was either not a valid hint, or the vaddr was not 2803 * contained in the entry, so do a full lookup. 2804 */ 2805 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) 2806 RETURN(KERN_INVALID_ADDRESS); 2807 2808 entry = tmp_entry; 2809 *out_entry = entry; 2810 } 2811 2812 /* 2813 * Handle submaps. 2814 */ 2815 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 2816 vm_map_t old_map = map; 2817 2818 *var_map = map = entry->object.sub_map; 2819 vm_map_unlock_read(old_map); 2820 goto RetryLookup; 2821 } 2822 2823 /* 2824 * Check whether this task is allowed to have this page. 2825 * Note the special case for MAP_ENTRY_COW 2826 * pages with an override. This is to implement a forced 2827 * COW for debuggers. 2828 */ 2829 if (fault_type & VM_PROT_OVERRIDE_WRITE) 2830 prot = entry->max_protection; 2831 else 2832 prot = entry->protection; 2833 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 2834 if ((fault_type & prot) != fault_type) { 2835 RETURN(KERN_PROTECTION_FAILURE); 2836 } 2837 if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 2838 (entry->eflags & MAP_ENTRY_COW) && 2839 (fault_type & VM_PROT_WRITE) && 2840 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) { 2841 RETURN(KERN_PROTECTION_FAILURE); 2842 } 2843 2844 /* 2845 * If this page is not pageable, we have to get it for all possible 2846 * accesses. 2847 */ 2848 *wired = (entry->wired_count != 0); 2849 if (*wired) 2850 prot = fault_type = entry->protection; 2851 2852 /* 2853 * If the entry was copy-on-write, we either ... 2854 */ 2855 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2856 /* 2857 * If we want to write the page, we may as well handle that 2858 * now since we've got the map locked. 2859 * 2860 * If we don't need to write the page, we just demote the 2861 * permissions allowed. 2862 */ 2863 if (fault_type & VM_PROT_WRITE) { 2864 /* 2865 * Make a new object, and place it in the object 2866 * chain. Note that no new references have appeared 2867 * -- one just moved from the map to the new 2868 * object. 2869 */ 2870 if (vm_map_lock_upgrade(map)) 2871 goto RetryLookup; 2872 vm_object_shadow( 2873 &entry->object.vm_object, 2874 &entry->offset, 2875 atop(entry->end - entry->start)); 2876 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2877 vm_map_lock_downgrade(map); 2878 } else { 2879 /* 2880 * We're attempting to read a copy-on-write page -- 2881 * don't allow writes. 2882 */ 2883 prot &= ~VM_PROT_WRITE; 2884 } 2885 } 2886 2887 /* 2888 * Create an object if necessary. 2889 */ 2890 if (entry->object.vm_object == NULL && 2891 !map->system_map) { 2892 if (vm_map_lock_upgrade(map)) 2893 goto RetryLookup; 2894 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 2895 atop(entry->end - entry->start)); 2896 entry->offset = 0; 2897 vm_map_lock_downgrade(map); 2898 } 2899 2900 /* 2901 * Return the object/offset from this entry. If the entry was 2902 * copy-on-write or empty, it has been fixed up. 2903 */ 2904 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 2905 *object = entry->object.vm_object; 2906 2907 /* 2908 * Return whether this is the only map sharing this data. 2909 */ 2910 *out_prot = prot; 2911 return (KERN_SUCCESS); 2912 2913#undef RETURN 2914} 2915 2916/* 2917 * vm_map_lookup_done: 2918 * 2919 * Releases locks acquired by a vm_map_lookup 2920 * (according to the handle returned by that lookup). 2921 */ 2922void 2923vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry) 2924{ 2925 /* 2926 * Unlock the main-level map 2927 */ 2928 GIANT_REQUIRED; 2929 vm_map_unlock_read(map); 2930} 2931 2932/* 2933 * Implement uiomove with VM operations. This handles (and collateral changes) 2934 * support every combination of source object modification, and COW type 2935 * operations. 2936 */ 2937int 2938vm_uiomove( 2939 vm_map_t mapa, 2940 vm_object_t srcobject, 2941 off_t cp, 2942 int cnta, 2943 vm_offset_t uaddra, 2944 int *npages) 2945{ 2946 vm_map_t map; 2947 vm_object_t first_object, oldobject, object; 2948 vm_map_entry_t entry; 2949 vm_prot_t prot; 2950 boolean_t wired; 2951 int tcnt, rv; 2952 vm_offset_t uaddr, start, end, tend; 2953 vm_pindex_t first_pindex, osize, oindex; 2954 off_t ooffset; 2955 int cnt; 2956 2957 GIANT_REQUIRED; 2958 2959 if (npages) 2960 *npages = 0; 2961 2962 cnt = cnta; 2963 uaddr = uaddra; 2964 2965 while (cnt > 0) { 2966 map = mapa; 2967 2968 if ((vm_map_lookup(&map, uaddr, 2969 VM_PROT_READ, &entry, &first_object, 2970 &first_pindex, &prot, &wired)) != KERN_SUCCESS) { 2971 return EFAULT; 2972 } 2973 2974 vm_map_clip_start(map, entry, uaddr); 2975 2976 tcnt = cnt; 2977 tend = uaddr + tcnt; 2978 if (tend > entry->end) { 2979 tcnt = entry->end - uaddr; 2980 tend = entry->end; 2981 } 2982 2983 vm_map_clip_end(map, entry, tend); 2984 2985 start = entry->start; 2986 end = entry->end; 2987 2988 osize = atop(tcnt); 2989 2990 oindex = OFF_TO_IDX(cp); 2991 if (npages) { 2992 vm_pindex_t idx; 2993 for (idx = 0; idx < osize; idx++) { 2994 vm_page_t m; 2995 if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) { 2996 vm_map_lookup_done(map, entry); 2997 return 0; 2998 } 2999 /* 3000 * disallow busy or invalid pages, but allow 3001 * m->busy pages if they are entirely valid. 3002 */ 3003 if ((m->flags & PG_BUSY) || 3004 ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) { 3005 vm_map_lookup_done(map, entry); 3006 return 0; 3007 } 3008 } 3009 } 3010 3011/* 3012 * If we are changing an existing map entry, just redirect 3013 * the object, and change mappings. 3014 */ 3015 if ((first_object->type == OBJT_VNODE) && 3016 ((oldobject = entry->object.vm_object) == first_object)) { 3017 3018 if ((entry->offset != cp) || (oldobject != srcobject)) { 3019 /* 3020 * Remove old window into the file 3021 */ 3022 pmap_remove (map->pmap, uaddr, tend); 3023 3024 /* 3025 * Force copy on write for mmaped regions 3026 */ 3027 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 3028 3029 /* 3030 * Point the object appropriately 3031 */ 3032 if (oldobject != srcobject) { 3033 3034 /* 3035 * Set the object optimization hint flag 3036 */ 3037 vm_object_set_flag(srcobject, OBJ_OPT); 3038 vm_object_reference(srcobject); 3039 entry->object.vm_object = srcobject; 3040 3041 if (oldobject) { 3042 vm_object_deallocate(oldobject); 3043 } 3044 } 3045 3046 entry->offset = cp; 3047 map->timestamp++; 3048 } else { 3049 pmap_remove (map->pmap, uaddr, tend); 3050 } 3051 3052 } else if ((first_object->ref_count == 1) && 3053 (first_object->size == osize) && 3054 ((first_object->type == OBJT_DEFAULT) || 3055 (first_object->type == OBJT_SWAP)) ) { 3056 3057 oldobject = first_object->backing_object; 3058 3059 if ((first_object->backing_object_offset != cp) || 3060 (oldobject != srcobject)) { 3061 /* 3062 * Remove old window into the file 3063 */ 3064 pmap_remove (map->pmap, uaddr, tend); 3065 3066 /* 3067 * Remove unneeded old pages 3068 */ 3069 vm_object_page_remove(first_object, 0, 0, 0); 3070 3071 /* 3072 * Invalidate swap space 3073 */ 3074 if (first_object->type == OBJT_SWAP) { 3075 swap_pager_freespace(first_object, 3076 0, 3077 first_object->size); 3078 } 3079 3080 /* 3081 * Force copy on write for mmaped regions 3082 */ 3083 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 3084 3085 /* 3086 * Point the object appropriately 3087 */ 3088 if (oldobject != srcobject) { 3089 /* 3090 * Set the object optimization hint flag 3091 */ 3092 vm_object_set_flag(srcobject, OBJ_OPT); 3093 vm_object_reference(srcobject); 3094 3095 if (oldobject) { 3096 TAILQ_REMOVE(&oldobject->shadow_head, 3097 first_object, shadow_list); 3098 oldobject->shadow_count--; 3099 /* XXX bump generation? */ 3100 vm_object_deallocate(oldobject); 3101 } 3102 3103 TAILQ_INSERT_TAIL(&srcobject->shadow_head, 3104 first_object, shadow_list); 3105 srcobject->shadow_count++; 3106 /* XXX bump generation? */ 3107 3108 first_object->backing_object = srcobject; 3109 } 3110 first_object->backing_object_offset = cp; 3111 map->timestamp++; 3112 } else { 3113 pmap_remove (map->pmap, uaddr, tend); 3114 } 3115/* 3116 * Otherwise, we have to do a logical mmap. 3117 */ 3118 } else { 3119 3120 vm_object_set_flag(srcobject, OBJ_OPT); 3121 vm_object_reference(srcobject); 3122 3123 pmap_remove (map->pmap, uaddr, tend); 3124 3125 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 3126 vm_map_lock_upgrade(map); 3127 3128 if (entry == &map->header) { 3129 map->first_free = &map->header; 3130 } else if (map->first_free->start >= start) { 3131 map->first_free = entry->prev; 3132 } 3133 3134 SAVE_HINT(map, entry->prev); 3135 vm_map_entry_delete(map, entry); 3136 3137 object = srcobject; 3138 ooffset = cp; 3139 3140 rv = vm_map_insert(map, object, ooffset, start, tend, 3141 VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE); 3142 3143 if (rv != KERN_SUCCESS) 3144 panic("vm_uiomove: could not insert new entry: %d", rv); 3145 } 3146 3147/* 3148 * Map the window directly, if it is already in memory 3149 */ 3150 pmap_object_init_pt(map->pmap, uaddr, 3151 srcobject, oindex, tcnt, 0); 3152 3153 map->timestamp++; 3154 vm_map_unlock(map); 3155 3156 cnt -= tcnt; 3157 uaddr += tcnt; 3158 cp += tcnt; 3159 if (npages) 3160 *npages += osize; 3161 } 3162 return 0; 3163} 3164 3165/* 3166 * Performs the copy_on_write operations necessary to allow the virtual copies 3167 * into user space to work. This has to be called for write(2) system calls 3168 * from other processes, file unlinking, and file size shrinkage. 3169 */ 3170void 3171vm_freeze_copyopts(vm_object_t object, vm_pindex_t froma, vm_pindex_t toa) 3172{ 3173 int rv; 3174 vm_object_t robject; 3175 vm_pindex_t idx; 3176 3177 GIANT_REQUIRED; 3178 if ((object == NULL) || 3179 ((object->flags & OBJ_OPT) == 0)) 3180 return; 3181 3182 if (object->shadow_count > object->ref_count) 3183 panic("vm_freeze_copyopts: sc > rc"); 3184 3185 while ((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) { 3186 vm_pindex_t bo_pindex; 3187 vm_page_t m_in, m_out; 3188 3189 bo_pindex = OFF_TO_IDX(robject->backing_object_offset); 3190 3191 vm_object_reference(robject); 3192 3193 vm_object_pip_wait(robject, "objfrz"); 3194 3195 if (robject->ref_count == 1) { 3196 vm_object_deallocate(robject); 3197 continue; 3198 } 3199 3200 vm_object_pip_add(robject, 1); 3201 3202 for (idx = 0; idx < robject->size; idx++) { 3203 3204 m_out = vm_page_grab(robject, idx, 3205 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 3206 3207 if (m_out->valid == 0) { 3208 m_in = vm_page_grab(object, bo_pindex + idx, 3209 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 3210 if (m_in->valid == 0) { 3211 rv = vm_pager_get_pages(object, &m_in, 1, 0); 3212 if (rv != VM_PAGER_OK) { 3213 printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex); 3214 continue; 3215 } 3216 vm_page_deactivate(m_in); 3217 } 3218 3219 vm_page_protect(m_in, VM_PROT_NONE); 3220 pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out)); 3221 m_out->valid = m_in->valid; 3222 vm_page_dirty(m_out); 3223 vm_page_activate(m_out); 3224 vm_page_wakeup(m_in); 3225 } 3226 vm_page_wakeup(m_out); 3227 } 3228 3229 object->shadow_count--; 3230 object->ref_count--; 3231 TAILQ_REMOVE(&object->shadow_head, robject, shadow_list); 3232 robject->backing_object = NULL; 3233 robject->backing_object_offset = 0; 3234 3235 vm_object_pip_wakeup(robject); 3236 vm_object_deallocate(robject); 3237 } 3238 3239 vm_object_clear_flag(object, OBJ_OPT); 3240} 3241 3242#include "opt_ddb.h" 3243#ifdef DDB 3244#include <sys/kernel.h> 3245 3246#include <ddb/ddb.h> 3247 3248/* 3249 * vm_map_print: [ debug ] 3250 */ 3251DB_SHOW_COMMAND(map, vm_map_print) 3252{ 3253 static int nlines; 3254 /* XXX convert args. */ 3255 vm_map_t map = (vm_map_t)addr; 3256 boolean_t full = have_addr; 3257 3258 vm_map_entry_t entry; 3259 3260 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 3261 (void *)map, 3262 (void *)map->pmap, map->nentries, map->timestamp); 3263 nlines++; 3264 3265 if (!full && db_indent) 3266 return; 3267 3268 db_indent += 2; 3269 for (entry = map->header.next; entry != &map->header; 3270 entry = entry->next) { 3271 db_iprintf("map entry %p: start=%p, end=%p\n", 3272 (void *)entry, (void *)entry->start, (void *)entry->end); 3273 nlines++; 3274 { 3275 static char *inheritance_name[4] = 3276 {"share", "copy", "none", "donate_copy"}; 3277 3278 db_iprintf(" prot=%x/%x/%s", 3279 entry->protection, 3280 entry->max_protection, 3281 inheritance_name[(int)(unsigned char)entry->inheritance]); 3282 if (entry->wired_count != 0) 3283 db_printf(", wired"); 3284 } 3285 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3286 /* XXX no %qd in kernel. Truncate entry->offset. */ 3287 db_printf(", share=%p, offset=0x%lx\n", 3288 (void *)entry->object.sub_map, 3289 (long)entry->offset); 3290 nlines++; 3291 if ((entry->prev == &map->header) || 3292 (entry->prev->object.sub_map != 3293 entry->object.sub_map)) { 3294 db_indent += 2; 3295 vm_map_print((db_expr_t)(intptr_t) 3296 entry->object.sub_map, 3297 full, 0, (char *)0); 3298 db_indent -= 2; 3299 } 3300 } else { 3301 /* XXX no %qd in kernel. Truncate entry->offset. */ 3302 db_printf(", object=%p, offset=0x%lx", 3303 (void *)entry->object.vm_object, 3304 (long)entry->offset); 3305 if (entry->eflags & MAP_ENTRY_COW) 3306 db_printf(", copy (%s)", 3307 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 3308 db_printf("\n"); 3309 nlines++; 3310 3311 if ((entry->prev == &map->header) || 3312 (entry->prev->object.vm_object != 3313 entry->object.vm_object)) { 3314 db_indent += 2; 3315 vm_object_print((db_expr_t)(intptr_t) 3316 entry->object.vm_object, 3317 full, 0, (char *)0); 3318 nlines += 4; 3319 db_indent -= 2; 3320 } 3321 } 3322 } 3323 db_indent -= 2; 3324 if (db_indent == 0) 3325 nlines = 0; 3326} 3327 3328 3329DB_SHOW_COMMAND(procvm, procvm) 3330{ 3331 struct proc *p; 3332 3333 if (have_addr) { 3334 p = (struct proc *) addr; 3335 } else { 3336 p = curproc; 3337 } 3338 3339 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 3340 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 3341 (void *)vmspace_pmap(p->p_vmspace)); 3342 3343 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL); 3344} 3345 3346#endif /* DDB */
|