Subject: Some changes for the brave of heart
To: None <port-mac68k@NetBSD.ORG>
From: Scott Reynolds <scottr@og.org>
List: port-mac68k
Date: 12/31/1996 00:41:11
If all of the following are true

  -  You like to do strange and wonderful things to your kernels;
  -  Your system is `expendable' -- or at least, can be easily recovered;
  -  You have a Duo 2x0, Quadra 6x0, Quadra 700, or AV Quadra that works
     with any kernel;

You could be an alpha tester!  The enclosed patch has been tested on a Mac
IIx, but since that's an 030-based machine, the 040 changes can't be
checked out.  What these changes do:

030-based:

  -  Modify get_pte() so that it's not quite so intrusive.  The minimal
     use of the TT0 register should allow this to work on the Duo 2x0
     machines, now.  (I'm not sure about ADB, though, and it's late and
     I'm tired...)

040-based:

  -  Add AV internal video probe
  -  Get memory mapping from Mac OS page tables, trying the supervisor
     page tables and falling back to the user page tables (i.e. this
     might work on both the AV machines and on the Q6x0/Q700).

If you try this, please let me know if they cause problems or get your
machine to work where it hadn't previously.

Thanks!

--scott

PS - there should be no change for the 030 machines in general, but I'd
like to hear feedback from someone with a IIci, LC III, or other 030-based
internal video machine.

Index: mac68k/locore.s
===================================================================
RCS file: /cvsroot/src/sys/arch/mac68k/mac68k/locore.s,v
retrieving revision 1.71
diff -c -r1.71 locore.s
*** locore.s	1996/11/06 20:19:35	1.71
--- locore.s	1996/12/31 06:09:58
***************
*** 900,908 ****
  	.long	0x4e7b0005		| movc d0,itt1 ;Disable itt1
  	.long	0x4e7b0006		| movc d0,dtt0 ;Disable dtt0
  	.long	0x4e7b0007		| movc d0,dtt1 ;Disable dtt1
- 	.long	0x4e7b0003		| movc d0,tc   ;Disable MMU
  
! 	movl	#0x0,sp@-		| Fake unenabled MMU
  	jra	do_bootstrap
  
  Lstartnot040:
--- 900,910 ----
  	.long	0x4e7b0005		| movc d0,itt1 ;Disable itt1
  	.long	0x4e7b0006		| movc d0,dtt0 ;Disable dtt0
  	.long	0x4e7b0007		| movc d0,dtt1 ;Disable dtt1
  
! 	.long	0x4e7a0003		| movec tc,d0
! 	lea	_macos_tc,a0
! 	movl	d0,a0@
! 	movl	d0,sp@-			| Save current TC for bootstrap
  	jra	do_bootstrap
  
  Lstartnot040:
***************
*** 939,949 ****
  	cmpl	#MMU_68040, _mmutype
  	jne	Lenablepre040MMU	| if not 040, skip
  	movl	a1,d1
- 	.long	0x4e7b1807		| movc d1,srp
  	.word	0xf4d8			| cinva bc
  	.word	0xf518			| pflusha
  	movl	#0x8000,d0
! 	.long	0x4e7b0003		| Enable MMU
  	movl	#0x80008000,d0
  	movc	d0,cacr			| turn on both caches
  	jra	Lloaddone
--- 941,951 ----
  	cmpl	#MMU_68040, _mmutype
  	jne	Lenablepre040MMU	| if not 040, skip
  	movl	a1,d1
  	.word	0xf4d8			| cinva bc
  	.word	0xf518			| pflusha
+ 	.long	0x4e7b1807		| movc d1,srp
  	movl	#0x8000,d0
! 	.long	0x4e7b0003		| movc d0,tc
  	movl	#0x80008000,d0
  	movc	d0,cacr			| turn on both caches
  	jra	Lloaddone
***************
*** 960,966 ****
  	jgt	LnokillTT
  
  	lea	longscratch,a0
! 	movl	#0, a0@
  	.long	0xF0100800		| movl a0@,tt0
  	.long	0xF0100C00		| movl a0@,tt1
  
--- 962,968 ----
  	jgt	LnokillTT
  
  	lea	longscratch,a0
! 	movl	#0,a0@
  	.long	0xF0100800		| movl a0@,tt0
  	.long	0xF0100C00		| movl a0@,tt1
  
***************
*** 1527,1537 ****
  	cmpl	#MMU_68040,_mmutype	| 68040?
  	jne	Lmotommu4		| no, skip
  	movc	dfc,d1
! 	moveq	#FC_USERD, d0		| user space
! 	movc	d0, dfc
  	.word	0xf508			| pflush a0@
  	moveq	#FC_SUPERD,d0		| supervisor space
! 	movc	d0, dfc
  	.word	0xf508			| pflush a0@
  	movc	d1,dfc
  	rts
--- 1529,1539 ----
  	cmpl	#MMU_68040,_mmutype	| 68040?
  	jne	Lmotommu4		| no, skip
  	movc	dfc,d1
! 	moveq	#FC_USERD,d0		| user space
! 	movc	d0,dfc
  	.word	0xf508			| pflush a0@
  	moveq	#FC_SUPERD,d0		| supervisor space
! 	movc	d0,dfc
  	.word	0xf508			| pflush a0@
  	movc	d1,dfc
  	rts
***************
*** 1874,1886 ****
  	.long	0x4e7a0003		| movec tc,d0
  	andw	#0x8000,d0
  	jeq	Lget_phys1		| MMU is disabled
! 	movc	sfc,d1
! 	movql	#1,d0			| FC for ptestr
! 	movc	d0,sfc
  	movl	sp@(4),a0		| logical address to look up
  	.word	0xf568			| ptestr (a0)
  	.long	0x4e7a0805		| movec mmusr,d0
! 	movc	d1,sfc
  	rts
  Lget_phys1:
  #endif
--- 1876,1888 ----
  	.long	0x4e7a0003		| movec tc,d0
  	andw	#0x8000,d0
  	jeq	Lget_phys1		| MMU is disabled
! 	movc	dfc,d1
! 	movl	sp@(8),d0		| FC for ptestr
! 	movc	d0,dfc
  	movl	sp@(4),a0		| logical address to look up
  	.word	0xf568			| ptestr (a0)
  	.long	0x4e7a0805		| movec mmusr,d0
! 	movc	d1,dfc
  	rts
  Lget_phys1:
  #endif
***************
*** 1918,1927 ****
   */
  	.globl	_get_pte
  _get_pte:
! 	addl	#-4,sp		| make temporary space
  	movl	sp@(8),a0	| logical address to look up
  	movl	#0,a1		| clear in case of failure
! 	ptestr	#1,a0@,#7,a1	| search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  	movl	sp@(16),a0	| where to store the psr
--- 1920,1934 ----
   */
  	.globl	_get_pte
  _get_pte:
! 	subql	#4,sp		| make temporary space
! 
! 	lea	longscratch,a0
! 	movl	#0x00ff8710,a0@	| Set up FC 1 r/w access
! 	.long	0xf0100800	| pmove a0@,tt0
! 
  	movl	sp@(8),a0	| logical address to look up
  	movl	#0,a1		| clear in case of failure
! 	ptestr	#FC_USERD,a0@,#7,a1 | search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  	movl	sp@(16),a0	| where to store the psr
***************
*** 1931,1948 ****
  	tstl	a1		| check address we got back
  	jeq	get_pte_fail2	| if 0, then was not set -- fail
  
- 	| enable tt0
  	movl	a1,d0
  	movl	d0,pte_tmp	| save for later
- 	andl	#0xff000000,d0	| keep msb
- 	orl	#0x00008707,d0	| enable tt for reading and writing
- 	movl	d0,longscratch
- 	lea	longscratch,a0
- 	.long	0xf0100800	| pmove a0@,tt0
  
  	| send first long back to user
  	movl	sp@(12),a0	| address of where to put pte
! 	movl	a1@,d0		|
  	movl	d0,a0@		| first long
  
  	andl	#3,d0		| dt bits of pte
--- 1938,1949 ----
  	tstl	a1		| check address we got back
  	jeq	get_pte_fail2	| if 0, then was not set -- fail
  
  	movl	a1,d0
  	movl	d0,pte_tmp	| save for later
  
  	| send first long back to user
  	movl	sp@(12),a0	| address of where to put pte
! 	movsl	a1@,d0		|
  	movl	d0,a0@		| first long
  
  	andl	#3,d0		| dt bits of pte
***************
*** 1979,2010 ****
  	movl	a0@,d0		| load high long
  	jra	pte_got_parent
  pte_level_one:
! 	ptestr	#1,a0@,#1,a1	| search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  	jra	pte_got_it
  pte_level_two:
! 	ptestr	#1,a0@,#2,a1	| search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  	jra	pte_got_it
  pte_level_three:
! 	ptestr	#1,a0@,#3,a1	| search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  	jra	pte_got_it
  pte_level_four:
! 	ptestr	#1,a0@,#4,a1	| search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  	jra	pte_got_it
  pte_level_five:
! 	ptestr	#1,a0@,#5,a1	| search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  	jra	pte_got_it
  pte_level_six:
! 	ptestr	#1,a0@,#6,a1	| search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  
--- 1980,2011 ----
  	movl	a0@,d0		| load high long
  	jra	pte_got_parent
  pte_level_one:
! 	ptestr	#FC_USERD,a0@,#1,a1 | search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  	jra	pte_got_it
  pte_level_two:
! 	ptestr	#FC_USERD,a0@,#2,a1 | search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  	jra	pte_got_it
  pte_level_three:
! 	ptestr	#FC_USERD,a0@,#2,a1 | search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  	jra	pte_got_it
  pte_level_four:
! 	ptestr	#FC_USERD,a0@,#4,a1 | search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  	jra	pte_got_it
  pte_level_five:
! 	ptestr	#FC_USERD,a0@,#5,a1 | search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  	jra	pte_got_it
  pte_level_six:
! 	ptestr	#FC_USERD,a0@,#6,a1 | search for logical address
  	pmove	psr,sp@		| store processor status register
  	movw	sp@,d1
  
***************
*** 2014,2028 ****
  	tstl	a1		| check address we got back
  	jeq	get_pte_fail6	| if 0, then was not set -- fail
  
! 	| change tt0
! 	movl	a1,d0
! 	andl	#0xff000000,d0	| keep msb
! 	orl	#0x00008707,d0	| enable tt for reading and writing
! 	movl	d0,longscratch
! 	lea	longscratch,a0
! 	.long	0xF0100800	| pmove a0@,tt0
! 
! 	movl	a1@,d0		| get pte of parent
  	movl	d0,_macos_tt0	| XXX for later analysis (kill this line)
  pte_got_parent:
  	andl	#3,d0		| dt bits of pte
--- 2015,2021 ----
  	tstl	a1		| check address we got back
  	jeq	get_pte_fail6	| if 0, then was not set -- fail
  
! 	movsl	a1@,d0		| get pte of parent
  	movl	d0,_macos_tt0	| XXX for later analysis (kill this line)
  pte_got_parent:
  	andl	#3,d0		| dt bits of pte
***************
*** 2036,2071 ****
  	|  is that the first long might have been the last long of RAM.
  
  	movl	pte_tmp,a1	| get address of our original pte
! 	addl	#4,a1		| address of ite second long
  
! 	| change tt0 back
! 	movl	a1,d0
! 	andl	#0xff000000,d0	| keep msb
! 	orl	#0x00008707,d0	| enable tt for reading and writing
! 	movl	d0,longscratch
! 	lea	longscratch,a0
! 	.long	0xF0100800	| pmove a0@,tt0
! 
! 	movl	sp@(12),a0	| address of return pte
! 	movl	a1@,a0@(4)	| write in second long
  
! 	movl	#1,d0		| return long-format
  	jra	get_pte_success
  
  short_format:
! 	movl	#0,d0		| return short-format
  	jra	get_pte_success
  
  get_pte_fail:
! 	movl	#-1,d0		| return failure
  
  get_pte_success:
! 	clrl	d1		| disable tt
! 	movl	d1,longscratch
! 	lea	longscratch,a0
! 	.long	0xF0100800	| pmove a0@,tt0
  
! 	addl	#4,sp		| return temporary space
  	rts
  
  get_pte_fail1:
--- 2029,2057 ----
  	|  is that the first long might have been the last long of RAM.
  
  	movl	pte_tmp,a1	| get address of our original pte
! 	addql	#4,a1		| address of ite second long
  
! 	| send second long back to user
! 	movl	sp@(12),a0	| address of where to put pte
! 	movsl	a1@,d0		|
! 	movl	d0,a0@(4)	| write in second long
  
! 	movql	#1,d0		| return long-format
  	jra	get_pte_success
  
  short_format:
! 	movql	#0,d0		| return short-format
  	jra	get_pte_success
  
  get_pte_fail:
! 	movql	#-1,d0		| return failure
  
  get_pte_success:
! 	lea	longscratch,a0	| disable tt
! 	movl	#0,a0@
! 	.long	0xf0100800	| pmove a0@,tt0
  
! 	addql	#4,sp		| return temporary space
  	rts
  
  get_pte_fail1:
Index: mac68k/machdep.c
===================================================================
RCS file: /cvsroot/src/sys/arch/mac68k/mac68k/machdep.c,v
retrieving revision 1.128
diff -c -r1.128 machdep.c
*** machdep.c	1996/11/19 07:17:49	1.128
--- machdep.c	1996/12/31 06:10:07
***************
*** 2335,2341 ****
  {
  	static int firstpass = 1;
  	int setup_mrg_vectors = 0;
- 	u_long phys;
  	struct cpu_model_info *cpui;
  
  	/*
--- 2335,2340 ----
***************
*** 2355,2371 ****
  		return;
  
  	/*
- 	 * Get the PA of the console framebuffer, iff we're on an 040.  
- 	 * We can't call get_physical() on the 851/030 this early, but
- 	 * neither can we call it later on the 040; by the time
- 	 * we've figured out we have an 851 or 030, we've disabled the
- 	 * 040 MMU.  XXX This sucks.
- 	 */
- 	if (mmutype == MMU_68040)
- 		mac68k_vidphys = get_physical(videoaddr, &phys) ?
- 		    phys : videoaddr;
- 
- 	/*
  	 * Set up any machine specific stuff that we have to before
  	 * ANYTHING else happens
  	 */
--- 2354,2359 ----
***************
*** 2554,2563 ****
     	3) restore regs
  */
  
! 	asm("movl a0, sp@-");
! 	asm("movl a1, sp@-");
! 	asm("movl d0, sp@-");
! 	asm("movl d1, sp@-");
  
  /* check to see if gray bars are turned off */
  	if (mac68k_machine.do_graybars) {
--- 2542,2551 ----
     	3) restore regs
  */
  
! 	asm volatile ("	movl a0, sp@-;
! 			movl a1, sp@-;
! 			movl d0, sp@-;
! 			movl d1, sp@-");
  
  /* check to see if gray bars are turned off */
  	if (mac68k_machine.do_graybars) {
***************
*** 2568,2582 ****
  		for (i = 0; i < 2 * videorowbytes / 4; i++)
  			((u_long *) videoaddr)[gray_nextaddr++] = 0x00000000;
  	}
! 	asm("movl sp@+, d1");
! 	asm("movl sp@+, d0");
! 	asm("movl sp@+, a1");
! 	asm("movl sp@+, a0");
  }
  #endif
  
  /* in locore */
! extern int ptest040 __P((u_int addr));
  extern int get_pte __P((u_int addr, u_long pte[2], u_short * psr));
  
  /*
--- 2556,2570 ----
  		for (i = 0; i < 2 * videorowbytes / 4; i++)
  			((u_long *) videoaddr)[gray_nextaddr++] = 0x00000000;
  	}
! 	asm volatile ("	movl sp@+, d1;
! 			movl sp@+, d0;
! 			movl sp@+, a1;
! 			movl sp@+, a0");
  }
  #endif
  
  /* in locore */
! extern int ptest040 __P((u_int addr, u_int fc));
  extern int get_pte __P((u_int addr, u_long pte[2], u_short * psr));
  
  /*
***************
*** 2594,2605 ****
  	extern u_int macos_tc;
  
  	if (mmutype == MMU_68040) {
! 		ph = ptest040(addr);
! 		if (!(ph & MMU40_RES))
! 			return 0;
  
! 		*phys = ph;
! 		mask = 0x00000fff;
  	} else {
  		i = get_pte(addr, pte, &psr);
  
--- 2582,2596 ----
  	extern u_int macos_tc;
  
  	if (mmutype == MMU_68040) {
! 		ph = ptest040(addr, FC_SUPERD);
! 		if ((ph & MMU40_RES) == 0) {
! 			ph = ptest040(addr, FC_USERD);
! 			if ((ph & MMU40_RES) == 0)
! 				return 0;
! 		}
  
! 		mask = (macos_tc & 0x4000) ? 0x00001fff : 0x00000fff;
! 		ph &= (~mask);
  	} else {
  		i = get_pte(addr, pte, &psr);
  
***************
*** 2633,2639 ****
  		 */
  		mask = (1 << (32 - numbits)) - 1;
  	}
! 	*phys = (ph & ~mask) | (addr & mask);
  
  	return 1;
  }
--- 2624,2630 ----
  		 */
  		mask = (1 << (32 - numbits)) - 1;
  	}
! 	*phys = ph + (addr & mask);
  
  	return 1;
  }
***************
*** 2811,2816 ****
--- 2802,2813 ----
  			 */
  			check_video("LC video (0x50f40000)",
  					512 * 1024, 512 * 1024);
+ 		} else if (0x50100100 <= videoaddr && videoaddr < 0x50400000) {
+ 			/*
+ 			 * Kludge for AV internal video
+ 			 */
+ 			check_video("AV video (0x50100100)", 1 * 1024 * 1024,
+ 						1 * 1024 * 1024);
  		} else {
  			printf( "  no internal video at address 0 -- "
  				"videoaddr is 0x%lx.\n", videoaddr);
***************
*** 2837,2851 ****
  	 * Be careful as we assume that no registers are clobbered
  	 * when we call this from assembly.
  	 */
! 	asm("movl a0, sp@-");
! 	asm("movl a1, sp@-");
! 	asm("movl d0, sp@-");
! 	asm("movl d1, sp@-");
  
  	/* printf("*"); */
  
! 	asm("movl sp@+, d1");
! 	asm("movl sp@+, d0");
! 	asm("movl sp@+, a1");
! 	asm("movl sp@+, a0");
  }
--- 2834,2848 ----
  	 * Be careful as we assume that no registers are clobbered
  	 * when we call this from assembly.
  	 */
! 	asm volatile ("	movl a0, sp@-;
! 			movl a1, sp@-;
! 			movl d0, sp@-;
! 			movl d1, sp@-");
  
  	/* printf("*"); */
  
! 	asm volatile ("	movl sp@+, d1;
! 			movl sp@+, d0;
! 			movl sp@+, a1;
! 			movl sp@+, a0");
  }
Index: mac68k/pmap_bootstrap.c
===================================================================
RCS file: /cvsroot/src/sys/arch/mac68k/mac68k/pmap_bootstrap.c,v
retrieving revision 1.29
diff -c -r1.29 pmap_bootstrap.c
*** pmap_bootstrap.c	1996/10/15 06:41:48	1.29
--- pmap_bootstrap.c	1996/12/31 06:10:09
***************
*** 570,576 ****
  	oldROMBase = ROMBase;
  	mac68k_vidphys = videoaddr;
  
! 	if ((tc & 0x80000000) && (mmutype == MMU_68030)) {
  		if (mac68k_machine.do_graybars)
  			printf("Getting mapping from MMU.\n");
  		(void) get_mapping();
--- 570,577 ----
  	oldROMBase = ROMBase;
  	mac68k_vidphys = videoaddr;
  
! 	if (((tc & 0x80000000) && (mmutype == MMU_68030)) ||
! 	    ((tc & 0x8000) && (mmutype == MMU_68040))) {
  		if (mac68k_machine.do_graybars)
  			printf("Getting mapping from MMU.\n");
  		(void) get_mapping();