flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Detecting DOSBox

Author
Thread Post new topic Reply to topic
smiddy



Joined: 31 Oct 2004
Posts: 559
smiddy
I've been working through my code and have an MZ exe I have developed to launch my OS. It launches on top of DOS and gets into protected mode and when told, exits back out to DOS. I have been testing it on DOSBox and DOSBox doesn't provide controller emulations, so in order to load files you are required to use DOS interrupts. Has anyone found a way to detect if you are running on top of DOSBox already, such that I can detect it a flag using it in my OS such that my OS acknowledges it is within DOSBox and thus will use DOS interrupts (where appropriate for file I/O etcetera)?

Are there memory locales I can scan to make the determination?

Right now I can detect being within DOS, and am close to determining if I'm in DOSBox since INT 13h doesn't work, and EDD is not available, PCI is not available, and falls through to DOS' functions for determining current disk. Is there something else that I can for certain rely on to help make the determination?

Smiddy
Post 03 Feb 2015, 03:58
View user's profile Send private message Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 204
ACP
Post 03 Feb 2015, 15:48
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 559
smiddy


Whoa, thanks! I was going to try a search like that. No reinventing the wheel. Much appreciated!

Smiddy
Post 03 Feb 2015, 19:24
View user's profile Send private message Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 204
ACP
You're welcome. The other possible technique for detecting the emulator is to actually use INT 1 to tunnel into BIOS and check how things are looking there. Actually DosBOX BIOS is pretty simple. You can even use Sourcer package to dump it and disassemble from DosBox prompt.
Post 03 Feb 2015, 22:34
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 559
smiddy
Thanks! I was going to look at the source next and see what I could delve from it. I hadn't known about INT 1 either, I'll take a close look there as well.

I don't know if these are helpful to you (or others), I think I post these way back when I wrote them:

Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; VM - Detects if we're in a virtual machine. Current virtual machines
;;      detected: Virtual PC, VMWare, and Bochs. I am developing ways to
;;      detect DOSBox and QEMU.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data Area for DetectVPC
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;align 4
VPCDetectedMessage      db  'Virtual PC Detected',13,10,0
VPCNotDetectedMessage   db  'Virtual PC NOT Detected',13,10,0
;align 4
VPCOn                   dd 0

DetectVPC:

    pusha
    
    cli                                     ; Turn off interrupts
    mov esi,VPCInvalidOpcodeException       ; Store new Invalid Opcode Exception
    mov eax,6                               ; Invalid Opcode is 6
    call AddExceptionToIDT                  ; Call routine to replace it
    sti                                     ; Turn on interrupts
    
    mov ebx,0                               ; This will stay 0 if VPC running
    mov eax,1                               ; VPC function number
    
.CallVPC:
    
    db 0Fh,3Fh,07h,0Bh                      ; Call VPC
    
    test ebx,ebx
    jz .InVPC
    mov eax,0
    mov esi,VPCNotDetectedMessage
    jmp .Done
    
.InVPC:

    mov eax,1
    mov esi,VPCDetectedMessage
    
.Done:

    mov [VPCOn],eax
    call PrintString32
    
    cli
    mov esi,IllegalInstruction              ; Restore original unhandled interrupt
    mov eax,6                               ; Invalid Opcode is 6
    call AddExceptionToIDT
    sti
    
    popa
    
    ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; VPCInvalidOpcodeException - replaced invalid opcode exception handler with
;;                             this one to go past the VPC call in the above
;;                             procedure.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

VPCInvalidOpcodeException:

    mov ebx,-1                              ; Not running VPC
    add DWORD [ss:esp],4                    ; Fix the EIP in stack to skip past call VPC
    
    iret
    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data area for SetectBochs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;align 4
BochsDetectedMessage    db 'Bochs detected',13,10,0
BochsNotDetectedMessage db 'Bochs NOT detected',13,10,0
;align 4
BochsOn                 dd 0

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DetectBochs - Detects if Bochs is running your code. This has been tested
;;               to work with standard release build for Windows environment.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

DetectBochs:

    mov dx,0E9h
    in al,dx
    
    cmp al,0E9h
    je .InBochs
    mov eax,0
    mov esi,BochsNotDetectedMessage
    jmp .Done
    
.InBochs:

    mov eax,1
    mov esi,BochsDetectedMessage
    
.Done:

    mov [BochsOn],eax
    call PrintString32
    
    ret
    

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data area for DetectVMWare
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;align 4
VMWareDetectedMessage       db  'VM Ware Detected',13,10,0
VMWareNotDetectedMessage    db  'VM Ware NOT Detected',13,10,0

;ALIGN 4
VMWareOn                    dd  0           ; Default = 0 = OFF, 1 = ON

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DetectVMWare - Detects if your code is running under VMWare
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

DetectVMWare:
    
    mov eax,564D5868h                       ; 'VMXh'
    mov ebx,12345678h                       ; This can be any number, but not 'VMXh'
    mov ecx,0Ah                             ; Get VMWare version
    mov edx,5658h                           ; Port number
    IN  eax,dx                              ; Read port 5658h
;    call ShowRegisters                     ; Shows EAX, EBX, ECX, and EDX (more later)
    cmp ebx,564D5868h                       ; Is this from the EAX?
    je .InVMWare                            ; Yes, goto flag it
    mov eax,0
    mov esi,VMWareNotDetectedMessage
    jmp .Done
    
.InVMWare:

    mov eax,1
    mov esi,VMWareDetectedMessage
    
.Done:

    mov [VMWareOn],eax
    call PrintString32
    
    ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data Area for DetectVPC
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;align 4
QEMUDetectedMessage      db  'QEMU Detected',13,10,0
QEMUNotDetectedMessage   db  'QEMU NOT Detected',13,10,0
;align 4
QEMUOn                   dd 0

DetectQEMU:

    pusha
    
    cli                                     ; Turn off interrupts
    mov esi,QEMUInvalidOpcodeException      ; Store new Invalid Opcode Exception
    mov eax,6                               ; Invalid Opcode handler is 6
    call AddExceptionToIDT                  ; Call routine to replace it
    sti                                     ; Turn on interrupts
    
    mov ebx,0                               ; This will stay 0 if VPC running
    mov eax,1                               ; VPC function number
    
.CallQEMU:
    
    db 0Fh,3Fh,07h,0Bh                      ; Call VPC
    
    test ebx,ebx
    jz .InQEMU
    mov eax,0
    mov esi,QEMUNotDetectedMessage
    jmp .Done
    
.InQEMU:

    mov eax,1
    mov esi,QEMUDetectedMessage
    
.Done:

    mov [QEMUOn],eax
    call PrintString32
    
    cli
    mov esi,IllegalInstruction              ; Restore original unhandled interrupt
    mov eax,6                               ; Invalid Opcode is 6
    call AddExceptionToIDT
    sti
    
    popa
    
    ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; QEMUInvalidOpcodeException - replaced invalid opcode exception handler with
;;                             this one to go past the VPC call in the above
;;                             procedure.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

QEMUInvalidOpcodeException:

    mov ebx,-1                              ; Not running VPC
    add DWORD [ss:esp],4                    ; Fix the EIP in stack to skip past call VPC
    
    iret
    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DetectDOSBox - Detects if we are running under DOSBox (stub)
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

DOSBoxMessage db 'DOSBox detection is not implemented, yet.',13,10,0

DetectDOSBox:

        mov esi,DOSBoxMessage
        call PrintString32
        
    


My coding is ugly, but effective. LOL! I haven't tested these lately, so they may not work with current versions. I will be testing them again very soon, once I get my boot drive working, which is why I needed to know DOSBox is loading the my OS, so I can load another file, VENDORS.TXT to check the PCI bus pool against that data.
Post 03 Feb 2015, 22:54
View user's profile Send private message Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 204
ACP
The code you see on the github has been developed for a DOS Extender in order to know which INTs should be extended (for example there is no point in installing extended int handler if the BIOS is not handling it anyway under emulator) and to be able to use internal DPMI host if emulator provides one. You are also detecting CPU emulators and hypervisor. From extender point of view hypervisor is transparent but for other uses your code is definitively useful. The rpix86 detection method has been discussed with it's author. Code has been tested and is known to be working.
Post 05 Feb 2015, 10:55
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.