#include <mach_debug.h>
#include <mach_kdb.h>
#include <mach_kdp.h>
#include <debug.h>
#include <cpus.h>
#include <mach/vm_types.h>
#include <mach/vm_param.h>
#include <mach/thread_status.h>
#include <kern/misc_protos.h>
#include <kern/assert.h>
#include <kern/cpu_number.h>
#include <ppc/proc_reg.h>
#include <ppc/Firmware.h>
#include <ppc/boot.h>
#include <ppc/misc_protos.h>
#include <ppc/pmap.h>
#include <ppc/pmap_internals.h>
#include <ppc/mem.h>
#include <ppc/mappings.h>
#include <ppc/exception.h>
#include <ppc/mp.h>
#ifdef __MACHO__
#include <mach-o/mach_header.h>
#endif
extern unsigned int intstack[];
extern unsigned int intstack_top_ss;
vm_offset_t mem_size;
vm_offset_t mem_actual;
mem_region_t pmap_mem_regions[PMAP_MEM_REGION_MAX];
int pmap_mem_regions_count = 0;
mem_region_t free_regions[FREE_REGION_MAX];
int free_regions_count;
#ifndef __MACHO__
extern unsigned long etext;
#endif
unsigned int avail_remaining = 0;
vm_offset_t first_avail;
vm_offset_t static_memory_end;
extern vm_offset_t avail_next;
#ifdef __MACHO__
extern struct mach_header _mh_execute_header;
vm_offset_t sectTEXTB;
int sectSizeTEXT;
vm_offset_t sectDATAB;
int sectSizeDATA;
vm_offset_t sectLINKB;
int sectSizeLINK;
vm_offset_t sectKLDB;
int sectSizeKLD;
vm_offset_t end, etext, edata;
#endif
extern unsigned long exception_entry;
extern unsigned long exception_end;
void ppc_vm_init(unsigned int mem_limit, boot_args *args)
{
unsigned int htabmask;
unsigned int i, j, batsize, kmapsize;
vm_offset_t addr;
int boot_task_end_offset;
const char *cpus;
mapping *mp;
vm_offset_t first_phys_avail;
vm_offset_t sizeadj, oldstart;
sectTEXTB = (vm_offset_t)getsegdatafromheader(
&_mh_execute_header, "__TEXT", §SizeTEXT);
sectDATAB = (vm_offset_t)getsegdatafromheader(
&_mh_execute_header, "__DATA", §SizeDATA);
sectLINKB = (vm_offset_t)getsegdatafromheader(
&_mh_execute_header, "__LINKEDIT", §SizeLINK);
sectKLDB = (vm_offset_t)getsegdatafromheader(
&_mh_execute_header, "__KLD", §SizeKLD);
etext = (vm_offset_t) sectTEXTB + sectSizeTEXT;
edata = (vm_offset_t) sectDATAB + sectSizeDATA;
end = round_page(getlastaddr());
#if DEBUG
kprintf("sectTEXT: %x, size: %x\n", sectTEXTB, sectSizeTEXT);
kprintf("sectDATA: %x, size: %x\n", sectDATAB, sectSizeDATA);
kprintf("sectLINK: %x, size: %x\n", sectLINKB, sectSizeLINK);
kprintf("sectKLD: %x, size: %x\n", sectKLDB, sectSizeKLD);
kprintf("end: %x\n", end);
#endif
mem_actual = args->PhysicalDRAM[0].base + args->PhysicalDRAM[0].size;
addr = 0;
for (i = 1; i < kMaxDRAMBanks; i++) {
if (args->PhysicalDRAM[i].size == 0) continue;
if((args->PhysicalDRAM[i].base + args->PhysicalDRAM[i].size) > mem_actual) {
mem_actual = args->PhysicalDRAM[i].base + args->PhysicalDRAM[i].size;
}
if (args->PhysicalDRAM[i].base ==
args->PhysicalDRAM[addr].base +
args->PhysicalDRAM[addr].size) {
kprintf("region 0x%08x size 0x%08x joining region 0x%08x size 0x%08x\n",
args->PhysicalDRAM[addr].base, args->PhysicalDRAM[addr].size,
args->PhysicalDRAM[i].base, args->PhysicalDRAM[i].size);
args->PhysicalDRAM[addr].size += args->PhysicalDRAM[i].size;
args->PhysicalDRAM[i].size = 0;
continue;
}
addr = i;
}
pmap_mem_regions_count = 0;
mem_size = 0;
for (i = 0; i < kMaxDRAMBanks; i++) {
if (args->PhysicalDRAM[i].size == 0)
continue;
if (mem_limit > 0 &&
mem_size + args->PhysicalDRAM[i].size > mem_limit)
args->PhysicalDRAM[i].size = mem_limit - mem_size;
pmap_mem_regions[pmap_mem_regions_count].start =
args->PhysicalDRAM[i].base;
pmap_mem_regions[pmap_mem_regions_count].end =
args->PhysicalDRAM[i].base +
args->PhysicalDRAM[i].size;
assert ((pmap_mem_regions_count == 0) ||
pmap_mem_regions[pmap_mem_regions_count].start >
pmap_mem_regions[pmap_mem_regions_count-1].start);
if (pmap_mem_regions_count > 0) {
avail_remaining +=
args->PhysicalDRAM[i].size / PPC_PGBYTES;
}
mem_size += args->PhysicalDRAM[i].size;
pmap_mem_regions_count++;
}
kprintf("mem_size: %d M\n",mem_size / (1024 * 1024));
first_phys_avail = static_memory_end;
first_avail = adjust_bat_limit(first_phys_avail, 0, FALSE, FALSE);
kmapsize = (round_page(exception_end) - trunc_page(exception_entry)) +
(round_page(sectTEXTB+sectSizeTEXT) - trunc_page(sectTEXTB)) +
(round_page(sectDATAB+sectSizeDATA) - trunc_page(sectDATAB)) +
(round_page(sectLINKB+sectSizeLINK) - trunc_page(sectLINKB)) +
(round_page(sectKLDB+sectSizeKLD) - trunc_page(sectKLDB)) +
(round_page(static_memory_end) - trunc_page(end));
pmap_bootstrap(mem_size,&first_avail,&first_phys_avail, kmapsize);
#ifdef __MACHO__
#if DEBUG
kprintf("Mapping memory:\n");
kprintf(" exception vector: %08X, %08X - %08X\n", trunc_page(exception_entry),
trunc_page(exception_entry), round_page(exception_end));
kprintf(" sectTEXTB: %08X, %08X - %08X\n", trunc_page(sectTEXTB),
trunc_page(sectTEXTB), round_page(sectTEXTB+sectSizeTEXT));
kprintf(" sectDATAB: %08X, %08X - %08X\n", trunc_page(sectDATAB),
trunc_page(sectDATAB), round_page(sectDATAB+sectSizeDATA));
kprintf(" sectLINKB: %08X, %08X - %08X\n", trunc_page(sectLINKB),
trunc_page(sectLINKB), round_page(sectLINKB+sectSizeLINK));
kprintf(" sectKLDB: %08X, %08X - %08X\n", trunc_page(sectKLDB),
trunc_page(sectKLDB), round_page(sectKLDB+sectSizeKLD));
kprintf(" end: %08X, %08X - %08X\n", trunc_page(end),
trunc_page(end), static_memory_end);
#endif
pmap_map(trunc_page(exception_entry), trunc_page(exception_entry),
round_page(exception_end), VM_PROT_READ|VM_PROT_EXECUTE);
pmap_map(trunc_page(sectTEXTB), trunc_page(sectTEXTB),
round_page(sectTEXTB+sectSizeTEXT), VM_PROT_READ|VM_PROT_EXECUTE);
pmap_map(trunc_page(sectDATAB), trunc_page(sectDATAB),
round_page(sectDATAB+sectSizeDATA), VM_PROT_READ|VM_PROT_WRITE);
for (addr = trunc_page(sectKLDB);
addr < round_page(sectKLDB+sectSizeKLD);
addr += PAGE_SIZE) {
pmap_enter(kernel_pmap, addr, addr,
VM_PROT_READ|VM_PROT_WRITE,
VM_WIMG_USE_DEFAULT, TRUE);
}
for (addr = trunc_page(sectLINKB);
addr < round_page(sectLINKB+sectSizeLINK);
addr += PAGE_SIZE) {
pmap_enter(kernel_pmap, addr, addr,
VM_PROT_READ|VM_PROT_WRITE,
VM_WIMG_USE_DEFAULT, TRUE);
}
for(addr = trunc_page(end); addr < round_page(static_memory_end); addr += PAGE_SIZE) {
pmap_enter(kernel_pmap, addr, addr,
VM_PROT_READ|VM_PROT_WRITE,
VM_WIMG_USE_DEFAULT, TRUE);
}
#endif
#if DEBUG
for (i=0 ; i < free_regions_count; i++) {
kprintf("Free region start 0x%08x end 0x%08x\n",
free_regions[i].start,free_regions[i].end);
}
#endif
LoadIBATs((unsigned int *)&shadow_BAT.IBATs[0]);
LoadDBATs((unsigned int *)&shadow_BAT.DBATs[0]);
#if DEBUG
for(i=0; i<4; i++) kprintf("DBAT%1d: %08X %08X\n",
i, shadow_BAT.DBATs[i].upper, shadow_BAT.DBATs[i].lower);
for(i=0; i<4; i++) kprintf("IBAT%1d: %08X %08X\n",
i, shadow_BAT.IBATs[i].upper, shadow_BAT.IBATs[i].lower);
#endif
}
void ppc_vm_cpu_init(
struct per_proc_info *proc_info)
{
hash_table_init(hash_table_base, hash_table_size);
LoadIBATs((unsigned int *)&shadow_BAT.IBATs[0]);
LoadDBATs((unsigned int *)&shadow_BAT.DBATs[0]);
sync();isync();
}