Index: kernel/generic/src/mm/as.c
===================================================================
--- kernel/generic/src/mm/as.c	(revision 326bf65ebff26a7eb2af6a434fcb5ee4b2a7e0a0)
+++ kernel/generic/src/mm/as.c	(revision b6f3e7e89f35903bd2012ad022ef70b966d8b246)
@@ -302,5 +302,5 @@
 	 * We don't want any area to have conflicts with NULL page.
 	 */
-	if (overlaps(addr, count << PAGE_WIDTH, (uintptr_t) NULL, PAGE_SIZE))
+	if (overlaps(addr, P2SZ(count), (uintptr_t) NULL, PAGE_SIZE))
 		return false;
 	
@@ -329,6 +329,6 @@
 			mutex_lock(&area->lock);
 			
-			if (overlaps(addr, count << PAGE_WIDTH,
-			    area->base, area->pages << PAGE_WIDTH)) {
+			if (overlaps(addr, P2SZ(count), area->base,
+			    P2SZ(area->pages))) {
 				mutex_unlock(&area->lock);
 				return false;
@@ -346,6 +346,6 @@
 			mutex_lock(&area->lock);
 			
-			if (overlaps(addr, count << PAGE_WIDTH,
-			    area->base, area->pages << PAGE_WIDTH)) {
+			if (overlaps(addr, P2SZ(count), area->base,
+			    P2SZ(area->pages))) {
 				mutex_unlock(&area->lock);
 				return false;
@@ -366,6 +366,6 @@
 		mutex_lock(&area->lock);
 		
-		if (overlaps(addr, count << PAGE_WIDTH,
-		    area->base, area->pages << PAGE_WIDTH)) {
+		if (overlaps(addr, P2SZ(count), area->base,
+		    P2SZ(area->pages))) {
 			mutex_unlock(&area->lock);
 			return false;
@@ -380,6 +380,5 @@
 	 */
 	if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
-		return !overlaps(addr, count << PAGE_WIDTH,
-		    KERNEL_ADDRESS_SPACE_START,
+		return !overlaps(addr, P2SZ(count), KERNEL_ADDRESS_SPACE_START,
 		    KERNEL_ADDRESS_SPACE_END - KERNEL_ADDRESS_SPACE_START);
 	}
@@ -474,5 +473,6 @@
 	
 	btree_node_t *leaf;
-	as_area_t *area = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
+	as_area_t *area = (as_area_t *) btree_search(&as->as_area_btree, va,
+	    &leaf);
 	if (area) {
 		/* va is the base address of an address space area */
@@ -495,6 +495,6 @@
 		mutex_lock(&area->lock);
 
-		size_t size = area->pages << PAGE_WIDTH;
-		if ((area->base <= va) && (va <= area->base + (size - 1)))
+		if ((area->base <= va) &&
+		    (va <= area->base + (P2SZ(area->pages) - 1)))
 			return area;
 		
@@ -506,5 +506,6 @@
 	 * Because of its position in the B+tree, it must have base < va.
 	 */
-	btree_node_t *lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf);
+	btree_node_t *lnode = btree_leaf_node_left_neighbour(&as->as_area_btree,
+	    leaf);
 	if (lnode) {
 		area = (as_area_t *) lnode->value[lnode->keys - 1];
@@ -512,5 +513,5 @@
 		mutex_lock(&area->lock);
 		
-		if (va < area->base + (area->pages << PAGE_WIDTH))
+		if (va <= area->base + (P2SZ(area->pages) - 1))
 			return area;
 		
@@ -577,5 +578,5 @@
 	
 	if (pages < area->pages) {
-		uintptr_t start_free = area->base + (pages << PAGE_WIDTH);
+		uintptr_t start_free = area->base + P2SZ(pages);
 		
 		/*
@@ -590,5 +591,5 @@
 		 */
 		ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid,
-		    area->base + (pages << PAGE_WIDTH), area->pages - pages);
+		    area->base + P2SZ(pages), area->pages - pages);
 		
 		/*
@@ -613,8 +614,8 @@
 				size_t i = 0;
 				
-				if (overlaps(ptr, size << PAGE_WIDTH, area->base,
-				    pages << PAGE_WIDTH)) {
+				if (overlaps(ptr, P2SZ(size), area->base,
+				    P2SZ(pages))) {
 					
-					if (ptr + (size << PAGE_WIDTH) <= start_free) {
+					if (ptr + P2SZ(size) <= start_free) {
 						/*
 						 * The whole interval fits
@@ -647,6 +648,6 @@
 				
 				for (; i < size; i++) {
-					pte_t *pte = page_mapping_find(as, ptr +
-					    (i << PAGE_WIDTH));
+					pte_t *pte = page_mapping_find(as,
+					    ptr + P2SZ(i));
 					
 					ASSERT(pte);
@@ -657,10 +658,9 @@
 					    (area->backend->frame_free)) {
 						area->backend->frame_free(area,
-						    ptr + (i << PAGE_WIDTH),
+						    ptr + P2SZ(i),
 						    PTE_GET_FRAME(pte));
 					}
 					
-					page_mapping_remove(as, ptr +
-					    (i << PAGE_WIDTH));
+					page_mapping_remove(as, ptr + P2SZ(i));
 				}
 			}
@@ -671,5 +671,5 @@
 		 */
 		
-		tlb_invalidate_pages(as->asid, area->base + (pages << PAGE_WIDTH),
+		tlb_invalidate_pages(as->asid, area->base + P2SZ(pages),
 		    area->pages - pages);
 		
@@ -677,6 +677,6 @@
 		 * Invalidate software translation caches (e.g. TSB on sparc64).
 		 */
-		as_invalidate_translation_cache(as, area->base +
-		    (pages << PAGE_WIDTH), area->pages - pages);
+		as_invalidate_translation_cache(as, area->base + P2SZ(pages),
+		    area->pages - pages);
 		tlb_shootdown_finalize(ipl);
 		
@@ -797,6 +797,6 @@
 			
 			for (size = 0; size < (size_t) node->value[i]; size++) {
-				pte_t *pte =
-				    page_mapping_find(as, ptr + (size << PAGE_WIDTH));
+				pte_t *pte = page_mapping_find(as,
+				     ptr + P2SZ(size));
 				
 				ASSERT(pte);
@@ -807,8 +807,9 @@
 				    (area->backend->frame_free)) {
 					area->backend->frame_free(area,
-					    ptr + (size << PAGE_WIDTH), PTE_GET_FRAME(pte));
+					    ptr + P2SZ(size),
+					    PTE_GET_FRAME(pte));
 				}
 				
-				page_mapping_remove(as, ptr + (size << PAGE_WIDTH));
+				page_mapping_remove(as, ptr + P2SZ(size));
 			}
 		}
@@ -897,5 +898,5 @@
 	}
 	
-	size_t src_size = src_area->pages << PAGE_WIDTH;
+	size_t src_size = P2SZ(src_area->pages);
 	unsigned int src_flags = src_area->flags;
 	mem_backend_t *src_backend = src_area->backend;
@@ -1094,6 +1095,6 @@
 	for (cur = area->used_space.leaf_head.next;
 	    cur != &area->used_space.leaf_head; cur = cur->next) {
-		btree_node_t *node
-		    = list_get_instance(cur, btree_node_t, leaf_link);
+		btree_node_t *node = list_get_instance(cur, btree_node_t,
+		    leaf_link);
 		btree_key_t i;
 		
@@ -1103,6 +1104,6 @@
 			
 			for (size = 0; size < (size_t) node->value[i]; size++) {
-				pte_t *pte =
-				    page_mapping_find(as, ptr + (size << PAGE_WIDTH));
+				pte_t *pte = page_mapping_find(as,
+				    ptr + P2SZ(size));
 				
 				ASSERT(pte);
@@ -1113,5 +1114,5 @@
 				
 				/* Remove old mapping */
-				page_mapping_remove(as, ptr + (size << PAGE_WIDTH));
+				page_mapping_remove(as, ptr + P2SZ(size));
 			}
 		}
@@ -1159,5 +1160,5 @@
 				
 				/* Insert the new mapping */
-				page_mapping_insert(as, ptr + (size << PAGE_WIDTH),
+				page_mapping_insert(as, ptr + P2SZ(size),
 				    old_frame[frame_idx++], page_flags);
 				
@@ -1481,5 +1482,5 @@
 	
 	if (src_area) {
-		size = src_area->pages << PAGE_WIDTH;
+		size = P2SZ(src_area->pages);
 		mutex_unlock(&src_area->lock);
 	} else
@@ -1536,14 +1537,14 @@
 		if (page >= right_pg) {
 			/* Do nothing. */
-		} else if (overlaps(page, count << PAGE_WIDTH, left_pg,
-		    left_cnt << PAGE_WIDTH)) {
+		} else if (overlaps(page, P2SZ(count), left_pg,
+		    P2SZ(left_cnt))) {
 			/* The interval intersects with the left interval. */
 			return false;
-		} else if (overlaps(page, count << PAGE_WIDTH, right_pg,
-		    right_cnt << PAGE_WIDTH)) {
+		} else if (overlaps(page, P2SZ(count), right_pg,
+		    P2SZ(right_cnt))) {
 			/* The interval intersects with the right interval. */
 			return false;
-		} else if ((page == left_pg + (left_cnt << PAGE_WIDTH)) &&
-		    (page + (count << PAGE_WIDTH) == right_pg)) {
+		} else if ((page == left_pg + P2SZ(left_cnt)) &&
+		    (page + P2SZ(count) == right_pg)) {
 			/*
 			 * The interval can be added by merging the two already
@@ -1553,5 +1554,5 @@
 			btree_remove(&area->used_space, right_pg, leaf);
 			goto success;
-		} else if (page == left_pg + (left_cnt << PAGE_WIDTH)) {
+		} else if (page == left_pg + P2SZ(left_cnt)) {
 			/*
 			 * The interval can be added by simply growing the left
@@ -1560,5 +1561,5 @@
 			node->value[node->keys - 1] += count;
 			goto success;
-		} else if (page + (count << PAGE_WIDTH) == right_pg) {
+		} else if (page + P2SZ(count) == right_pg) {
 			/*
 			 * The interval can be addded by simply moving base of
@@ -1587,9 +1588,8 @@
 		 */
 		
-		if (overlaps(page, count << PAGE_WIDTH, right_pg,
-		    right_cnt << PAGE_WIDTH)) {
+		if (overlaps(page, P2SZ(count), right_pg, P2SZ(right_cnt))) {
 			/* The interval intersects with the right interval. */
 			return false;
-		} else if (page + (count << PAGE_WIDTH) == right_pg) {
+		} else if (page + P2SZ(count) == right_pg) {
 			/*
 			 * The interval can be added by moving the base of the
@@ -1626,14 +1626,14 @@
 		if (page < left_pg) {
 			/* Do nothing. */
-		} else if (overlaps(page, count << PAGE_WIDTH, left_pg,
-		    left_cnt << PAGE_WIDTH)) {
+		} else if (overlaps(page, P2SZ(count), left_pg,
+		    P2SZ(left_cnt))) {
 			/* The interval intersects with the left interval. */
 			return false;
-		} else if (overlaps(page, count << PAGE_WIDTH, right_pg,
-		    right_cnt << PAGE_WIDTH)) {
+		} else if (overlaps(page, P2SZ(count), right_pg,
+		    P2SZ(right_cnt))) {
 			/* The interval intersects with the right interval. */
 			return false;
-		} else if ((page == left_pg + (left_cnt << PAGE_WIDTH)) &&
-		    (page + (count << PAGE_WIDTH) == right_pg)) {
+		} else if ((page == left_pg + P2SZ(left_cnt)) &&
+		    (page + P2SZ(count) == right_pg)) {
 			/*
 			 * The interval can be added by merging the two already
@@ -1643,5 +1643,5 @@
 			btree_remove(&area->used_space, right_pg, node);
 			goto success;
-		} else if (page == left_pg + (left_cnt << PAGE_WIDTH)) {
+		} else if (page == left_pg + P2SZ(left_cnt)) {
 			/*
 			 * The interval can be added by simply growing the left
@@ -1650,5 +1650,5 @@
 			leaf->value[leaf->keys - 1] += count;
 			goto success;
-		} else if (page + (count << PAGE_WIDTH) == right_pg) {
+		} else if (page + P2SZ(count) == right_pg) {
 			/*
 			 * The interval can be addded by simply moving base of
@@ -1677,9 +1677,8 @@
 		 */
 		
-		if (overlaps(page, count << PAGE_WIDTH, left_pg,
-		    left_cnt << PAGE_WIDTH)) {
+		if (overlaps(page, P2SZ(count), left_pg, P2SZ(left_cnt))) {
 			/* The interval intersects with the left interval. */
 			return false;
-		} else if (left_pg + (left_cnt << PAGE_WIDTH) == page) {
+		} else if (left_pg + P2SZ(left_cnt) == page) {
 			/*
 			 * The interval can be added by growing the left
@@ -1716,6 +1715,6 @@
 			 */
 			
-			if (overlaps(page, count << PAGE_WIDTH, left_pg,
-			    left_cnt << PAGE_WIDTH)) {
+			if (overlaps(page, P2SZ(count), left_pg,
+			    P2SZ(left_cnt))) {
 				/*
 				 * The interval intersects with the left
@@ -1723,6 +1722,6 @@
 				 */
 				return false;
-			} else if (overlaps(page, count << PAGE_WIDTH, right_pg,
-			    right_cnt << PAGE_WIDTH)) {
+			} else if (overlaps(page, P2SZ(count), right_pg,
+			    P2SZ(right_cnt))) {
 				/*
 				 * The interval intersects with the right
@@ -1730,6 +1729,6 @@
 				 */
 				return false;
-			} else if ((page == left_pg + (left_cnt << PAGE_WIDTH)) &&
-			    (page + (count << PAGE_WIDTH) == right_pg)) {
+			} else if ((page == left_pg + P2SZ(left_cnt)) &&
+			    (page + P2SZ(count) == right_pg)) {
 				/*
 				 * The interval can be added by merging the two
@@ -1739,5 +1738,5 @@
 				btree_remove(&area->used_space, right_pg, leaf);
 				goto success;
-			} else if (page == left_pg + (left_cnt << PAGE_WIDTH)) {
+			} else if (page == left_pg + P2SZ(left_cnt)) {
 				/*
 				 * The interval can be added by simply growing
@@ -1746,5 +1745,5 @@
 				leaf->value[i - 1] += count;
 				goto success;
-			} else if (page + (count << PAGE_WIDTH) == right_pg) {
+			} else if (page + P2SZ(count) == right_pg) {
 				/*
 				 * The interval can be addded by simply moving
@@ -1812,5 +1811,5 @@
 			for (i = 0; i < leaf->keys; i++) {
 				if (leaf->key[i] == page) {
-					leaf->key[i] += count << PAGE_WIDTH;
+					leaf->key[i] += P2SZ(count);
 					leaf->value[i] -= count;
 					goto success;
@@ -1822,13 +1821,12 @@
 	}
 	
-	btree_node_t *node = btree_leaf_node_left_neighbour(&area->used_space, leaf);
+	btree_node_t *node = btree_leaf_node_left_neighbour(&area->used_space,
+	    leaf);
 	if ((node) && (page < leaf->key[0])) {
 		uintptr_t left_pg = node->key[node->keys - 1];
 		size_t left_cnt = (size_t) node->value[node->keys - 1];
 		
-		if (overlaps(left_pg, left_cnt << PAGE_WIDTH, page,
-		    count << PAGE_WIDTH)) {
-			if (page + (count << PAGE_WIDTH) ==
-			    left_pg + (left_cnt << PAGE_WIDTH)) {
+		if (overlaps(left_pg, P2SZ(left_cnt), page, P2SZ(count))) {
+			if (page + P2SZ(count) == left_pg + P2SZ(left_cnt)) {
 				/*
 				 * The interval is contained in the rightmost
@@ -1839,6 +1837,8 @@
 				node->value[node->keys - 1] -= count;
 				goto success;
-			} else if (page + (count << PAGE_WIDTH) <
-			    left_pg + (left_cnt << PAGE_WIDTH)) {
+			} else if (page + P2SZ(count) <
+			    left_pg + P2SZ(left_cnt)) {
+				size_t new_cnt;
+
 				/*
 				 * The interval is contained in the rightmost
@@ -1848,9 +1848,9 @@
 				 * new interval.
 				 */
-				size_t new_cnt = ((left_pg + (left_cnt << PAGE_WIDTH)) -
-				    (page + (count << PAGE_WIDTH))) >> PAGE_WIDTH;
+				new_cnt = ((left_pg + P2SZ(left_cnt)) -
+				    (page + P2SZ(count))) >> PAGE_WIDTH;
 				node->value[node->keys - 1] -= count + new_cnt;
 				btree_insert(&area->used_space, page +
-				    (count << PAGE_WIDTH), (void *) new_cnt, leaf);
+				    P2SZ(count), (void *) new_cnt, leaf);
 				goto success;
 			}
@@ -1865,8 +1865,6 @@
 		size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
 		
-		if (overlaps(left_pg, left_cnt << PAGE_WIDTH, page,
-		    count << PAGE_WIDTH)) {
-			if (page + (count << PAGE_WIDTH) ==
-			    left_pg + (left_cnt << PAGE_WIDTH)) {
+		if (overlaps(left_pg, P2SZ(left_cnt), page, P2SZ(count))) {
+			if (page + P2SZ(count) == left_pg + P2SZ(left_cnt)) {
 				/*
 				 * The interval is contained in the rightmost
@@ -1876,6 +1874,8 @@
 				leaf->value[leaf->keys - 1] -= count;
 				goto success;
-			} else if (page + (count << PAGE_WIDTH) < left_pg +
-			    (left_cnt << PAGE_WIDTH)) {
+			} else if (page + P2SZ(count) < left_pg +
+			    P2SZ(left_cnt)) {
+				size_t new_cnt;
+
 				/*
 				 * The interval is contained in the rightmost
@@ -1885,9 +1885,9 @@
 				 * interval.
 				 */
-				size_t new_cnt = ((left_pg + (left_cnt << PAGE_WIDTH)) -
-				    (page + (count << PAGE_WIDTH))) >> PAGE_WIDTH;
+				new_cnt = ((left_pg + P2SZ(left_cnt)) -
+				    (page + P2SZ(count))) >> PAGE_WIDTH;
 				leaf->value[leaf->keys - 1] -= count + new_cnt;
 				btree_insert(&area->used_space, page +
-				    (count << PAGE_WIDTH), (void *) new_cnt, leaf);
+				    P2SZ(count), (void *) new_cnt, leaf);
 				goto success;
 			}
@@ -1911,8 +1911,8 @@
 			 * to (i - 1) and i.
 			 */
-			if (overlaps(left_pg, left_cnt << PAGE_WIDTH, page,
-			    count << PAGE_WIDTH)) {
-				if (page + (count << PAGE_WIDTH) ==
-				    left_pg + (left_cnt << PAGE_WIDTH)) {
+			if (overlaps(left_pg, P2SZ(left_cnt), page,
+			    P2SZ(count))) {
+				if (page + P2SZ(count) ==
+				    left_pg + P2SZ(left_cnt)) {
 					/*
 					 * The interval is contained in the
@@ -1923,6 +1923,8 @@
 					leaf->value[i - 1] -= count;
 					goto success;
-				} else if (page + (count << PAGE_WIDTH) <
-				    left_pg + (left_cnt << PAGE_WIDTH)) {
+				} else if (page + P2SZ(count) <
+				    left_pg + P2SZ(left_cnt)) {
+					size_t new_cnt;
+
 					/*
 					 * The interval is contained in the
@@ -1932,11 +1934,10 @@
 					 * also inserting a new interval.
 					 */
-					size_t new_cnt = ((left_pg +
-					    (left_cnt << PAGE_WIDTH)) -
-					    (page + (count << PAGE_WIDTH))) >>
+					new_cnt = ((left_pg + P2SZ(left_cnt)) -
+					    (page + P2SZ(count))) >>
 					    PAGE_WIDTH;
 					leaf->value[i - 1] -= count + new_cnt;
 					btree_insert(&area->used_space, page +
-					    (count << PAGE_WIDTH), (void *) new_cnt,
+					    P2SZ(count), (void *) new_cnt,
 					    leaf);
 					goto success;
@@ -2034,10 +2035,11 @@
 		btree_key_t i;
 		for (i = 0; (ret == 0) && (i < node->keys); i++) {
+			uintptr_t addr;
+
 			as_area_t *area = (as_area_t *) node->value[i];
 			
 			mutex_lock(&area->lock);
 			
-			uintptr_t addr =
-			    ALIGN_UP(area->base + (area->pages << PAGE_WIDTH),
+			addr = ALIGN_UP(area->base + P2SZ(area->pages),
 			    PAGE_SIZE);
 			
@@ -2098,5 +2100,5 @@
 			
 			info[area_idx].start_addr = area->base;
-			info[area_idx].size = FRAMES2SIZE(area->pages);
+			info[area_idx].size = P2SZ(area->pages);
 			info[area_idx].flags = area->flags;
 			++area_idx;
@@ -2136,5 +2138,5 @@
 			    " (%p - %p)\n", area, (void *) area->base,
 			    area->pages, (void *) area->base,
-			    (void *) (area->base + FRAMES2SIZE(area->pages)));
+			    (void *) (area->base + P2SZ(area->pages)));
 			mutex_unlock(&area->lock);
 		}
