Calling a NASM Procedure from C makes Bochs Restart

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Puffy
Member
Member
Posts: 26
Joined: Mon May 26, 2008 7:00 am

Calling a NASM Procedure from C makes Bochs Restart

Post by Puffy »

Hello there,

I have the following procedure in NASM (checks to see if you can use the CPUID instruction):

File: is_cpuid_supported.asm

Code: Select all

SECTION .data
global is_cpuid_supported
SECTION .text
is_cpuid_supported:
	push ebp
	mov ebp,esp
	pushfd
	popf
	mov eax,ecx
	xor  eax,200000h
	cmp eax,ecx
	jne cpuid_not_supported
cpuid_supported:
	mov eax,1
	jmp return_value
cpuid_not_supported:
	mov eax,0
return_value:
	push ecx
	mov esp,ebp
	pop ebp
	rtn
I then declare the function prototypes from a C header file (test.h):

Code: Select all

#ifndef TEST_H_
#define TEST_H_
extern int is_cpuid_supported(void);
#endif /*TEST_H_*/
And I try and call the procedure from my main function in C (main.c):

Code: Select all

#include <test.h>
#include <screen.h>

void kmain( void* mbd, unsigned int magic )
{
	initScreen();
	if (is_cpuid_supported()){
		printString("Yes");
	}else{
		printString("No");
	}
	for(;;);
}

(All of the screen / print procedues are defined and do work).

Every time I compile my kernel and try to boot it in bochs, it just restarts. I have no idea what I'm doing wrong!

Any ideas?

Cheers,

Puff
User avatar
Zenith
Member
Member
Posts: 224
Joined: Tue Apr 10, 2007 4:42 pm

Re: Calling a NASM Procedure from C makes Bochs Restart

Post by Zenith »

Can you give us the Bochs output when it reboots?

Also, I'd advise setting up a basic GDT/IDT before you read CPUID and such features. Also, you can inline the assembly so you can use assembly more reliably and also save a function call.
"Sufficiently advanced stupidity is indistinguishable from malice."
Puffy
Member
Member
Posts: 26
Joined: Mon May 26, 2008 7:00 am

Re: Calling a NASM Procedure from C makes Bochs Restart

Post by Puffy »

Here's bochs log file:

Code: Select all

00000000000i[     ] Bochs x86 Emulator 2.3.7
00000000000i[     ]   Build from CVS snapshot, on June 3, 2008
00000000000i[     ] System configuration
00000000000i[     ]   processors: 1 (cores=1, HT threads=1)
00000000000i[     ]   A20 line support: yes
00000000000i[     ]   APIC support: yes
00000000000i[     ] CPU configuration
00000000000i[     ]   level: 6
00000000000i[     ]   SMP support: no
00000000000i[     ]   FPU support: yes
00000000000i[     ]   MMX support: yes
00000000000i[     ]   SSE support: 2
00000000000i[     ]   CLFLUSH support: yes
00000000000i[     ]   VME support: yes
00000000000i[     ]   3dnow! support: no
00000000000i[     ]   PAE support: yes
00000000000i[     ]   PGE support: yes
00000000000i[     ]   PSE support: yes
00000000000i[     ]   x86-64 support: yes
00000000000i[     ]   SEP support: yes
00000000000i[     ]   MWAIT support: no
00000000000i[     ]   XSAVE support: no
00000000000i[     ]   AES support: no
00000000000i[     ] Optimization configuration
00000000000i[     ]   Guest2HostTLB support: yes
00000000000i[     ]   RepeatSpeedups support: yes
00000000000i[     ]   Icache support: yes
00000000000i[     ]   Trace cache support: yes
00000000000i[     ]   Fast function calls: yes
00000000000i[     ] Devices configuration
00000000000i[     ]   ACPI support: yes
00000000000i[     ]   NE2000 support: yes
00000000000i[     ]   PCI support: yes
00000000000i[     ]   SB16 support: yes
00000000000i[     ]   USB support: yes
00000000000i[     ]   VGA extension support: vbe cirrus
00000000000i[MEM0 ] allocated memory at 0xb5622008. after alignment, vector=0xb5623000
00000000000i[MEM0 ] 32.00MB
00000000000i[MEM0 ] rom at 0xfffe0000/131072 ('/usr/local/share/bochs/BIOS-bochs-latest')
00000000000i[MEM0 ] rom at 0xc0000/38400 ('/usr/local/share/bochs/VGABIOS-lgpl-latest')
00000000000i[APIC?] set APIC ID to 0
00000000000i[APIC0] 80686
00000000000i[APIC0] local apic in CPU apicid=00 initializing
00000000000i[     ] lt_dlhandle is 0x8daa898
00000000000i[PLGIN] loaded plugin libbx_unmapped.la
00000000000i[     ] lt_dlhandle is 0x8daada8
00000000000i[PLGIN] loaded plugin libbx_biosdev.la
00000000000i[     ] lt_dlhandle is 0x8dab398
00000000000i[PLGIN] loaded plugin libbx_cmos.la
00000000000i[     ] lt_dlhandle is 0x8dab520
00000000000i[PLGIN] loaded plugin libbx_dma.la
00000000000i[     ] lt_dlhandle is 0x8dabdf0
00000000000i[PLGIN] loaded plugin libbx_pic.la
00000000000i[     ] lt_dlhandle is 0x8dac148
00000000000i[PLGIN] loaded plugin libbx_vga.la
00000000000i[     ] lt_dlhandle is 0x8dac950
00000000000i[PLGIN] loaded plugin libbx_floppy.la
00000000000i[     ] lt_dlhandle is 0x8dac9b8
00000000000i[PLGIN] loaded plugin libbx_harddrv.la
00000000000i[     ] lt_dlhandle is 0x8dbee30
00000000000i[PLGIN] loaded plugin libbx_keyboard.la
00000000000i[     ] lt_dlhandle is 0x8d74928
00000000000i[PLGIN] loaded plugin libbx_serial.la
00000000000i[     ] lt_dlhandle is 0x8dbeec8
00000000000i[PLGIN] loaded plugin libbx_parallel.la
00000000000i[     ] lt_dlhandle is 0x8dc05e0
00000000000i[PLGIN] loaded plugin libbx_extfpuirq.la
00000000000i[     ] lt_dlhandle is 0x8dc0aa8
00000000000i[PLGIN] loaded plugin libbx_gameport.la
00000000000i[     ] lt_dlhandle is 0x8dc0fd0
00000000000i[PLGIN] loaded plugin libbx_speaker.la
00000000000i[     ] lt_dlhandle is 0x8dc14e8
00000000000i[PLGIN] loaded plugin libbx_pci.la
00000000000i[     ] lt_dlhandle is 0x8dc1678
00000000000i[PLGIN] loaded plugin libbx_pci2isa.la
00000000000i[     ] lt_dlhandle is 0x8dc1cb0
00000000000i[PLGIN] loaded plugin libbx_pci_ide.la
00000000000i[     ] lt_dlhandle is 0x8dc2358
00000000000i[PLGIN] loaded plugin libbx_acpi.la
00000000000i[IOAP ] initializing I/O APIC
00000000000i[IOAP ] set APIC ID to 1
00000000000i[MEM0 ] Register memory access handlers: 0xfec00000 - 0xfec00fff
00000000000i[CMOS ] Using local time for initial clock
00000000000i[CMOS ] Setting initial clock to: Tue Jul 22 22:35:54 2008 (time0=1216762554)
00000000000i[DMA  ] channel 4 used by cascade
00000000000i[DMA  ] channel 2 used by Floppy Drive
00000000000i[FDD  ] fd0: 'floppy.img' ro=0, h=2,t=80,spt=18
00000000000i[PCI  ] 440FX Host bridge present at device 0, function 0
00000000000i[PCI  ] PIIX3 PCI-to-ISA bridge present at device 1, function 0
00000000000i[MEM0 ] Register memory access handlers: 0x000a0000 - 0x000bffff
00000000000i[XGUI ] test_alloc_colors: 16 colors available out of 16 colors tried
00000000000i[XGUI ] font 8 wide x 16 high, display depth = 24
00000000000i[MEM0 ] Register memory access handlers: 0xe0000000 - 0xe07fffff
00000000000i[CLVGA] VBE Bochs Display Extension Enabled
00000000000i[CLVGA] interval=40000
00000000000i[PLGIN] init_mem of 'harddrv' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'keyboard' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'serial' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'parallel' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'gameport' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'speaker' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'pci_ide' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'acpi' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'harddrv' plugin device by virtual method
00000000000i[HD   ] Using boot sequence floppy, none, none
00000000000i[HD   ] Floppy boot signature check is enabled
00000000000i[PLGIN] init_dev of 'keyboard' plugin device by virtual method
00000000000i[KBD  ] will paste characters every 1000 keyboard ticks
00000000000i[PLGIN] init_dev of 'serial' plugin device by virtual method
00000000000i[SER  ] com1 at 0x03f8 irq 4
00000000000i[PLGIN] init_dev of 'parallel' plugin device by virtual method
00000000000i[PAR  ] parallel port 1 at 0x0378 irq 7
00000000000i[PLGIN] init_dev of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'gameport' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'speaker' plugin device by virtual method
00000000000i[SPEAK] Failed to open /dev/console: No such file or directory
00000000000i[SPEAK] Deactivating beep on console
00000000000i[PLGIN] init_dev of 'pci_ide' plugin device by virtual method
00000000000i[PCI  ] PIIX3 PCI IDE controller present at device 1, function 1
00000000000i[PLGIN] init_dev of 'acpi' plugin device by virtual method
00000000000i[PCI  ] ACPI Controller present at device 1, function 3
00000000000i[PLGIN] register state of 'harddrv' plugin device by virtual method
00000000000i[PLGIN] register state of 'keyboard' plugin device by virtual method
00000000000i[PLGIN] register state of 'serial' plugin device by virtual method
00000000000i[PLGIN] register state of 'parallel' plugin device by virtual method
00000000000i[PLGIN] register state of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] register state of 'gameport' plugin device by virtual method
00000000000i[PLGIN] register state of 'speaker' plugin device by virtual method
00000000000i[PLGIN] register state of 'pci_ide' plugin device by virtual method
00000000000i[PLGIN] register state of 'acpi' plugin device by virtual method
00000000000i[SYS  ] bx_pc_system_c::Reset(HARDWARE) called
00000000000i[CPU0 ] cpu hardware reset
00000000000i[APIC0] local apic in CPU 0 initializing
00000000000i[PLGIN] reset of 'harddrv' plugin device by virtual method
00000000000i[PLGIN] reset of 'keyboard' plugin device by virtual method
00000000000i[PLGIN] reset of 'serial' plugin device by virtual method
00000000000i[PLGIN] reset of 'parallel' plugin device by virtual method
00000000000i[PLGIN] reset of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] reset of 'gameport' plugin device by virtual method
00000000000i[PLGIN] reset of 'speaker' plugin device by virtual method
00000000000i[PLGIN] reset of 'pci_ide' plugin device by virtual method
00000000000i[PLGIN] reset of 'acpi' plugin device by virtual method
00000000000i[XGUI ] [x] Mouse off
00000003302i[BIOS ] $Revision: 1.209 $ $Date: 2008/06/02 20:08:10 $
00000080000e[CLVGA] character height = 1, skipping text update
00000160000e[CLVGA] character height = 1, skipping text update
00000240000e[CLVGA] character height = 1, skipping text update
00000317069i[KBD  ] reset-disable command received
00000320000e[CLVGA] character height = 1, skipping text update
00000400000e[CLVGA] character height = 1, skipping text update
00000436653i[VBIOS] VGABios $Id: vgabios.c,v 1.67 2008/01/27 09:44:12 vruppert Exp $

00000436724i[CLVGA] VBE known Display Interface b0c0
00000436756i[CLVGA] VBE known Display Interface b0c4
00000439681i[VBIOS] VBE Bios $Id: vbe.c,v 1.60 2008/03/02 07:47:21 vruppert Exp $
00000560000i[XGUI ] charmap update. Font Height is 16
00000762682i[BIOS ] Starting rombios32
00000763509i[BIOS ] ram_size=0x02000000
00000784058i[BIOS ] Found 1 cpu(s)
00000800359i[BIOS ] bios_table_addr: 0x000fb778 end=0x000fcc00
00000800426i[PCI  ] 440FX PMC write to PAM register 59 (TLB Flush)
00001259233i[PCI  ] 440FX PMC write to PAM register 59 (TLB Flush)
00001718720i[P2I  ] PCI IRQ routing: PIRQA# set to 0x0b
00001718767i[P2I  ] PCI IRQ routing: PIRQB# set to 0x09
00001718814i[P2I  ] PCI IRQ routing: PIRQC# set to 0x0b
00001718861i[P2I  ] PCI IRQ routing: PIRQD# set to 0x09
00001718877i[P2I  ] write: ELCR2 = 0x0a
00001719814i[BIOS ] PIIX3 init: elcr=00 0a
00001740069i[BIOS ] PCI: bus=0 devfn=0x00: vendor_id=0x8086 device_id=0x1237
00001743306i[BIOS ] PCI: bus=0 devfn=0x08: vendor_id=0x8086 device_id=0x7000
00001746039i[BIOS ] PCI: bus=0 devfn=0x09: vendor_id=0x8086 device_id=0x7010
00001746500i[PIDE ] new BM-DMA address: 0xc000
00001747383i[BIOS ] region 4: 0x0000c000
00001749959i[BIOS ] PCI: bus=0 devfn=0x0b: vendor_id=0x8086 device_id=0x7113
00001750459i[ACPI ] new irq line = 11
00001750496i[ACPI ] new PM base address: 0xb000
00001750558i[ACPI ] new SM base address: 0xb100
00001751026i[CPU0 ] Enter to System Management Mode
00001751036i[CPU0 ] RSM: Resuming from System Management Mode
00001751070i[PCI  ] setting SMRAM control register to 0x4a
00001751352i[PCI  ] setting SMRAM control register to 0x0a
00001774633i[BIOS ] MP table addr=0x000fb850 MPC table addr=0x000fb780 size=0xd0
00001776836i[BIOS ] SMBIOS table addr=0x000fb860
00001779880i[BIOS ] ACPI tables: RSDP addr=0x000fb970 ACPI DATA addr=0x01ff0000 size=0x9d8
00001799413i[PCI  ] 440FX PMC write to PAM register 59 (TLB Flush)
00001800425i[BIOS ] bios_table_cur_addr: 0x000fb994
00008023335i[BIOS ] Booting from 0000:7c00
00012720441i[BIOS ] int13_harddisk: function 41, unmapped device for ELDL=80
00012725222i[BIOS ] int13_harddisk: function 08, unmapped device for ELDL=80
00012729879i[BIOS ] *** int 15h function AX=00c0, BX=0000 not yet supported!
00121951525i[FDD  ] partial read() on floppy image returns 142/512
00121974258i[FDD  ] read() on floppy image returns 0
00121996991i[FDD  ] read() on floppy image returns 0
00153999146i[CPU0 ] LOCK prefix unallowed (op1=0x53, attr=0x0, mod=0x0, nnn=0)
00153999148i[CPU0 ] LOCK prefix unallowed (op1=0x53, attr=0x0, mod=0x0, nnn=0)
00153999148i[CPU0 ] CPU is in protected mode (active)
00153999148i[CPU0 ] CS.d_b = 32 bit
00153999148i[CPU0 ] SS.d_b = 32 bit
00153999148i[CPU0 ] EFER   = 0x00000000
00153999148i[CPU0 ] | RAX=000000000000000f  RBX=000000000002bd20
00153999148i[CPU0 ] | RCX=000000000001c300  RDX=000000000002be00
00153999148i[CPU0 ] | RSP=000000000010100c  RBP=0000000000067f20
00153999148i[CPU0 ] | RSI=000000000002be78  RDI=00000000fffffe80
00153999148i[CPU0 ] |  R8=0000000000000000   R9=0000000000000000
00153999148i[CPU0 ] | R10=0000000000000000  R11=0000000000000000
00153999148i[CPU0 ] | R12=0000000000000000  R13=0000000000000000
00153999148i[CPU0 ] | R14=0000000000000000  R15=0000000000000000
00153999148i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df IF tf sf zf af PF cf
00153999148i[CPU0 ] | SEG selector     base    limit G D
00153999148i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00153999148i[CPU0 ] |  CS:0008( 0001| 0|  0) 00000000 000fffff 1 1
00153999148i[CPU0 ] |  DS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00153999148i[CPU0 ] |  SS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00153999148i[CPU0 ] |  ES:0010( 0002| 0|  0) 00000000 000fffff 1 1
00153999148i[CPU0 ] |  FS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00153999148i[CPU0 ] |  GS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00153999148i[CPU0 ] |  MSR_FS_BASE:0000000000000000
00153999148i[CPU0 ] |  MSR_GS_BASE:0000000000000000
00153999148i[CPU0 ] | RIP=0000000000000003 (0000000000000003)
00153999148i[CPU0 ] | CR0=0x60000011 CR1=0x0 CR2=0x0000000000000000
00153999148i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00153999148i[CPU0 ] >> lock push ebx : F053
00153999148e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
00153999148i[SYS  ] bx_pc_system_c::Reset(SOFTWARE) called
00153999148i[CPU0 ] cpu software reset
00153999148i[APIC0] local apic in CPU 0 initializing
00154002451i[BIOS ] $Revision: 1.209 $ $Date: 2008/06/02 20:08:10 $
00154386571i[KBD  ] reset-disable command received
00154506155i[VBIOS] 
VGABios $Id: vgabios.c,v 1.67 2008/01/27 09:44:12 vruppert Exp $

00154506226i[CLVGA] VBE known Display Interface b0c0
00154506258i[CLVGA] VBE known Display Interface b0c4
00154509183i[VBIOS] VBE Bios $Id: vbe.c,v 1.60 2008/03/02 07:47:21 vruppert Exp $
00154560000i[XGUI ] charmap update. Font Height is 16
00154640000i[XGUI ] charmap update. Font Height is 16
00154832184i[BIOS ] Starting rombios32
00154833011i[BIOS ] ram_size=0x02000000
00154853560i[BIOS ] Found 1 cpu(s)
00154869861i[BIOS ] bios_table_addr: 0x000fb778 end=0x000fcc00
00154869928i[PCI  ] 440FX PMC write to PAM register 59 (TLB Flush)
00155328735i[PCI  ] 440FX PMC write to PAM register 59 (TLB Flush)
00155789316i[BIOS ] PIIX3 init: elcr=00 0a
00155809571i[BIOS ] PCI: bus=0 devfn=0x00: vendor_id=0x8086 device_id=0x1237
00155812808i[BIOS ] PCI: bus=0 devfn=0x08: vendor_id=0x8086 device_id=0x7000
00155815541i[BIOS ] PCI: bus=0 devfn=0x09: vendor_id=0x8086 device_id=0x7010
00155816885i[BIOS ] region 4: 0x0000c000
00155819461i[BIOS ] PCI: bus=0 devfn=0x0b: vendor_id=0x8086 device_id=0x7113
00155843428i[BIOS ] MP table addr=0x000fb850 MPC table addr=0x000fb780 size=0xd0
00155845631i[BIOS ] SMBIOS table addr=0x000fb860
00155848675i[BIOS ] ACPI tables: RSDP addr=0x000fb970 ACPI DATA addr=0x01ff0000 size=0x9d8
00155868208i[PCI  ] 440FX PMC write to PAM register 59 (TLB Flush)
00155869220i[BIOS ] bios_table_cur_addr: 0x000fb994
00162342491i[BIOS ] Booting from 0000:7c00
00167039507i[BIOS ] int13_harddisk: function 41, unmapped device for ELDL=80
00167044288i[BIOS ] int13_harddisk: function 08, unmapped device for ELDL=80
00167048945i[BIOS ] *** int 15h function AX=00c0, BX=0000 not yet supported!
00234614000p[XGUI ] >>PANIC<< POWER button turned off.
00234614000i[CPU0 ] CPU is in real mode (active)
00234614000i[CPU0 ] CS.d_b = 16 bit
00234614000i[CPU0 ] SS.d_b = 16 bit
00234614000i[CPU0 ] EFER   = 0x00000000
00234614000i[CPU0 ] | RAX=0000000000000000  RBX=000000000002001e
00234614000i[CPU0 ] | RCX=0000000000008e06  RDX=0000000000000706
00234614000i[CPU0 ] | RSP=0000000000001fda  RBP=0000000000001ff0
00234614000i[CPU0 ] | RSI=000000000002be78  RDI=000000000001eb61
00234614000i[CPU0 ] |  R8=0000000000000000   R9=0000000000000000
00234614000i[CPU0 ] | R10=0000000000000000  R11=0000000000000000
00234614000i[CPU0 ] | R12=0000000000000000  R13=0000000000000000
00234614000i[CPU0 ] | R14=0000000000000000  R15=0000000000000000
00234614000i[CPU0 ] | IOPL=0 id vip vif ac vm rf nt of df IF tf sf ZF af PF cf
00234614000i[CPU0 ] | SEG selector     base    limit G D
00234614000i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00234614000i[CPU0 ] |  CS:f000( 0003| 0|  0) 000f0000 0000ffff 0 0
00234614000i[CPU0 ] |  DS:0040( 0004| 0|  0) 00000400 0000ffff 0 0
00234614000i[CPU0 ] |  SS:0000( 0004| 0|  0) 00000000 0000ffff 0 0
00234614000i[CPU0 ] |  ES:0000( 0004| 0|  0) 00000000 0000ffff 0 0
00234614000i[CPU0 ] |  FS:0000( 0004| 0|  0) 00000000 0000ffff 0 0
00234614000i[CPU0 ] |  GS:0000( 0004| 0|  0) 00000000 0000ffff 0 0
00234614000i[CPU0 ] |  MSR_FS_BASE:0000000000000000
00234614000i[CPU0 ] |  MSR_GS_BASE:0000000000000000
00234614000i[CPU0 ] | RIP=000000000000e86c (000000000000e86c)
00234614000i[CPU0 ] | CR0=0x60000010 CR1=0x0 CR2=0x0000000000000000
00234614000i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00234614000i[CPU0 ] >> nop  : 90
00234614000i[CMOS ] Last time is 1216762671 (Tue Jul 22 22:37:51 2008)
00234614000i[XGUI ] Exit
00234614000i[     ] restoring default signal behavior
00234614000i[CTRL ] quit_sim called with exit code 1
I have thought about inline assembly but my only concern is that then my Kernel will be using two different assemblers (NASM and GAS). If there is a way to use NASM syntax inline assembly that would be brilliant. Still, I'd like to know why this isn't working more so.
jnc100
Member
Member
Posts: 777
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Calling a NASM Procedure from C makes Bochs Restart

Post by jnc100 »

You seem to be jumping to address 0x0 and trying to execute code from there. Usual culprits are messing up the stack before a ret and a problem with your toolchain (or options to it) that cause the relocation for the external function call to be incorrect. I don't think its your problem, but why are you doing a push ecx near the end of the function?

You can use Intel (NASM) syntax in inline assembly in gcc. I've never done it but as I gather the syntax is:

Code: Select all

asm {
  .intel_syntax
  mov dest, src
  .att_syntax
}
I suggest (to solve your original problem) you use bochs debugger and step through the call to the inline function to find out whether the error is on the call or on the return.

Regards,
John.

edit: actually, your whole test for cpuid is wrong. You need to test whether you are able to both set and clear bit 21 of EFLAGS, e.g. (entirely untested)

edit2: fixed line 7

Code: Select all

id EQU 1 << 21
mov eax, 1

; set id
pushf
pop edx
or edx, id
push edx
popf

; test is set
pushf
pop edx
test edx, id
jz .no_cpuid

; unset id
xor edx, id
push edx
popf

; test not set
pushf
pop edx
xor edx, id
test edx, id
jz .no_cpuid

; set id
push edx
popf

; test set
pushf
pop edx
test edx, id
jz .no_cpuid

jmp .have_cpuid

.no_cpuid: xor eax, eax
.have_cpuid: ret
Last edited by jnc100 on Wed Jul 23, 2008 12:40 am, edited 1 time in total.
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: Calling a NASM Procedure from C makes Bochs Restart

Post by suthers »

Yah, had a similar problem recently, I fixed it simply by getting a cross compiler, before that all my pointers to ASM functions either came out at random addresses or at 0x0 (though I guess that's a random address to...)
Jules
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: Calling a NASM Procedure from C makes Bochs Restart

Post by Brynet-Inc »

It would seem your code isn't valid..

The steps to determining if CPUID is supported are as follows:

1) Push EFLAGS register onto stack.
2) Set bit 21.
3) Pop EFLAGS register.
4) Repeat step 1.
5) If bit 21 is still set, cpuid is supported.

The following should work.. it's unoptimized of coarse, but let us know how it goes. ;)

Code: Select all

SECTION .data
global is_cpuid_supported
SECTION .text
is_cpuid_supported:
   push ebp
   mov ebp,esp
   pushfd ; push EFLAGS reg onto stack.
   pop ecx
   xor ecx,200000h ; set bit 21.
   push ecx
   popf ; pop modified EFLAGS.
   pushfd ; ..bring it back out to play.
   pop ecx
   and ecx,200000h ; is bit 21 set?
   test ecx,ecx
   jz .cpuid_not_supported
.cpuid_supported:
   mov eax,1
   jmp .return_value
.cpuid_not_supported:
   mov eax,0
.return_value:
   leave ; Leave procedure, mov esp,ebp; + pop ebp;
   ret
Personally, I would also change the return values... 0 for success, 1 for fail.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
jnc100
Member
Member
Posts: 777
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Calling a NASM Procedure from C makes Bochs Restart

Post by jnc100 »

@Brynet: your code assumes that if cpuid isn't supported then:
1) you cannot set the ID bit (correct)
2) by default it is unset (this is undefined)

The only way I can see that properly tests it (given the rather vague description in the Intel manuals) is to prove that:
1) you can set it from a known unset state
2) you can clear it from a known set state

I've made a bit of a fix to my code above (and -> or at line 7). It was late at night when I wrote it...
And (my own personal preference), if you use 1 for success, 0 for failure, then in C you can simply do 'if(cpuid_is_supported()) cpuid_functions();'

Regards,
John.
Puffy
Member
Member
Posts: 26
Joined: Mon May 26, 2008 7:00 am

Re: Calling a NASM Procedure from C makes Bochs Restart

Post by Puffy »

Thanks for all your help. I've knocked together the following function, using some of the code here as a reference (I didn't straight copy/paste but I did check back to see if my code worked in much the same way).

I'm pretty sure this function works, but could one of you check it over. It doesn't crash bochs, at least :).

Edit: Encountered a small problem when calling this function, added 'local_' prefix and '%=' suffix to labels to resolve it (gives all the lables a unique number)

Code: Select all

int isCPUIDSupported()
{
	int id_flag = 1 << 21;
	int return_value;
	__asm__ __volatile__(
	".intel_syntax noprefix;" //Change from AT&T syntax to intel syntax
	"pushfd;" //Push EFLAGS register to the stack
	"pop ecx;" //Pop EFLAGS into ecx
	"mov edx,ecx;" //Store original EFLAGS in edx
	"xor ecx,%1;" //Try to set the ID flag of EFLAGS
	"push ecx;" 
	"popf;" //Set EFLAGS to our modified version
	"pushfd;" //Push EFLAGS to stack
	"pop ecx;" //Pop EFLAGS into ecx
	"cmp edx,ecx;" //Compare 'new' EFLAGS to original
	"jne local_cpuid_is_supported%=;" //If edx and ecx differ, ID is set
	"local_cpuid_is_not_supported%=:;"
	"mov %0,0;" //Set 'return_value' to 0
	"jmp local_return_value%=;"
	"local_cpuid_is_supported%=:;"
	"push edx;" //Push original EFLAGS to stack
	"popf;" //Restore EFLAGS
	"mov %0,1;" //Set 'return_value' to 1
	"local_return_value%=:;"
	".att_syntax;" : "=r" (return_value): "r" (id_flag)
	);
	return return_value;
}

Last edited by Puffy on Wed Jul 23, 2008 1:09 pm, edited 1 time in total.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Re: Calling a NASM Procedure from C makes Bochs Restart

Post by inflater »

Well, sorry for late reply, but I use this method for detecting CPUID:

Code: Select all

(global variables)
TestInvalidOpcode db 0
...
your INT6 handler:
invalid_instruction:
	cmp [TestInvalidOpcode],1                     ;did user expected INT6 to fire?
	je Test_IOC
	... do normal INT6 fault handler here ... end it up with CLI and HLT or JMP $
Test_IOC:
	mov [TestInvalidOpcode],0                     ;zero the flag
	add dword [esp],4                                ;fix the EIP to continue AFTER the faulty opcode
	iret                                                      ;return back
...
your checking routine:
	mov [TestInvalidOpcode],1
	cpuid
	cmp [TestInvalidOpcode],1
	je CPUIDisSupported
	... CPUID not supported ...
This can be used to detect e.g. MMX, SSE, 3DNow instructions etc.
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Calling a NASM Procedure from C makes Bochs Restart

Post by thepowersgang »

Just checking, do you have --fno-leading-underscore set when compiling?

Usually C requires a leading underscore on variable and function names to be able to reference them.
That means that in assembler you should define cpuid as _getCPUID and reference it in c as void getCPUID();
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: Calling a NASM Procedure from C makes Bochs Restart

Post by suthers »

It would even link if that was a problem, let alone run...
Jules
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Calling a NASM Procedure from C makes Bochs Restart

Post by thepowersgang »

try decompiling the final binary and finding if and where the function is referenced.
That could give you and idea for a breakpoint to set in bochs
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Post Reply