Index: arch/ppc32/loader/asm.S
===================================================================
--- arch/ppc32/loader/asm.S	(revision a88ab79527d0ad8172c9cf4cde9461f03f3a3a3c)
+++ arch/ppc32/loader/asm.S	(revision bab785fe0f9fc5d441ad855a3adbd52b66bb6a51)
@@ -196,5 +196,5 @@
 	li r29, 8
 	mtctr r29
-	li r30, 0
+	li r30, 0                     # ASID 0 (VSIDs 0 .. 7)
 
 	seg_fill_uspace:
@@ -208,6 +208,6 @@
 	li r29, 8
 	mtctr r29
-	lis r30, 0x4000
-	ori r30, r30, 8
+	lis r30, 0x4000               # priviledged access only
+	ori r30, r30, 8               # ASID 0 (VSIDs 8 .. 15)
 	
 	seg_fill_kernel:
@@ -261,4 +261,6 @@
 	pht_clear:
 		
+		# write zeroes
+		
 		stw r29, 0(r31)
 		
@@ -280,23 +282,51 @@
 	mtsdr1 r31
 	
-	# create identity mapping
-	
 #ifdef CONFIG_BAT
 	
+	# create BAT identity mapping
+	
 	DEBUG_mapping
 	
-	# FIXME: map exactly the size of RAM
-	
-	lis r31, 0x8000
-	ori r31, r31, 0x0ffe
+	lwz r31, 0(r3)
+	
+	lis r29, 0x0002
+	cmpw r31, r29
+	blt no_bat                    # less than 128 KB -> no BAT
+	
+	li r29, 18
+	srw r31, r31, r29             # r31 = total >> 18
+	
+	# create Block Length mask by replicating
+	# the leading logical one 14 times
+	
+	li r29, 14
+	mtctr r31
+	li r29, 1
+	
+	bat_mask:
+		srw r30, r31, r29         # r30 = mask >> 1
+		or r31, r31, r30          # mask = mask | r30
+		
+		bdnz bat_mask
+	
+	andi. r31, r31, 0x07ff        # mask = mask & 0x07ff (BAT can map up to 256 MB)
+	
+	li r29, 2
+	slw r31, r31, r29             # mask = mask << 2
+	ori r31, r31, 0x0002          # mask = mask | 0x0002 (priviledged access only)
+	
+	lis r29, 0x8000
+	or r29, r29, r31
 	
 	lis r30, 0x0000
 	ori r30, r30, 0x0002
 	
-	mtspr ibat0u, r31
+	mtspr ibat0u, r29
 	mtspr ibat0l, r30
 	
-	mtspr dbat0u, r31
+	mtspr dbat0u, r29
 	mtspr dbat0l, r30
+	
+	no_bat:
 
 #endif
Index: arch/ppc32/loader/debug.inc
===================================================================
--- arch/ppc32/loader/debug.inc	(revision a88ab79527d0ad8172c9cf4cde9461f03f3a3a3c)
+++ arch/ppc32/loader/debug.inc	(revision bab785fe0f9fc5d441ad855a3adbd52b66bb6a51)
@@ -8308,2 +8308,7 @@
 #endif
 .endm
+
+.macro DEBUG_bat_mask
+#ifdef CONFIG_DEBUG
+#endif
+.endm
