wok rev 24067

linld: add cpu detection
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Jul 05 15:05:16 2021 +0000 (2021-07-05)
parents 68cf96abc146
children ef1efd2c8811
files linld/stuff/src/COMMON.H linld/stuff/src/CRTL.ASM linld/stuff/src/JUMP.ASM linld/stuff/src/LINLD.CPP linld/stuff/src/VCPI.ASM linld/stuff/src/_BEG.ASM linld/stuff/src/pipehole.awk
line diff
     1.1 --- a/linld/stuff/src/COMMON.H	Mon Jul 05 15:00:07 2021 +0000
     1.2 +++ b/linld/stuff/src/COMMON.H	Mon Jul 05 15:05:16 2021 +0000
     1.3 @@ -16,6 +16,7 @@
     1.4  #define LINUX001	linux 0.01 support
     1.5  #define ELKS		elks support
     1.6  #define IPXE		ipxe support
     1.7 +#define CPUTYPE		detect cpu family and model
     1.8  /* End of the optional features */
     1.9  
    1.10  #ifdef ISOHOOK
    1.11 @@ -108,12 +109,21 @@
    1.12  #define vid_mode	(cmdnum[1])	// -3 = ask, -2 = Extended VGA, -1 = Normal VGA,  n = as "n" was pressed
    1.13  #define topmem		(cmdnum[2])
    1.14  #define base_himem	(cmdnum[3])
    1.15 +#ifdef CPUTYPE
    1.16 +#define	skip_alloc	(* (char *) &cmdnum[5])
    1.17 +#define mincpu		(* (short *) &cmdnum[6])
    1.18 +extern unsigned cputype;
    1.19 +#else
    1.20  #define	skip_alloc	(* (char *) &cmdnum[4])
    1.21 +#endif
    1.22 +extern u32 cmdnum[5
    1.23 +#ifdef CPUTYPE
    1.24 +	+1
    1.25 +#endif
    1.26  #ifdef QUICK_BOOT
    1.27 -extern u32 cmdnum[7];
    1.28 -#else
    1.29 -extern u32 cmdnum[5];
    1.30 +	+2
    1.31  #endif
    1.32 +	];
    1.33  
    1.34  extern _fastcall void memcpy_image_kernel();
    1.35  extern _fastcall void memcpy_image_initrd();
     2.1 --- a/linld/stuff/src/CRTL.ASM	Mon Jul 05 15:00:07 2021 +0000
     2.2 +++ b/linld/stuff/src/CRTL.ASM	Mon Jul 05 15:05:16 2021 +0000
     2.3 @@ -952,8 +952,8 @@
     2.4  		dec	bx
     2.5  		sub	di,dx
     2.6  @@loop:
     2.7 +		add	di,dx
     2.8  		mov	si,cx			; s
     2.9 -		add	di,dx
    2.10  @@match:
    2.11  		lodsb				; *s++
    2.12  		or	al,20h			; locase
    2.13 @@ -969,12 +969,13 @@
    2.14  		cmp	[byte bx],'|'
    2.15  		je	@@testal
    2.16  		jl	@@notsw			; } ~ <del>
    2.17 +		;mov	dl,1
    2.18  		cmp	al,20h			; EOS or 20h
    2.19  		jne	@@loop			; assume [bx] = '|'
    2.20  @@notsw:
    2.21  		mov	al,-1
    2.22  		jne	@@notopt
    2.23 -		jmp	@@setnum
    2.24 +		jmp	@@setnum2
    2.25  @@testal:
    2.26  		cmp	al,-1
    2.27  		jz	@@loop
    2.28 @@ -1044,10 +1045,11 @@
    2.29  		endif
    2.30  @@end:
    2.31  @@setnum:
    2.32 +		mov	[di+2],dx
    2.33 +@@setnum2:
    2.34  		xchg	ax,si
    2.35 -		mov	[di+2],dx
    2.36  @@done:
    2.37 -		mov	[di],si
    2.38 +		or	[di],si
    2.39  		xchg	ax,di
    2.40  @@nokeyword:
    2.41  		pop	di si bx
     3.1 --- a/linld/stuff/src/JUMP.ASM	Mon Jul 05 15:00:07 2021 +0000
     3.2 +++ b/linld/stuff/src/JUMP.ASM	Mon Jul 05 15:05:16 2021 +0000
     3.3 @@ -25,36 +25,40 @@
     3.4  ;***************************************************************
     3.5  
     3.6  	macro	dos_shutdown
     3.7 -		xor	si,si
     3.8 +		xor	bx,bx
     3.9                  ifdef   QUICK_BOOT
    3.10  		extrn	_cmdnum:dword
    3.11 -quickboot	=	(word _cmdnum+20)
    3.12 -		cmp	si,[quickboot]
    3.13 -		mov	ds,si
    3.14 +	ifdef	CPUTYPE
    3.15 +quickboot	=	(byte _cmdnum+24)
    3.16 +	else
    3.17 +quickboot	=	(byte _cmdnum+20)
    3.18 +	endif
    3.19 +		cmp	bl,[quickboot]
    3.20 +		mov	ds,bx
    3.21  		jne	skip_shutdown
    3.22  		else
    3.23 -		mov	ds,si
    3.24 +		mov	ds,bx
    3.25  		endif
    3.26  		ifndef	NO386
    3.27 -		push	[dword si+4]		; save step
    3.28 -		mov	[word si+4],offset step19
    3.29 +		push	[dword bx+4]		; save step
    3.30 +		mov	[word bx+4],offset step19
    3.31  		else
    3.32  		mov	ax,offset step19
    3.33 -		xchg	ax,[word si+4]
    3.34 -		push	[word si+6]	
    3.35 +		xchg	ax,[word bx+4]
    3.36 +		push	[word bx+6]	
    3.37  		push	ax			; save step
    3.38  		endif
    3.39  		mov	[word cs:sssp],sp
    3.40 -		;cmp	[byte si+7],0F0h
    3.41 +		;cmp	[byte bx+7],0F0h
    3.42  		;jnc	notdos
    3.43 -		mov	[si+6],cs
    3.44 +		mov	[bx+6],cs
    3.45  		pushf
    3.46  		pushf
    3.47  		pop	ax
    3.48  		inc	ah			; set TF
    3.49  		push	ax
    3.50  		popf
    3.51 -		call	[dword si+4*19h]
    3.52 +		call	[dword bx+4*19h]
    3.53  notdos:
    3.54  		ifndef	NO386
    3.55  		lss	sp,[dword cs:sssp]
     4.1 --- a/linld/stuff/src/LINLD.CPP	Mon Jul 05 15:00:07 2021 +0000
     4.2 +++ b/linld/stuff/src/LINLD.CPP	Mon Jul 05 15:05:16 2021 +0000
     4.3 @@ -8,11 +8,6 @@
     4.4  
     4.5  extern char bzimagestr[];
     4.6  const char* cmdstr[4] = {"auto",bzimagestr,NULL,NULL};
     4.7 -#ifdef QUICK_BOOT
     4.8 -extern u32 cmdnum[7];
     4.9 -#else
    4.10 -extern u32 cmdnum[5];
    4.11 -#endif
    4.12  
    4.13  inline void syntax() {
    4.14      die("Syntax:" NL
    4.15 @@ -22,8 +17,8 @@
    4.16  #endif
    4.17  	"..." NL
    4.18          "vga mode: ask,extended,normal or dec/oct/hex number" NL
    4.19 -#ifdef CPU32
    4.20 -        "-32 for 386+" NL
    4.21 +#ifdef CPUTYPE
    4.22 +        "-m mincpu	family*256+model" NL
    4.23  #endif
    4.24  #ifdef CPU64
    4.25          "-64 for cpu64 only" NL
    4.26 @@ -40,6 +35,9 @@
    4.27          NL NL "Examples:" NL
    4.28  	"\tLINLD "
    4.29  	"-b 64m "
    4.30 +#ifdef CPUTYPE
    4.31 +	"-m 0x600 "
    4.32 +#endif
    4.33          "initrd=rootfs4.gz,rootfs3.gz,rootfs2.gz,rootfs1.gz "
    4.34  	"\"cl=root=/dev/null video=-32\""
    4.35  	NL "\tLINLD memtest"
    4.36 @@ -58,12 +56,6 @@
    4.37      if (!*++argv) syntax();
    4.38      const char **clp = &cmdline;
    4.39      do {
    4.40 -#ifdef CPU32
    4.41 -        if ((*(u16 *)*argv|2) == 0x332F) {	// -32 /32
    4.42 -	    extrn char cpu386;
    4.43 -	    if (cpu386 == 0) exit();
    4.44 -	}
    4.45 -#endif
    4.46  #ifdef CPU64
    4.47          if ((*(u16 *)*argv|2) == 0x362F) {	// -64 /64
    4.48  	    if (cpuhaslm() == 0) exit();
    4.49 @@ -74,11 +66,15 @@
    4.50  #else
    4.51  	if (argstr(*argv,"cl|image|initrd",clp) == (int) clp) continue;
    4.52  #endif
    4.53 +	if (argnum(*argv,"root|vga|mem|-b|"
    4.54 +#ifdef CPUTYPE
    4.55 +		"-m|"
    4.56 +#endif
    4.57 +		"-f~"
    4.58  #ifdef QUICK_BOOT
    4.59 -	if (argnum(*argv,"root|vga|mem|-b|-f~-q~-v~",&root_dev) >= (int) &base_himem) continue;
    4.60 -#else
    4.61 -	if (argnum(*argv,"root|vga|mem|-b|-f~",&root_dev) >= (int) &base_himem) continue;
    4.62 +		"-q~-v~"
    4.63  #endif
    4.64 +		,&root_dev) >= (int) &base_himem) continue;
    4.65  	if (fileexist(*argv) != -1) {
    4.66              kernel_name=*argv;
    4.67  	    continue;
    4.68 @@ -86,6 +82,9 @@
    4.69  	*clp = (const char *) heap_top +1;
    4.70  	strcatb((const char *) heap_top,*argv);
    4.71      } while (*++argv);
    4.72 +#ifdef CPUTYPE
    4.73 +    if (mincpu > cputype) exit();
    4.74 +#endif
    4.75      puts(*clp);
    4.76      asm{
    4.77  	xchg	ax,bx
     5.1 --- a/linld/stuff/src/VCPI.ASM	Mon Jul 05 15:00:07 2021 +0000
     5.2 +++ b/linld/stuff/src/VCPI.ASM	Mon Jul 05 15:05:16 2021 +0000
     5.3 @@ -351,7 +351,11 @@
     5.4                  ;mov     [bx+2],ss
     5.5                  ifdef   QUICK_BOOT
     5.6  		extrn	_cmdnum:dword
     5.7 +	ifdef	CPUTYPE
     5.8 +v86boot	=	(byte _cmdnum+28)
     5.9 +	else
    5.10  v86boot	=	(byte _cmdnum+24)
    5.11 +	endif
    5.12  		test	[v86boot],bh
    5.13  		jnz	@vm2rm_ret
    5.14  		endif
     6.1 --- a/linld/stuff/src/_BEG.ASM	Mon Jul 05 15:00:07 2021 +0000
     6.2 +++ b/linld/stuff/src/_BEG.ASM	Mon Jul 05 15:05:16 2021 +0000
     6.3 @@ -11,10 +11,12 @@
     6.4  	include "himem.inc"
     6.5  	include	"isostate.inc"
     6.6  
     6.7 +CMDNUMCNT	=	5
     6.8 +	ifdef	CPUTYPE
     6.9 +CMDNUMCNT	=	CMDNUMCNT+1
    6.10 +	endif
    6.11  	ifdef	QUICK_BOOT
    6.12 -CMDNUMCNT	=	7
    6.13 -	else
    6.14 -CMDNUMCNT	=	5
    6.15 +CMDNUMCNT	=	CMDNUMCNT+2
    6.16  	endif
    6.17  
    6.18  	macro	alloc_isostate
    6.19 @@ -78,6 +80,10 @@
    6.20          ;global  _bss_start:byte
    6.21          label   _bss_start byte
    6.22          global  stktop:byte
    6.23 +		ifdef	CPUTYPE
    6.24 +	global	_cputype:word
    6.25 +_cputype	dw	?
    6.26 +		endif
    6.27          global  _cpu386:byte
    6.28  _cpu386		db	?
    6.29  		ifdef	CPU64
    6.30 @@ -133,6 +139,13 @@
    6.31  ;***************************************************************
    6.32  
    6.33  ; Check for oldies
    6.34 +		ifndef	NO386
    6.35 +		ifdef	CPUTYPE
    6.36 +		mov	ax,100h
    6.37 +		mov	cl,32		; 186+ masks with 31
    6.38 +		shr	ax,cl
    6.39 +		endif
    6.40 +		endif
    6.41  		push	bx		; < 286 : flags[12..15] are forced 1
    6.42  		popf			; = 286 : flags[12..15] are forced 0, cld, cli
    6.43  		pushf			; > 286 : only flags[15] is forced 0
    6.44 @@ -142,8 +155,15 @@
    6.45  		mov	bx,offset msg_badcpu
    6.46                  js	godie   	;it is not a 386+, die
    6.47  		else
    6.48 +		ifdef	CPUTYPE
    6.49 +		jc	not286
    6.50 +		mov	ah,2
    6.51 +not286:
    6.52 +                js	jmp_endcpu86   	;it is not a 386+, try ELKS & co
    6.53 +		else
    6.54                  js	endcpu86   	;it is not a 386+, try ELKS & co
    6.55  		endif
    6.56 +		endif
    6.57                  p386
    6.58  		ifdef	VCPI
    6.59                  mov     edx,cs
    6.60 @@ -176,6 +196,7 @@
    6.61  godie:
    6.62  		call	near die
    6.63  		else
    6.64 +jmp_endcpu86:
    6.65  		jmp	endcpu86
    6.66  		endif
    6.67  
    6.68 @@ -219,7 +240,11 @@
    6.69  		pushfd
    6.70  		pop	dx
    6.71  		pop	ax
    6.72 +		ifdef	CPUTYPE
    6.73 +		xor	al,24h		; toggle CPUID feature bit & AC bit
    6.74 +		else
    6.75  		xor	al,20h		; toggle CPUID feature bit 21 (=> pentium+)
    6.76 +		endif
    6.77  		push	ax		;  (toggle AC: bit 18 => 486+)
    6.78  		push	dx
    6.79  		popfd
    6.80 @@ -227,9 +252,47 @@
    6.81  		pop	dx
    6.82  		pop	dx
    6.83  		xor	al,dl		; clear C
    6.84 +		ifdef	CPUTYPE
    6.85 +		cmp	al,20h
    6.86 +		mov	ax,0400h
    6.87 +		je	is486
    6.88 +		mov	ah,3
    6.89 +is486:
    6.90 +		jae	set_cputype
    6.91 +		xor	eax,eax
    6.92 +		inc	ax
    6.93 +		cpuid			; Basic cpuid (late 486 or Pentium)
    6.94 +		xor	eax,eax
    6.95 +		inc	ax
    6.96 +		cpuid			; again cause of Nasty EMM386s
    6.97 +		push	eax
    6.98 +		bt	edx,26		; sse2 bit
    6.99 +		jnc	baseInfo
   6.100 +		else
   6.101  		jne	@@no_cpuid	; CPUID feature bit changed ?
   6.102 -		mov	eax,80000001h	; Extended Processor Info and Feature Bits
   6.103 -		cpuid
   6.104 +		endif
   6.105 +		mov	eax,80000001h	; Extended Processor Info and Feature Bits (Pentium 4 or newer)
   6.106 +		cpuid			; Extended cpuid (Pentium 4)
   6.107 +		ifdef	CPUTYPE
   6.108 +baseInfo:
   6.109 +		pop	ax		; base IDs
   6.110 +		and	ax,0FF0h	; keep FamilyID & ModelID
   6.111 +		pop	cx		; extended IDs
   6.112 +		mov	bx,cx
   6.113 +		shr	bx,4		; extended FamilyID in bl
   6.114 +		shr	al,4		; set base ModelID
   6.115 +		cmp	ah,0Fh
   6.116 +		jz	SetModelID
   6.117 +		cmp	ah,06h
   6.118 +		jnz	ModelIDset
   6.119 +		db	3Dh		; cmp ax,0DC00h
   6.120 +SetModelID:
   6.121 +		add	ah,bl		; use extended FamilyID
   6.122 +		shl	cl,4
   6.123 +		or	al,cl		; use extended ModelID
   6.124 +ModelIDset:
   6.125 +set_cputype:
   6.126 +		endif
   6.127  		mov	dl,-1		; set 386 flag
   6.128  		ifdef	NO386
   6.129  		db	66h		; mov [_cpu_features],edx
   6.130 @@ -243,6 +306,9 @@
   6.131  		dec	[_cpu386]
   6.132  		endif
   6.133  endcpu86:
   6.134 +		ifdef	CPUTYPE
   6.135 +		mov	[_cputype],ax	; _cputype: FFMM or 0[1-4]00=80[1-4]86 or 0000=8086/8088
   6.136 +		endif
   6.137                  p8086
   6.138  
   6.139  ;***************************************************************
     7.1 --- a/linld/stuff/src/pipehole.awk	Mon Jul 05 15:00:07 2021 +0000
     7.2 +++ b/linld/stuff/src/pipehole.awk	Mon Jul 05 15:05:16 2021 +0000
     7.3 @@ -17,6 +17,12 @@
     7.4  	if (/add	di,2/) $0="	scasw	; " $0
     7.5  	if (/bx,di/ || /\[bp-2\]/) next
     7.6  	sub(/\[bx\],0/,"[di],0")
     7.7 +	if (/ptr @exit\$qv/) {
     7.8 +		if (gotexit) next
     7.9 +		print "call_exit:"
    7.10 +		gotexit=1
    7.11 +	}
    7.12 +	if (/short @1@422/ && gotexit) $0="	ja	call_exit"
    7.13  	if (islinld==1) {
    7.14  		if (/,word.*/) islinld=0
    7.15  		print "; " $0
    7.16 @@ -33,7 +39,7 @@
    7.17  			print "	mov	ax,word ptr [di]"
    7.18  			next
    7.19  		}
    7.20 -		if (/ax,word ptr/) next
    7.21 +		if (/ax,word ptr \[/) next
    7.22  	}
    7.23  	if (/buf_cmdline\+1/) {
    7.24  		islinld=5