Subversion Repositories Kolibri OS

Rev

Rev 9969 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;
  3. ;; Copyright (C) KolibriOS team 2004-2024. All rights reserved.
  4. ;; Kernel programmers are acknowledged in CREDITS.TXT
  5. ;;
  6. ;; Data in this file was originally part of MenuetOS project which is
  7. ;; distributed under the terms of GNU GPL. It is modified and redistributed as
  8. ;; part of KolibriOS project under the terms of GNU GPL.
  9. ;;
  10. ;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa
  11. ;;
  12. ;; KolibriOS is distributed in the hope that it will be useful, but WITHOUT ANY
  13. ;; WARRANTY. No author or distributor accepts responsibility to anyone for the
  14. ;; consequences of using it or for whether it serves any particular purpose or
  15. ;; works at all, unless he says so in writing. Refer to the GNU General Public
  16. ;; License (the "GPL") for full details.
  17. ;
  18. ;; Everyone is granted permission to copy, modify and redistribute KolibriOS,
  19. ;; but only under the conditions described in the GPL. A copy of this license
  20. ;; is supposed to have been given to you along with KolibriOS so you can know
  21. ;; your rights and responsibilities. It should be in a file named COPYING.
  22. ;; Among other things, the copyright notice and this notice must be preserved
  23. ;; on all copies.
  24. ;;
  25. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  26.  
  27. format binary as "mnt"
  28.  
  29. include 'macros.inc'
  30. include 'struct.inc'
  31.  
  32. $Revision: 10002 $
  33.  
  34.  
  35. USE_COM_IRQ     = 1      ; make irq 3 and irq 4 available for PCI devices
  36. VESA_1_2_VIDEO  = 0      ; enable vesa 1.2 bank switch functions
  37.  
  38. ; Enabling the next line will enable serial output console
  39. ;debug_com_base  = 0x3f8  ; 0x3f8 is com1, 0x2f8 is com2, 0x3e8 is com3,
  40.                           ; 0x2e8 is com4, no irq's are used
  41.  
  42. include "proc32.inc"
  43. include "kglobals.inc"
  44. include "lang.inc"
  45. include "encoding.inc"
  46.  
  47. include "const.inc"
  48.  
  49. iglobal
  50. ; The following variable, if equal to 1, duplicates debug output to the screen.
  51. debug_direct_print db 0
  52. ; Start the first app (LAUNCHER) after kernel is loaded? (1=yes, 2 or 0=no)
  53. launcher_start db 1
  54. endg
  55.  
  56. max_processes  =  255
  57. tss_step       =  128 + 8192 ; tss & i/o - 65535 ports, * 256=557056*4
  58.  
  59. os_stack       =  os_data_l - gdts    ; GDTs
  60. os_code        =  os_code_l - gdts
  61. graph_data     =  3 + graph_data_l - gdts
  62. tss0           =  tss0_l - gdts
  63. app_code       =  3 + app_code_l - gdts
  64. app_data       =  3 + app_data_l - gdts
  65. app_tls        =  3 + tls_data_l - gdts
  66. pci_code_sel   =  pci_code_32-gdts
  67. pci_data_sel   =  pci_data_32-gdts
  68.  
  69.  
  70. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  71. ;;
  72. ;;   Included files:
  73. ;;
  74. ;;   Kernel16.inc
  75. ;;    - Booteng.inc   English text for bootup
  76. ;;    - Bootcode.inc  Hardware setup
  77. ;;    - Pci16.inc     PCI functions
  78. ;;
  79. ;;   Kernel32.inc
  80. ;;    - Sys32.inc     Process management
  81. ;;    - Shutdown.inc  Shutdown and restart
  82. ;;    - Fat32.inc     Read / write hd
  83. ;;    - Vesa12.inc    Vesa 1.2 driver
  84. ;;    - Vesa20.inc    Vesa 2.0 driver
  85. ;;    - Vga.inc       VGA driver
  86. ;;    - Stack.inc     Network interface
  87. ;;    - Mouse.inc     Mouse pointer
  88. ;;    - Scincode.inc  Window skinning
  89. ;;    - Pci32.inc     PCI functions
  90. ;;
  91. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  92.  
  93. ; That is a loading and initialization code that also draws the blue screen
  94. ; menu with svn revision number near top right corner of the screen.
  95. include "bootbios.inc"
  96.  
  97. use32
  98. org $+KERNEL_BASE
  99.  
  100. align 4
  101. B32:
  102.         mov     ax, os_stack       ; Selector for os
  103.         mov     ds, ax
  104.         mov     es, ax
  105.         mov     fs, ax
  106.         mov     gs, ax
  107.         mov     ss, ax
  108.         mov     esp, TMP_STACK_TOP       ; Set stack
  109.  
  110. ; CLEAR 0x280000 - HEAP_BASE
  111.  
  112.         xor     eax, eax
  113.         mov     edi, CLEAN_ZONE
  114.         mov     ecx, (HEAP_BASE - OS_BASE - CLEAN_ZONE) / 4
  115.         cld
  116.         rep stosd
  117.  
  118. ; Clear kernel undefined globals and some more (what exactly?)
  119.         mov     edi, endofcode - OS_BASE
  120.         mov     ecx, 0x90000    ; why uglobals_size isn't enough?
  121.         sub     ecx, edi
  122.         shr     ecx, 2
  123.         rep stosd
  124.  
  125. ; SAVE & CLEAR 0-0xffff
  126.  
  127.         mov     edi, 0x1000
  128.         mov     ecx, 0x8000 / 4
  129.         rep stosd
  130.         mov     edi, 0xa000
  131.         mov     ecx, 0x6000 / 4
  132.         rep stosd
  133.  
  134.         call    test_cpu
  135.         bts     [cpu_caps - OS_BASE], CAPS_TSC    ;force use rdtsc
  136.  
  137.         call    acpi_locate
  138.         call    init_BIOS32
  139. ; MEMORY MODEL
  140.         call    mem_test
  141.         call    init_mem
  142.         call    init_page_map
  143.  
  144. ; ENABLE PAGING
  145.  
  146.         mov     eax, sys_proc - OS_BASE + PROC.pdt_0
  147.         mov     cr3, eax
  148.  
  149.         mov     eax, cr0
  150.         or      eax, CR0_PG + CR0_WP
  151.         mov     cr0, eax
  152.  
  153.         lgdt    [gdts]
  154.         jmp     pword os_code:high_code
  155.  
  156. align 4
  157. bios32_entry    dd ?
  158. tmp_page_tabs   dd ?
  159. use16
  160. ap_init16:
  161.         cli
  162.         lgdt    [cs:gdts_ap - ap_init16]
  163.         mov     eax, [cs:cr3_ap - ap_init16]
  164.         mov     cr3, eax
  165.         mov     eax, [cs:cr4_ap - ap_init16]
  166.         mov     cr4, eax
  167.         mov     eax, CR0_PE + CR0_PG + CR0_WP
  168.         mov     cr0, eax
  169.         jmp     pword os_code:ap_init_high
  170. align 16
  171. gdts_ap:
  172.         dw     gdte-gdts-1
  173.         dd     gdts
  174.         dw     0
  175. cr3_ap  dd     ?
  176. cr4_ap  dd     ?
  177. ap_init16_size = $ - ap_init16
  178. use32
  179.  
  180. __DEBUG__ fix 1
  181. __DEBUG_LEVEL__ fix 1
  182. include 'init.inc'
  183.  
  184. org OS_BASE+$
  185.  
  186. include 'fdo.inc'
  187.  
  188. align 4
  189. high_code:
  190.         mov     ax, os_stack
  191.         mov     bx, app_data
  192.         mov     cx, app_tls
  193.         mov     ss, ax
  194.         add     esp, OS_BASE
  195.  
  196.         mov     ds, bx
  197.         mov     es, bx
  198.         mov     fs, cx
  199.         mov     gs, bx
  200.  
  201.         xor     eax, eax
  202.         mov     ebx, 0xFFFFF000 + PG_SHARED + PG_NOCACHE + PG_UWR
  203.         bt      [cpu_caps], CAPS_PAT
  204.         setc    al
  205.         shl     eax, 7
  206.         or      ebx, eax
  207.  
  208.         mov     eax, PG_GLOBAL
  209.         bt      [cpu_caps], CAPS_PGE
  210.         jnc     @F
  211.  
  212.         or      [sys_proc + PROC.pdt_0 + (OS_BASE shr 20)], eax
  213.         or      ebx, eax
  214.  
  215.         mov     eax, cr4
  216.         or      eax, CR4_PGE
  217.         mov     cr4, eax
  218. @@:
  219.         mov     [pte_valid_mask], ebx
  220.  
  221.         xor     eax, eax
  222.         mov     [sys_proc + PROC.pdt_0], eax
  223.         mov     [sys_proc + PROC.pdt_0+4], eax
  224.  
  225.         mov     eax, cr3
  226.         mov     cr3, eax          ; flush TLB
  227.  
  228.         mov     ecx, pg_data.mutex
  229.         call    mutex_init
  230.  
  231.         mov     ecx, disk_list_mutex
  232.         call    mutex_init
  233.  
  234.         mov     ecx, keyboard_list_mutex
  235.         call    mutex_init
  236.  
  237.         mov     ecx, unpack_mutex
  238.         call    mutex_init
  239.  
  240.         mov     ecx, application_table_mutex
  241.         call    mutex_init
  242.  
  243.         mov     ecx, ide_mutex
  244.         call    mutex_init
  245.         mov     ecx, ide_channel1_mutex
  246.         call    mutex_init
  247.         mov     ecx, ide_channel2_mutex
  248.         call    mutex_init
  249.         mov     ecx, ide_channel3_mutex
  250.         call    mutex_init
  251.         mov     ecx, ide_channel4_mutex
  252.         call    mutex_init
  253.         mov     ecx, ide_channel5_mutex
  254.         call    mutex_init
  255.         mov     ecx, ide_channel6_mutex
  256.         call    mutex_init
  257. ;-----------------------------------------------------------------------------
  258. ; SAVE REAL MODE VARIABLES
  259. ;-----------------------------------------------------------------------------
  260. ; --------------- APM ---------------------
  261.  
  262. ; init selectors
  263.         mov     ebx, [BOOT.apm_entry]        ; offset of APM entry point
  264.         movzx   eax, word [BOOT.apm_code_32] ; real-mode segment base address of
  265.                                              ; protected-mode 32-bit code segment
  266.         movzx   ecx, word [BOOT.apm_code_16] ; real-mode segment base address of
  267.                                              ; protected-mode 16-bit code segment
  268.         movzx   edx, word [BOOT.apm_data_16] ; real-mode segment base address of
  269.                                              ; protected-mode 16-bit data segment
  270.  
  271.         shl     eax, 4
  272.         mov     [dword apm_code_32 + 2], ax
  273.         shr     eax, 16
  274.         mov     [dword apm_code_32 + 4], al
  275.  
  276.         shl     ecx, 4
  277.         mov     [dword apm_code_16 + 2], cx
  278.         shr     ecx, 16
  279.         mov     [dword apm_code_16 + 4], cl
  280.  
  281.         shl     edx, 4
  282.         mov     [dword apm_data_16 + 2], dx
  283.         shr     edx, 16
  284.         mov     [dword apm_data_16 + 4], dl
  285.  
  286.         mov     dword[apm_entry], ebx
  287.         mov     word [apm_entry + 4], apm_code_32 - gdts
  288.  
  289.         mov     eax, dword[BOOT.apm_version]   ; version & flags
  290.         mov     [apm_vf], eax
  291. ; -----------------------------------------
  292.         mov     al, [BOOT.dma]            ; DMA access
  293.         mov     [allow_dma_access], al
  294.  
  295.         mov     al, [BOOT.debug_print]    ; If nonzero, duplicates debug output to the screen
  296.         mov     [debug_direct_print], al
  297.  
  298.         mov     al, [BOOT.launcher_start] ; Start the first app (LAUNCHER) after kernel is loaded?
  299.         mov     [launcher_start], al
  300.  
  301.         mov     eax, [BOOT.devicesdat_size]
  302.         mov     [acpi_dev_size], eax
  303.         mov     eax, [BOOT.devicesdat_data]
  304.         mov     [acpi_dev_data], eax
  305.  
  306.         mov     esi, BOOT.bios_hd
  307.         movzx   ecx, byte [esi-1]
  308.         mov     [NumBiosDisks], ecx
  309.         mov     edi, BiosDisksData
  310.         shl     ecx, 2
  311.         rep movsd
  312.  
  313. ; -------- Fast System Call init ----------
  314. ; Intel SYSENTER/SYSEXIT (AMD CPU support it too)
  315.         bt      [cpu_caps], CAPS_SEP
  316.         jnc     .SEnP  ; SysEnter not Present
  317.         xor     edx, edx
  318.         mov     ecx, MSR_SYSENTER_CS
  319.         mov     eax, os_code
  320.         wrmsr
  321.         mov     ecx, MSR_SYSENTER_ESP
  322. ;           mov eax, sysenter_stack ; Check it
  323.         xor     eax, eax
  324.         wrmsr
  325.         mov     ecx, MSR_SYSENTER_EIP
  326.         mov     eax, sysenter_entry
  327.         wrmsr
  328. .SEnP:
  329. ; AMD SYSCALL/SYSRET
  330.         cmp     byte[cpu_vendor], 'A'
  331.         jne     .noSYSCALL
  332.         mov     eax, 0x80000001
  333.         cpuid
  334.         test    edx, 0x800  ; bit_11 - SYSCALL/SYSRET support
  335.         jz      .noSYSCALL
  336.         mov     ecx, MSR_AMD_EFER
  337.         rdmsr
  338.         or      eax, 1 ; bit_0 - System Call Extension (SCE)
  339.         wrmsr
  340.  
  341.         ; !!!! It`s dirty hack, fix it !!!
  342.         ; Bits of EDX :
  343.         ; Bit 31–16 During the SYSRET instruction, this field is copied into the CS register
  344.         ;  and the contents of this field, plus 8, are copied into the SS register.
  345.         ; Bit 15–0 During the SYSCALL instruction, this field is copied into the CS register
  346.         ;  and the contents of this field, plus 8, are copied into the SS register.
  347.  
  348.         ; mov   edx, (os_code + 16) * 65536 + os_code
  349.         mov     edx, 0x1B0008
  350.  
  351.         mov     eax, syscall_entry
  352.         mov     ecx, MSR_AMD_STAR
  353.         wrmsr
  354. .noSYSCALL:
  355. ; -----------------------------------------
  356.         stdcall alloc_page
  357.         stdcall map_page, tss-0xF80, eax, PG_SWR
  358.         stdcall alloc_page
  359.         stdcall map_page, tss+0x80, eax, PG_SWR
  360.         stdcall alloc_page
  361.         stdcall map_page, tss+0x1080, eax, PG_SWR
  362.  
  363. ; LOAD IDT
  364.  
  365.         call    build_interrupt_table ;lidt is executed
  366.           ;lidt [idtreg]
  367.  
  368.         call    init_kernel_heap
  369.         call    init_fpu
  370.         mov     eax, [xsave_area_size]
  371.         lea     eax, [eax*2 + RING0_STACK_SIZE*2]
  372.         stdcall kernel_alloc, eax
  373.         mov     [os_stack_seg], eax
  374.  
  375.         lea     esp, [eax + RING0_STACK_SIZE]
  376.  
  377.         mov     [tss._ss0], os_stack
  378.         mov     [tss._esp0], esp
  379.         mov     [tss._esp], esp
  380.         mov     [tss._cs], os_code
  381.         mov     [tss._ss], os_stack
  382.         mov     [tss._ds], app_data
  383.         mov     [tss._es], app_data
  384.         mov     [tss._fs], app_data
  385.         mov     [tss._gs], app_data
  386.         mov     [tss._io], 128
  387. ;Add IO access table - bit array of permitted ports
  388.         mov     edi, tss._io_map_0
  389.         xor     eax, eax
  390.         not     eax
  391.         mov     ecx, 8192/4
  392.         rep stosd                    ; access to 4096*8=65536 ports
  393.  
  394.         mov     ax, tss0
  395.         ltr     ax
  396.  
  397.         mov     eax, sys_proc
  398.         list_init eax
  399.         add     eax, PROC.thr_list
  400.         list_init eax
  401.  
  402.         call    init_video
  403.         call    init_pat_mtrr
  404.         mov     [LFBAddress], LFB_BASE
  405.         mov     ecx, bios_fb
  406.         call    set_framebuffer
  407.         call    init_malloc
  408.  
  409.         stdcall alloc_kernel_space, 0x50000         ; FIXME check size
  410.         mov     [default_io_map], eax
  411.  
  412.         add     eax, 0x2000
  413.         mov     [ipc_tmp], eax
  414.         mov     ebx, 0x1000
  415.  
  416.         add     eax, 0x40000
  417.         mov     [proc_mem_map], eax
  418.  
  419.         add     eax, 0x8000
  420.         mov     [proc_mem_pdir], eax
  421.  
  422.         add     eax, ebx
  423.         mov     [proc_mem_tab], eax
  424.  
  425.         add     eax, ebx
  426.         mov     [tmp_task_ptab], eax
  427.  
  428.         add     eax, ebx
  429.         mov     [ipc_pdir], eax
  430.  
  431.         add     eax, ebx
  432.         mov     [ipc_ptab], eax
  433.  
  434.         stdcall kernel_alloc, (unpack.LZMA_BASE_SIZE + (unpack.LZMA_LIT_SIZE shl \
  435.                 (unpack.lc + unpack.lp)))*4
  436.  
  437.         mov     [unpack.p], eax
  438.  
  439.         call    init_events
  440.         mov     eax, srv.fd - SRV.fd
  441.         mov     [srv.fd], eax
  442.         mov     [srv.bk], eax
  443.  
  444. ;Set base of graphic segment to linear address of LFB
  445.         mov     eax, [LFBAddress]         ; set for gs
  446.         mov     [graph_data_l+2], ax
  447.         shr     eax, 16
  448.         mov     [graph_data_l+4], al
  449.         mov     [graph_data_l+7], ah
  450.  
  451.         stdcall kernel_alloc, [_display.win_map_size]
  452.         mov     [_display.win_map], eax
  453.  
  454.         xor     eax, eax
  455.         inc     eax
  456.  
  457. ; set background
  458.  
  459.         mov     [BgrDrawMode], eax
  460.         mov     [BgrDataWidth], eax
  461.         mov     [BgrDataHeight], eax
  462.         mov     [mem_BACKGROUND], 4
  463.         mov     [img_background], static_background_data
  464.  
  465. ; set clipboard
  466.  
  467.         xor     eax, eax
  468.         mov     [clipboard_slots], eax
  469.         mov     [clipboard_write_lock], eax
  470.         stdcall kernel_alloc, PAGE_SIZE
  471.         test    eax, eax
  472.         jnz     @f
  473.  
  474.         dec     eax
  475. @@:
  476.         mov     [clipboard_main_list], eax
  477.  
  478.         call    check_acpi
  479.  
  480.         mov     eax, [hpet_base]
  481.         test    eax, eax
  482.         jz      @F
  483.         stdcall map_io_mem, [hpet_base], 1024, PG_GLOBAL + PAT_UC + PG_SWR
  484.         mov     [hpet_base], eax
  485.         mov     eax, [eax+HPET_ID]
  486.         DEBUGF  1, "K : HPET caps %x\n", eax
  487.         call    init_hpet
  488. @@:
  489. ; SET UP OS TASK
  490.  
  491.         mov     esi, boot_setostask
  492.         call    boot_log
  493.  
  494.         mov     edi, sys_proc + PROC.heap_lock
  495.         mov     ecx, (PROC.ht_free - PROC.heap_lock)/4
  496.  
  497.         xor     eax, eax
  498.         cld
  499.         rep stosd
  500.  
  501.         mov     [edi], dword (PROC.pdt_0 - PROC.htab)/4 - 3
  502.         mov     [edi+4], dword 3           ;reserve handles for stdin stdout and stderr
  503.         mov     ecx, (PROC.pdt_0 - PROC.htab)/4
  504.         add     edi, 8
  505.         inc     eax
  506. @@:
  507.         stosd
  508.         inc     eax
  509.         cmp     eax, ecx
  510.         jbe     @B
  511.  
  512.         mov     [sys_proc + PROC.pdt_0_phys], sys_proc - OS_BASE + PROC.pdt_0
  513.  
  514.         mov     eax, -1
  515.         mov     edi, thr_slot_map+4
  516.         mov     [edi-4], dword 0xFFFFFFF8
  517.         stosd
  518.         stosd
  519.         stosd
  520.         stosd
  521.         stosd
  522.         stosd
  523.         stosd
  524.  
  525.         mov     [current_process], sys_proc
  526.  
  527.         ; set all the threads state to free
  528.         mov     edi, SLOT_BASE
  529.         movi    eax, TSTATE_FREE
  530.         movi    ecx, max_processes
  531. @@:
  532.         mov     [edi+APPDATA.state], TSTATE_FREE
  533.         add     edi, sizeof.APPDATA
  534.         dec     ecx
  535.         jns     @b
  536.  
  537.         mov     edx, SLOT_BASE + sizeof.APPDATA*1
  538.         mov     ebx, [os_stack_seg]
  539.         add     ebx, RING0_STACK_SIZE
  540.         add     ebx, [xsave_area_size]
  541.         call    setup_os_slot
  542.         mov     dword [edx], 'IDLE'
  543.         sub     [edx + APPDATA.saved_esp], 4
  544.         mov     eax, [edx + APPDATA.saved_esp]
  545.         mov     dword [eax], idle_thread
  546.         mov     ecx, IDLE_PRIORITY
  547.         call    scheduler_add_thread
  548.  
  549.         mov     edx, SLOT_BASE + sizeof.APPDATA*2
  550.         mov     ebx, [os_stack_seg]
  551.         call    setup_os_slot
  552.         mov     dword [edx], 'OS'
  553.         xor     ecx, ecx
  554.         call    scheduler_add_thread
  555.  
  556.         mov     [current_slot_idx], 2
  557.         mov     [thread_count], 2
  558.         mov     [current_slot], SLOT_BASE + sizeof.APPDATA*2
  559.  
  560. ; Move other CPUs to deep sleep, if it is useful
  561. uglobal
  562. use_mwait_for_idle db 0
  563. endg
  564.         cmp     [cpu_vendor+8], 'ntel'
  565.         jnz     .no_wake_cpus
  566.         bt      [cpu_caps+4], CAPS_MONITOR-32
  567.         jnc     .no_wake_cpus
  568.         dbgstr 'using mwait for idle loop'
  569.         inc     [use_mwait_for_idle]
  570.         mov     ebx, [cpu_count]
  571.         cmp     ebx, 1
  572.         jbe     .no_wake_cpus
  573.         call    create_trampoline_pgmap
  574.         mov     [cr3_ap + OS_BASE], eax
  575.         mov     eax, cr4
  576.         mov     [cr4_ap + OS_BASE], eax
  577.         mov     esi, OS_BASE + ap_init16
  578.         mov     edi, OS_BASE + 8000h
  579.         mov     ecx, (ap_init16_size + 3) / 4
  580.         rep movsd
  581.         mov     eax, [LAPIC_BASE]
  582.         test    eax, eax
  583.         jnz     @f
  584.         stdcall map_io_mem, [acpi_lapic_base], 0x1000, PG_GLOBAL + PG_NOCACHE + PG_SWR
  585.         mov     [LAPIC_BASE], eax
  586. @@:
  587.         lea     edi, [eax + APIC_ICRL]
  588.         mov     esi, smpt+4
  589.         dec     ebx
  590. .wake_cpus_loop:
  591.         lodsd
  592.         push    esi
  593.         xor     esi, esi
  594.         inc     esi
  595.         shl     eax, 24
  596.         mov     [edi+10h], eax
  597. ; assert INIT IPI
  598.         mov     dword [edi], 0C500h
  599.         call    delay_ms
  600. @@:
  601.         test    dword [edi], 1000h
  602.         jnz     @b
  603. ; deassert INIT IPI
  604.         mov     dword [edi], 8500h
  605.         call    delay_ms
  606. @@:
  607.         test    dword [edi], 1000h
  608.         jnz     @b
  609. ; send STARTUP IPI
  610.         mov     dword [edi], 600h + (8000h shr 12)
  611.         call    delay_ms
  612. @@:
  613.         test    dword [edi], 1000h
  614.         jnz     @b
  615.         pop     esi
  616.         dec     ebx
  617.         jnz     .wake_cpus_loop
  618.         mov     eax, [cpu_count]
  619.         dec     eax
  620. @@:
  621.         cmp     [ap_initialized], eax
  622.         jnz     @b
  623.         mov     eax, [cr3_ap + OS_BASE]
  624.         call    free_page
  625. .no_wake_cpus:
  626.  
  627. ; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
  628.         mov     esi, boot_initirq
  629.         call    boot_log
  630.         call    init_irqs
  631.  
  632.         mov     esi, boot_picinit
  633.         call    boot_log
  634.         call    PIC_init
  635.  
  636.         mov     esi, boot_v86machine
  637.         call    boot_log
  638. ; Initialize system V86 machine
  639.         call    init_sys_v86
  640.  
  641.         mov     esi, boot_inittimer
  642.         call    boot_log
  643. ; Initialize system timer (IRQ0)
  644.         call    PIT_init
  645.  
  646. ; Register ramdisk file system
  647. if ~ defined extended_primary_loader
  648.         cmp     [BOOT.rd_load_from], RD_LOAD_FROM_HD    ; will be loaded later
  649.         je      @f
  650. end if
  651.         cmp     [BOOT.rd_load_from], RD_LOAD_FROM_NONE
  652.         je      @f
  653.         call    register_ramdisk
  654. ;--------------------------------------
  655. @@:
  656.         mov     esi, boot_initapic
  657.         call    boot_log
  658. ; Try to Initialize APIC
  659.         call    APIC_init
  660.  
  661.         mov     esi, boot_enableirq
  662.         call    boot_log
  663. ; Enable timer IRQ (IRQ0) and co-processor IRQ (IRQ13)
  664. ; they are used: when partitions are scanned, hd_read relies on timer
  665.         call    unmask_timer
  666.         ; Prevent duplicate timer IRQs in APIC mode
  667.         cmp     [irq_mode], IRQ_APIC
  668.         jz      @f
  669.         stdcall enable_irq, 2               ; @#$%! PIC
  670. @@:
  671.         stdcall enable_irq, 13              ; co-processor
  672.  
  673. ; Setup serial output console (if enabled)
  674. if defined debug_com_base
  675.  
  676.         ; reserve port so nobody else will use it
  677.         xor     ebx, ebx
  678.         mov     ecx, debug_com_base
  679.         mov     edx, debug_com_base+7
  680.         call    r_f_port_area
  681.  
  682.         ; enable Divisor latch
  683.         mov     dx, debug_com_base+3
  684.         mov     al, 1 shl 7
  685.         out     dx, al
  686.  
  687.         ; Set speed to 115200 baud (max speed)
  688.         mov     dx, debug_com_base
  689.         mov     al, 0x01
  690.         out     dx, al
  691.  
  692.         mov     dx, debug_com_base+1
  693.         mov     al, 0x00
  694.         out     dx, al
  695.  
  696.         ; No parity, 8bits words, one stop bit, dlab bit back to 0
  697.         mov     dx, debug_com_base+3
  698.         mov     al, 3
  699.         out     dx, al
  700.  
  701.         ; disable interrupts
  702.         mov     dx, debug_com_base+1
  703.         mov     al, 0
  704.         out     dx, al
  705.  
  706.         ; clear +  enable fifo (64 bits)
  707.         mov     dx, debug_com_base+2
  708.         mov     al, 0x7 + 1 shl 5
  709.         out     dx, al
  710.  
  711. end if
  712.  
  713.  
  714. ;-----------------------------------------------------------------------------
  715. ; show SVN version of kernel on the message board
  716. ;-----------------------------------------------------------------------------
  717.         mov     eax, [version_inf.rev]
  718.         DEBUGF  1, "K : kernel SVN r%d\n", eax
  719. ;-----------------------------------------------------------------------------
  720. ; show CPU count on the message board
  721. ;-----------------------------------------------------------------------------
  722.         mov     eax, [cpu_count]
  723.         test    eax, eax
  724.         jnz     @F
  725.         mov     al, 1                             ; at least one CPU
  726. @@:
  727.         DEBUGF  1, "K : %d CPU detected\n", eax
  728. ;-----------------------------------------------------------------------------
  729. ; detect Floppy drives
  730. ;-----------------------------------------------------------------------------
  731.         mov     esi, boot_detectfloppy
  732.         call    boot_log
  733. include 'detect/dev_fd.inc'
  734. ;-----------------------------------------------------------------------------
  735. ; create pci-devices list
  736. ;-----------------------------------------------------------------------------
  737.         mov     [pci_access_enabled], 1
  738.         call    pci_enum
  739. ;-----------------------------------------------------------------------------
  740. ; initialisation IDE ATA code
  741. ;-----------------------------------------------------------------------------
  742. include 'detect/init_ata.inc'
  743. ;-----------------------------------------------------------------------------
  744. ; initialisation AHCI code
  745. ;-----------------------------------------------------------------------------
  746.         jmp     ahci_code_end
  747. include 'blkdev/ahci.inc'
  748. ahci_code_end:
  749.         call    ahci_init
  750. ;-----------------------------------------------------------------------------
  751. if 0
  752.         mov     ax, [BOOT.sys_disk]
  753.         cmp     ax, 'r1'; if using not ram disk, then load librares and parameters {SPraid.simba}
  754.         je      no_lib_load
  755.  
  756.         mov     esi, boot_loadlibs
  757.         call    boot_log
  758. ; LOADING LIBRARES
  759.         stdcall dll.Load, @IMPORT           ; loading librares for kernel (.obj files)
  760.         call    load_file_parse_table       ; prepare file parse table
  761.         call    set_kernel_conf             ; configure devices and gui
  762. no_lib_load:
  763. end if
  764.  
  765. ; Display APIC status
  766.         mov     esi, boot_APIC_found
  767.         cmp     [irq_mode], IRQ_APIC
  768.         je      @f
  769.         mov     esi, boot_APIC_nfound
  770. @@:
  771.         call    boot_log
  772.  
  773. ; PRINT AMOUNT OF MEMORY
  774.         mov     esi, boot_memdetect
  775.         call    boot_log
  776.  
  777.         movzx   ecx, word [boot_y]
  778.         if lang eq ru
  779.         or      ecx, (10+30*6) shl 16
  780.         else if lang eq sp
  781.         or      ecx, (10+33*6) shl 16
  782.         else
  783.         or      ecx, (10+29*6) shl 16
  784.         end if
  785.         sub     ecx, 10
  786.         mov     edx, 0xFFFFFF
  787.         mov     ebx, [MEM_AMOUNT]
  788.         shr     ebx, 20
  789.         xor     edi, edi
  790.         mov     eax, 0x00040000
  791.         inc     edi
  792.         call    display_number_force
  793.  
  794. ; BUILD SCHEDULER
  795.  
  796. ;        call    build_scheduler; sys32.inc
  797.  
  798. ;        mov     esi, boot_devices
  799. ;        call    boot_log
  800.  
  801. include "detect/vortex86.inc"                     ; Vortex86 SoC detection code
  802.  
  803.         stdcall load_pe_driver, szVidintel, 0
  804.  
  805.         call    usb_init
  806.  
  807. ; SET PRELIMINARY WINDOW STACK AND POSITIONS
  808.  
  809.         mov     esi, boot_windefs
  810.         call    boot_log
  811.         call    set_window_defaults
  812.  
  813. ; SET BACKGROUND DEFAULTS
  814.  
  815.         mov     esi, boot_bgr
  816.         call    boot_log
  817.         call    init_background
  818.         call    calculatebackground
  819.  
  820. ; RESERVE SYSTEM IRQ'S JA PORT'S
  821.  
  822.         mov     esi, boot_resirqports
  823.         call    boot_log
  824.         call    reserve_irqs_ports
  825.  
  826.         mov     [SLOT_BASE + APPDATA.window], window_data
  827.         mov     [SLOT_BASE + sizeof.APPDATA + APPDATA.window], window_data + sizeof.WDATA
  828.         mov     [SLOT_BASE + sizeof.APPDATA*2 + APPDATA.window], window_data + sizeof.WDATA*2
  829.         mov     [window_data + WDATA.thread], SLOT_BASE
  830.         mov     [window_data + sizeof.WDATA + WDATA.thread], SLOT_BASE + sizeof.APPDATA
  831.         mov     [window_data + sizeof.WDATA*2 + WDATA.thread], SLOT_BASE + sizeof.APPDATA*2
  832.  
  833.         call    init_display
  834.         mov     eax, [def_cursor]
  835.         mov     [window_data + sizeof.WDATA + WDATA.cursor], eax
  836.         mov     [window_data + sizeof.WDATA*2 + WDATA.cursor], eax
  837.  
  838. ; PRINT CPU FREQUENCY
  839.  
  840.         mov     esi, boot_cpufreq
  841.         call    boot_log
  842.  
  843.         cli
  844.         mov     ebx, [hpet_base]
  845.         test    ebx, ebx
  846.         jz      @F
  847.         mov     ebx, [ebx + HPET_COUNTER]
  848.  
  849.         rdtsc
  850.         mov     ecx, 1000
  851.         sub     eax, [hpet_tsc_start]
  852.         sbb     edx, [hpet_tsc_start + 4]
  853.         shld    edx, eax, 10
  854.         shl     eax, 10
  855.         mov     esi, eax
  856.         mov     eax, edx
  857.         mul     ecx
  858.         xchg    eax, esi
  859.         mul     ecx
  860.         adc     edx, esi
  861.         div     ebx
  862.         mul     ecx
  863.         div     [hpet_period]
  864.         mul     ecx
  865.         DEBUGF  1, "K : cpu frequency %u Hz\n", eax
  866.         jmp     .next
  867. @@:
  868.         rdtsc
  869.         mov     ecx, eax
  870.         mov     esi, 250            ; wait 1/4 a second
  871.         call    delay_ms
  872.         rdtsc
  873.  
  874.         sub     eax, ecx
  875.         xor     edx, edx
  876.         shld    edx, eax, 2
  877.         shl     eax, 2
  878. .next:
  879.         mov     dword [cpu_freq], eax
  880.         mov     dword [cpu_freq + 4], edx
  881.         mov     ebx, 1000000
  882.         div     ebx
  883.         mov     ebx, eax
  884.  
  885.         movzx   ecx, word [boot_y]
  886.         if lang eq ru
  887.         add     ecx, (10+19*6) shl 16 - 10
  888.         else if lang eq sp
  889.         add     ecx, (10+25*6) shl 16 - 10
  890.         else
  891.         add     ecx, (10+17*6) shl 16 - 10
  892.         end if
  893.  
  894.         mov     edx, 0xFFFFFF
  895.         xor     edi, edi
  896.         mov     eax, 0x00040000
  897.         inc     edi
  898.         call    display_number_force
  899.  
  900. ; SET VARIABLES
  901.  
  902.         call    set_variables
  903.  
  904. ; STACK AND FDC
  905.  
  906.         call    stack_init
  907.         call    fdc_init
  908.  
  909. ; PALETTE FOR 320x200 and 640x480 16 col
  910.  
  911.         cmp     [SCR_MODE], word 0x12
  912.         jne     no_pal_vga
  913.         mov     esi, boot_pal_vga
  914.         call    boot_log
  915.         call    paletteVGA
  916.       no_pal_vga:
  917.  
  918.         cmp     [SCR_MODE], word 0x13
  919.         jne     no_pal_ega
  920.         mov     esi, boot_pal_ega
  921.         call    boot_log
  922.         call    palette320x200
  923.       no_pal_ega:
  924.  
  925. ; LOAD DEFAULT SKIN
  926.  
  927.         call    load_default_skin
  928.  
  929. ; Protect I/O permission map
  930.  
  931.         mov     esi, [default_io_map]
  932.         stdcall map_page, esi, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map], PG_READ
  933.         add     esi, 0x1000
  934.         stdcall map_page, esi, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4], PG_READ
  935.  
  936.         stdcall map_page, tss._io_map_0, \
  937.                 [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map], PG_READ
  938.         stdcall map_page, tss._io_map_1, \
  939.                 [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4], PG_READ
  940.  
  941. ; SET KEYBOARD PARAMETERS
  942.         mov     al, 0xf6       ; reset keyboard, scan enabled
  943.         call    kb_write_wait_ack
  944.         test    ah, ah
  945.         jnz     .no_keyboard
  946.  
  947. iglobal
  948. align 4
  949. ps2_keyboard_functions:
  950.         dd      .end - $
  951.         dd      0       ; no close
  952.         dd      ps2_set_lights
  953. .end:
  954. endg
  955.         stdcall register_keyboard, ps2_keyboard_functions, 0
  956.        ; mov   al, 0xED       ; Keyboard LEDs - only for testing!
  957.        ; call  kb_write_wait_ack
  958.        ; mov   al, 111b
  959.        ; call  kb_write_wait_ack
  960.  
  961.         mov     al, 0xF3     ; set repeat rate & delay
  962.         call    kb_write_wait_ack
  963.         mov     al, 0; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
  964.         call    kb_write_wait_ack
  965.      ;// mike.dld [
  966.         call    set_lights
  967.      ;// mike.dld ]
  968.         stdcall attach_int_handler, 1, irq1, 0
  969.         DEBUGF  1, "K : IRQ1 return code %x\n", eax
  970. .no_keyboard:
  971.  
  972. ; Load PS/2 mouse driver
  973.         mov     esi, boot_setmouse
  974.         call    boot_log
  975.         stdcall load_pe_driver, szPS2MDriver, 0
  976.  
  977. ; LOAD FIRST APPLICATION
  978.         cmp     byte [launcher_start], 1        ; Check if starting LAUNCHER is selected on blue screen (1 = yes)
  979.         jnz     first_app_found
  980.  
  981.         cli
  982.         mov     ebp, firstapp
  983.         call    fs_execute_from_sysdir
  984.         test    eax, eax
  985.         jns     first_app_found
  986.  
  987.         mov     esi, boot_failed
  988.         call    boot_log
  989.  
  990.         mov     eax, 0xDEADBEEF      ; otherwise halt
  991.         hlt
  992.  
  993. first_app_found:
  994.  
  995. ; START MULTITASKING
  996. preboot_blogesc = 0       ; start immediately after bootlog
  997.  
  998. ; A 'All set - press ESC to start' messages if need
  999. if preboot_blogesc
  1000.         mov     esi, boot_tasking
  1001.         call    boot_log
  1002. .bll1:
  1003.         in      al, 0x60        ; wait for ESC key press
  1004.         cmp     al, 129
  1005.         jne     .bll1
  1006. end if
  1007.  
  1008.         mov     [timer_ticks_enable], 1         ; for cd driver
  1009.         sti
  1010.  
  1011.         call    mtrr_validate
  1012.  
  1013.         jmp     osloop
  1014.         ; Fly :)
  1015.  
  1016. uglobal
  1017. align 4
  1018. ap_initialized  dd      0
  1019. endg
  1020.  
  1021. ap_init_high:
  1022.         mov     ax, os_stack
  1023.         mov     bx, app_data
  1024.         mov     cx, app_tls
  1025.         mov     ss, ax
  1026.         mov     ds, bx
  1027.         mov     es, bx
  1028.         mov     fs, cx
  1029.         mov     gs, bx
  1030.         xor     esp, esp
  1031.         mov     eax, sys_proc - OS_BASE + PROC.pdt_0
  1032.         mov     cr3, eax
  1033.         lock inc [ap_initialized]
  1034.         jmp     idle_loop
  1035.  
  1036.  
  1037. include 'unpacker.inc'
  1038.  
  1039. align 4
  1040. boot_log:
  1041.         pushad
  1042.  
  1043.         mov     ebx, 10*65536
  1044.         mov     bx, word [boot_y]
  1045.         add     [boot_y], dword 10
  1046.         mov     ecx, 0x80ffffff; ASCIIZ string with white color
  1047.         xor     edi, edi
  1048.         mov     edx, esi
  1049.         inc     edi
  1050.         call    dtext
  1051.  
  1052.         mov     [novesachecksum], 1000
  1053.         call    checkVga_N13
  1054.  
  1055.         popad
  1056.  
  1057.         ret
  1058.  
  1059. ;-----------------------------------------------------------------------------
  1060. ; Register ramdisk file system
  1061. register_ramdisk:
  1062.         mov     esi, boot_initramdisk
  1063.         call    boot_log
  1064.         call    ramdisk_init
  1065.         ret
  1066.  
  1067. ; in: edx -> APPDATA for OS/IDLE slot
  1068. ; in: ebx = stack base
  1069. proc setup_os_slot
  1070.         xor     eax, eax
  1071.         mov     ecx, sizeof.APPDATA/4
  1072.         mov     edi, edx
  1073.         rep stosd
  1074.  
  1075.         mov     eax, tss+0x80
  1076.         call    get_pg_addr
  1077.         inc     eax
  1078.         mov     [edx + APPDATA.io_map], eax
  1079.         mov     eax, tss+0x1080
  1080.         call    get_pg_addr
  1081.         inc     eax
  1082.         mov     [edx + APPDATA.io_map + 4], eax
  1083.  
  1084.         mov     [edx + APPDATA.pl0_stack], ebx
  1085.         lea     edi, [ebx + RING0_STACK_SIZE]
  1086.         mov     [edx + APPDATA.fpu_state], edi
  1087.         mov     [edx + APPDATA.saved_esp0], edi
  1088.         mov     [edx + APPDATA.saved_esp], edi
  1089.         mov     [edx + APPDATA.terminate_protection], 1 ; make unkillable
  1090.  
  1091.         mov     esi, fpu_data
  1092.         mov     ecx, [xsave_area_size]
  1093.         add     ecx, 3
  1094.         shr     ecx, 2
  1095.         rep movsd
  1096.  
  1097.         lea     eax, [edx + APP_EV_OFFSET]
  1098.         mov     [edx + APPDATA.fd_ev], eax
  1099.         mov     [edx + APPDATA.bk_ev], eax
  1100.  
  1101.         lea     eax, [edx + APP_OBJ_OFFSET]
  1102.         mov     [edx + APPDATA.fd_obj], eax
  1103.         mov     [edx + APPDATA.bk_obj], eax
  1104.  
  1105.         mov     [edx + APPDATA.cur_dir], sysdir_path-2
  1106.  
  1107.         mov     [edx + APPDATA.process], sys_proc
  1108.  
  1109.         lea     ebx, [edx + APPDATA.list]
  1110.         lea     ecx, [sys_proc + PROC.thr_list]
  1111.         list_add_tail ebx, ecx
  1112.  
  1113.         mov     [edx + APPDATA.wnd_number], dh
  1114.         mov     byte [edx + APPDATA.tid], dh
  1115.         movzx   eax, dh
  1116.         shl     eax, BSF sizeof.WDATA
  1117.         add     eax, window_data
  1118.         mov     [edx + APPDATA.window], eax
  1119.  
  1120.         ret
  1121. endp
  1122.  
  1123. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1124. ;                                                                    ;
  1125. ;                    MAIN OS LOOP START                              ;
  1126. ;                                                                    ;
  1127. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1128. align 32
  1129. osloop:
  1130.         mov     edx, osloop_has_work?
  1131.         xor     ecx, ecx
  1132.         call    Wait_events
  1133.         xor     eax, eax
  1134.         xchg    eax, [osloop_nonperiodic_work]
  1135.         test    eax, eax
  1136.         jz      .no_periodic
  1137.  
  1138.         call    __sys_draw_pointer
  1139.         call    window_check_events
  1140.         call    mouse_check_events
  1141.         call    checkmisc
  1142.         call    checkVga_N13
  1143. ;--------------------------------------
  1144. .no_periodic:
  1145.         call    stack_handler
  1146.         call    check_fdd_motor_status
  1147.         call    check_ATAPI_device_event
  1148.         call    check_lights_state
  1149.         call    check_timers
  1150.  
  1151.         jmp     osloop
  1152. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1153. ;                                                                    ;
  1154. ;                      MAIN OS LOOP END                              ;
  1155. ;                                                                    ;
  1156. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1157. proc osloop_has_work?
  1158.         cmp     [osloop_nonperiodic_work], 0
  1159.         jnz     .yes
  1160.         call    stack_handler_has_work?
  1161.         jnz     .yes
  1162.         call    check_fdd_motor_status_has_work?
  1163.         jnz     .yes
  1164.         call    check_ATAPI_device_event_has_work?
  1165.         jnz     .yes
  1166.         call    check_lights_state_has_work?
  1167.         jnz     .yes
  1168.         call    check_timers_has_work?
  1169.         jnz     .yes
  1170. .no:
  1171.         xor     eax, eax
  1172.         ret
  1173. .yes:
  1174.         xor     eax, eax
  1175.         inc     eax
  1176.         ret
  1177. endp
  1178.  
  1179. proc wakeup_osloop
  1180.         mov     [osloop_nonperiodic_work], 1
  1181.         ret
  1182. endp
  1183.  
  1184. uglobal
  1185. align 4
  1186. osloop_nonperiodic_work dd      ?
  1187. endg
  1188.  
  1189. uglobal
  1190. align 64
  1191. idle_addr       rb      64
  1192. endg
  1193.  
  1194. idle_thread:
  1195.         sti
  1196.  
  1197. ; The following code can be executed by all CPUs in the system.
  1198. ; All other parts of the kernel do not expect multi-CPU.
  1199. ; Also, APs don't even have a stack here.
  1200. ; Beware. Don't do anything here. Anything at all.
  1201. idle_loop:
  1202.         cmp     [use_mwait_for_idle], 0
  1203.         jnz     .mwait
  1204.  
  1205. .hlt:
  1206.         hlt
  1207.         jmp     .hlt
  1208.  
  1209. .mwait:
  1210.         mov     eax, idle_addr
  1211.         xor     ecx, ecx
  1212.         xor     edx, edx
  1213.         monitor
  1214.         xor     ecx, ecx
  1215.         mov     eax, 20h        ; or 10h
  1216.         mwait
  1217.         jmp     .mwait
  1218.  
  1219.  
  1220. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1221. ;                                                                      ;
  1222. ;                   INCLUDED SYSTEM FILES                              ;
  1223. ;                                                                      ;
  1224. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1225.  
  1226.  
  1227. include "kernel32.inc"
  1228.  
  1229.  
  1230. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1231. ;                                                                      ;
  1232. ;                       KERNEL FUNCTIONS                               ;
  1233. ;                                                                      ;
  1234. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1235.  
  1236. reserve_irqs_ports:
  1237.  
  1238.  
  1239. ; RESERVE PORTS
  1240.         mov     eax, RESERVED_PORTS
  1241.         mov     ecx, 1
  1242.  
  1243.         mov     [eax], dword 4
  1244.  
  1245.         mov     [eax+16], ecx
  1246.         mov     [eax+16+4], dword 0
  1247.         mov     [eax+16+8], dword 0x2D
  1248.  
  1249.         mov     [eax+32], ecx
  1250.         mov     [eax+32+4], dword 0x30
  1251.         mov     [eax+32+8], dword 0x4D
  1252.  
  1253.         mov     [eax+48], ecx
  1254.         mov     [eax+48+4], dword 0x50
  1255.         mov     [eax+48+8], dword 0xDF
  1256.  
  1257.         mov     [eax+64], ecx
  1258.         mov     [eax+64+4], dword 0xE5
  1259.         mov     [eax+64+8], dword 0xFF
  1260.  
  1261.         ret
  1262.  
  1263.  
  1264. iglobal
  1265.   process_number dd 0x2
  1266. endg
  1267.  
  1268. set_variables:
  1269.  
  1270.         mov     ecx, 0x16                    ; flush port 0x60
  1271. @@:
  1272.         in      al, 0x60
  1273.         loop    @b
  1274.         push    eax
  1275.  
  1276.         mov     ax, [BOOT.y_res]
  1277.         shr     ax, 1
  1278.         shl     eax, 16
  1279.         mov     ax, [BOOT.x_res]
  1280.         shr     ax, 1
  1281.         mov     [MOUSE_X], eax
  1282.         call    wakeup_osloop
  1283.  
  1284.         xor     eax, eax
  1285.         mov     [BTN_ADDR], dword BUTTON_INFO ; address of button list
  1286.  
  1287.         mov     byte [KEY_COUNT], al              ; keyboard buffer
  1288.         mov     byte [BTN_COUNT], al              ; button buffer
  1289.  
  1290.         pop     eax
  1291.         ret
  1292. ;-----------------------------------------------------------------------------
  1293.  
  1294. align 4
  1295. display_number:
  1296. ; add check pointers
  1297.         test    bl, bl
  1298.         jz      @f
  1299.         bt      ebx, 30  ; check 30 bit
  1300.         jb      @f
  1301.         stdcall is_region_userspace, ecx, 4
  1302.         jz      @f
  1303.         ret
  1304. @@:
  1305.         jz      @f
  1306.         stdcall is_region_userspace, ecx, 8
  1307.         jz      @f
  1308.         ret
  1309. @@:
  1310.         test    esi, 0x08000000
  1311.         jz      @f
  1312.         stdcall is_region_userspace, edi, 1
  1313.         jz      @f
  1314.         ret
  1315. @@:
  1316. ;It is not optimization
  1317.         mov     eax, ebx
  1318.         mov     ebx, ecx
  1319.         mov     ecx, edx
  1320.         mov     edx, esi
  1321.         mov     esi, edi
  1322. ; eax = print type, al=0 -> ebx is number
  1323. ;                   al=1 -> ebx is pointer
  1324. ;                   ah=0 -> display decimal
  1325. ;                   ah=1 -> display hexadecimal
  1326. ;                   ah=2 -> display binary
  1327. ;                   eax bits 16-21 = number of digits to display (0-32)
  1328. ;                   eax bits 22-31 = reserved
  1329. ;
  1330. ; ebx = number or pointer
  1331. ; ecx = x shl 16 + y
  1332. ; edx = color
  1333.         xor     edi, edi
  1334. display_number_force:
  1335.         push    eax
  1336.         and     eax, 0x3fffffff
  1337.         cmp     eax, 0xffff     ; length > 0 ?
  1338.         pop     eax
  1339.         jge     .cont_displ
  1340.         ret
  1341.    .cont_displ:
  1342.         push    eax
  1343.         and     eax, 0x3fffffff
  1344.         cmp     eax, 61*0x10000  ; length <= 60 ?
  1345.         pop     eax
  1346.         jb      .cont_displ2
  1347.         ret
  1348.    .cont_displ2:
  1349.  
  1350.         pushad
  1351.  
  1352.         cmp     al, 1            ; ecx is a pointer ?
  1353.         jne     @f
  1354.         mov     ebp, ebx
  1355.         add     ebp, 4
  1356.         mov     ebp, [ebp] ;[ebp + std_application_base_address]
  1357.         mov     ebx, [ebx] ;[ebx + std_application_base_address]
  1358. @@:
  1359.         sub     esp, 64
  1360.  
  1361.         test    ah, ah            ; DECIMAL
  1362.         jnz     .no_display_desnum
  1363.  
  1364.         shr     eax, 16
  1365.         and     eax, 0xC03f
  1366. ;     and   eax,0x3f
  1367.         push    eax
  1368.         and     eax, 0x3f
  1369.         mov     edi, esp
  1370.         add     edi, 4+64-1
  1371.         mov     ecx, eax
  1372.         mov     eax, ebx
  1373.         mov     ebx, 10
  1374. @@:
  1375.         xor     edx, edx
  1376.         call    division_64_bits
  1377.         div     ebx
  1378.         add     dl, 48
  1379.         mov     [edi], dl
  1380.         dec     edi
  1381.         loop    @b
  1382.  
  1383.         pop     eax
  1384.         call    normalize_number
  1385.         call    draw_num_text
  1386.         add     esp, 64
  1387.         popad
  1388.         ret
  1389. .no_display_desnum:
  1390.  
  1391.         cmp     ah, 0x01         ; HEXADECIMAL
  1392.         jne     .no_display_hexnum
  1393.  
  1394.         shr     eax, 16
  1395.         and     eax, 0xC03f
  1396. ;     and   eax,0x3f
  1397.         push    eax
  1398.         and     eax, 0x3f
  1399.         mov     edi, esp
  1400.         add     edi, 4+64-1
  1401.         mov     ecx, eax
  1402.         mov     eax, ebx
  1403.         mov     ebx, 16
  1404. @@:
  1405.         xor     edx, edx
  1406.         call    division_64_bits
  1407.         div     ebx
  1408.    ;hexletters = __fdo_hexdigits
  1409.         add     edx, __fdo_hexdigits ;hexletters
  1410.         mov     dl, [edx]
  1411.         mov     [edi], dl
  1412.         dec     edi
  1413.         loop    @b
  1414.  
  1415.         pop     eax
  1416.         call    normalize_number
  1417.         call    draw_num_text
  1418.         add     esp, 64
  1419.         popad
  1420.         ret
  1421. .no_display_hexnum:
  1422.  
  1423.         cmp     ah, 0x02         ; BINARY
  1424.         jne     .no_display_binnum
  1425.  
  1426.         shr     eax, 16
  1427.         and     eax, 0xC03f
  1428. ;     and   eax,0x3f
  1429.         push    eax
  1430.         and     eax, 0x3f
  1431.         mov     edi, esp
  1432.         add     edi, 4+64-1
  1433.         mov     ecx, eax
  1434.         mov     eax, ebx
  1435.         mov     ebx, 2
  1436. @@:
  1437.         xor     edx, edx
  1438.         call    division_64_bits
  1439.         div     ebx
  1440.         add     dl, 48
  1441.         mov     [edi], dl
  1442.         dec     edi
  1443.         loop    @b
  1444.  
  1445.         pop     eax
  1446.         call    normalize_number
  1447.         call    draw_num_text
  1448.         add     esp, 64
  1449.         popad
  1450.         ret
  1451. .no_display_binnum:
  1452.  
  1453.         add     esp, 64
  1454.         popad
  1455.         ret
  1456.  
  1457. normalize_number:
  1458.         test    ah, 0x80
  1459.         jz      .continue
  1460.         mov     ecx, 48
  1461.         and     eax, 0x3f
  1462. @@:
  1463.         inc     edi
  1464.         cmp     [edi], cl
  1465.         jne     .continue
  1466.         dec     eax
  1467.         cmp     eax, 1
  1468.         ja      @r
  1469.         mov     al, 1
  1470. .continue:
  1471.         and     eax, 0x3f
  1472.         ret
  1473.  
  1474. division_64_bits:
  1475.         test    [esp+1+4], byte 0x40
  1476.         jz      .continue
  1477.         push    eax
  1478.         mov     eax, ebp
  1479.         div     ebx
  1480.         mov     ebp, eax
  1481.         pop     eax
  1482. .continue:
  1483.         ret
  1484.  
  1485. draw_num_text:
  1486.         mov     esi, eax
  1487.         mov     edx, 64+4
  1488.         sub     edx, eax
  1489.         add     edx, esp
  1490.         mov     ebx, [esp+64+32-8+4]
  1491. ; add window start x & y
  1492.  
  1493.         mov     ecx, [current_slot]
  1494.         mov     ecx, [ecx + APPDATA.window]
  1495.  
  1496.         mov     eax, [ecx + WDATA.box.left]
  1497.         add     eax, [ecx + WDATA.clientbox.left]
  1498.         shl     eax, 16
  1499.         add     eax, [ecx + WDATA.box.top]
  1500.         add     eax, [ecx + WDATA.clientbox.top]
  1501.         add     ebx, eax
  1502.         mov     ecx, [esp+64+32-12+4]
  1503.         mov     eax, [esp+64+8]         ; background color (if given)
  1504.         mov     edi, [esp+64+4]
  1505.         and     ecx, 5FFFFFFFh
  1506.         bt      ecx, 27
  1507.         jnc     @f
  1508.         mov     edi, eax
  1509. @@:
  1510.         jmp     dtext
  1511. ;-----------------------------------------------------------------------------
  1512. align 4
  1513. sys_setup:
  1514. ;  1 = roland mpu midi base , base io address
  1515. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1516. ;  3 = not used
  1517. ;  4 = not used
  1518. ;  5 = system language, 1eng 2fi 3ger 4rus
  1519. ;  6 = not used
  1520. ;  7 = not used
  1521. ;  8 = not used
  1522. ;  9 = not used
  1523. ; 10 = not used
  1524. ; 11 = enable lba read
  1525. ; 12 = enable pci access
  1526. ;-----------------------------------------------------------------------------
  1527.         and     [esp + SYSCALL_STACK.eax], 0
  1528. ; F.21.1 - set MPU MIDI base port
  1529.         dec     ebx
  1530.         jnz     @f
  1531.  
  1532.         cmp     ecx, 0x100
  1533.         jb      @f
  1534.  
  1535.         mov     esi, 65535
  1536.         cmp     esi, ecx
  1537.         jb      @f
  1538.  
  1539.         mov     [midi_base], cx
  1540.         mov     word [mididp], cx
  1541.         inc     cx
  1542.         mov     word [midisp], cx
  1543.         ret
  1544. ;--------------------------------------
  1545. @@:
  1546. ; F.21.2 - set keyboard layout
  1547.         dec     ebx
  1548.         jnz     @f
  1549.  
  1550.         mov     eax, edx
  1551. ; 1 = normal layout
  1552.         dec     ecx
  1553.         jnz     .shift
  1554.  
  1555.         mov     ebx, keymap
  1556.         mov     ecx, 128
  1557.         call    memmove
  1558.         ret
  1559. ;--------------------------------------
  1560. .shift:
  1561. ; 2 = layout at pressed Shift
  1562.         dec     ecx
  1563.         jnz     .alt
  1564.  
  1565.         mov     ebx, keymap_shift
  1566.         mov     ecx, 128
  1567.         call    memmove
  1568.         ret
  1569. ;--------------------------------------
  1570. .alt:
  1571. ; 3 = layout at pressed Alt
  1572.         dec     ecx
  1573.         jnz     .country
  1574.  
  1575.         mov     ebx, keymap_alt
  1576.         mov     ecx, 128
  1577.         call    memmove
  1578.         ret
  1579. ;--------------------------------------
  1580. .country:
  1581. ; country identifier
  1582.         sub     ecx, 6
  1583.         jnz     .error
  1584.  
  1585.         mov     word [keyboard], dx
  1586.         ret
  1587. ;--------------------------------------
  1588. @@:
  1589. ; F.21.5 - set system language
  1590.         sub     ebx, 3
  1591.         jnz     @f
  1592.  
  1593.         mov     [syslang], ecx
  1594.         ret
  1595. ;--------------------------------------
  1596. @@:
  1597. ; F.21.11 - enable/disable low-level access to HD
  1598.         and     ecx, 1
  1599.         sub     ebx, 6
  1600.         jnz     @f
  1601.  
  1602.         mov     [lba_read_enabled], ecx
  1603.         ret
  1604. ;--------------------------------------
  1605. @@:
  1606. ; F.21.12 - enable/disable low-level access to PCI
  1607.         dec     ebx
  1608.         jnz     .error
  1609.  
  1610.         mov     [pci_access_enabled], ecx
  1611.         ret
  1612. ;--------------------------------------
  1613. .error:
  1614.         or      [esp + SYSCALL_STACK.eax], -1
  1615.         ret
  1616. ;-----------------------------------------------------------------------------
  1617. align 4
  1618. sys_getsetup:
  1619. ;  1 = roland mpu midi base , base io address
  1620. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1621. ;  3 = not used
  1622. ;  4 = not used
  1623. ;  5 = system language, 1eng 2fi 3ger 4rus
  1624. ;  6 = not used
  1625. ;  7 = not used
  1626. ;  8 = not used
  1627. ;  9 = get hs timer tic
  1628. ; 10 = not used
  1629. ; 11 = get the state "lba read"
  1630. ; 12 = get the state "pci access"
  1631. ;-----------------------------------------------------------------------------
  1632. ; F.26.1 - get MPU MIDI base port
  1633.         dec     ebx
  1634.         jnz     @f
  1635.  
  1636.         movzx   eax, [midi_base]
  1637.         mov     [esp + SYSCALL_STACK.eax], eax
  1638.         ret
  1639. ;--------------------------------------
  1640. @@:
  1641. ; F.26.2 - get keyboard layout
  1642.         dec     ebx
  1643.         jnz     @f
  1644.  
  1645.         mov     ebx, edx
  1646.         ; if given memory address belongs to kernel then error
  1647.         stdcall is_region_userspace, ebx, 128
  1648.         jnz     .addr_error
  1649. ; 1 = normal layout
  1650.         dec     ecx
  1651.         jnz     .shift
  1652.  
  1653.         mov     eax, keymap
  1654.         mov     ecx, 128
  1655.         call    memmove
  1656.         ret
  1657. ;--------------------------------------
  1658. .shift:
  1659. ; 2 = layout with pressed Shift
  1660.         dec     ecx
  1661.         jnz     .alt
  1662.  
  1663.         mov     eax, keymap_shift
  1664.         mov     ecx, 128
  1665.         call    memmove
  1666.         ret
  1667. ;--------------------------------------
  1668. .alt:
  1669. ; 3 = layout with pressed Alt
  1670.         dec     ecx
  1671.         jne     .country
  1672.  
  1673.         mov     eax, keymap_alt
  1674.         mov     ecx, 128
  1675.         call    memmove
  1676.         ret
  1677. ;--------------------------------------
  1678. .country:
  1679. ; 9 = country identifier
  1680.         sub     ecx, 6
  1681.         jnz     .error
  1682.  
  1683.         movzx   eax, word [keyboard]
  1684.         mov     [esp + SYSCALL_STACK.eax], eax
  1685.         ret
  1686.  
  1687. .addr_error:    ; if given memory address is illegal
  1688.         or      [esp + SYSCALL_STACK.eax], -1
  1689.         ret
  1690. ;--------------------------------------
  1691. @@:
  1692. ; F.26.5 - get system language
  1693.         sub     ebx, 3
  1694.         jnz     @f
  1695.  
  1696.         mov     eax, [syslang]
  1697.         mov     [esp + SYSCALL_STACK.eax], eax
  1698.         ret
  1699. ;--------------------------------------
  1700. @@:
  1701. ; F.26.9 - get the value of the time counter
  1702.         sub     ebx, 4
  1703.         jnz     @f
  1704.  
  1705.         mov     eax, [timer_ticks]
  1706.         mov     [esp + SYSCALL_STACK.eax], eax
  1707.         ret
  1708. ;--------------------------------------
  1709. @@:
  1710. ; F.26.10 - get the time from kernel launch in nanoseconds
  1711.         dec     ebx
  1712.         jnz     @f
  1713.  
  1714.         call    get_clock_ns
  1715.         mov     [esp + SYSCALL_STACK.edx], edx
  1716.         mov     [esp + SYSCALL_STACK.eax], eax
  1717.         ret
  1718. ;--------------------------------------
  1719. @@:
  1720. ; F.26.11 - Find out whether low-level HD access is enabled
  1721.         dec     ebx
  1722.         jnz     @f
  1723.  
  1724.         mov     eax, [lba_read_enabled]
  1725.         mov     [esp + SYSCALL_STACK.eax], eax
  1726.         ret
  1727. ;--------------------------------------
  1728. @@:
  1729. ; F.26.12 - Find out whether low-level PCI access is enabled
  1730.         dec     ebx
  1731.         jnz     .error
  1732.  
  1733.         mov     eax, [pci_access_enabled]
  1734.         mov     [esp + SYSCALL_STACK.eax], eax
  1735.         ret
  1736. ;--------------------------------------
  1737. .error:
  1738.         or      [esp + SYSCALL_STACK.eax], -1
  1739.         ret
  1740. ;-----------------------------------------------------------------------------
  1741. get_timer_ticks:
  1742.         mov     eax, [timer_ticks]
  1743.         ret
  1744. ;-----------------------------------------------------------------------------
  1745. iglobal
  1746. midi_base dw 0
  1747. endg
  1748.  
  1749. align 4
  1750. sys_midi:
  1751.         cmp     word [mididp], 0
  1752.         jnz     @f
  1753.         mov     [esp + SYSCALL_STACK.eax], 1
  1754.         ret
  1755. @@:
  1756.         and     [esp + SYSCALL_STACK.eax], 0
  1757.         dec     ebx
  1758.         jnz     .smn1
  1759.  ;    call setuart
  1760. @@:
  1761.         call    .is_output
  1762.         test    al, al
  1763.         jnz     @b
  1764.         mov     dx, word [midisp]
  1765.         mov     al, 0xff
  1766.         out     dx, al
  1767. @@:
  1768.         mov     dx, word [midisp]
  1769.         mov     al, 0xff
  1770.         out     dx, al
  1771.         call    .is_input
  1772.         test    al, al
  1773.         jnz     @b
  1774.         call    .get_mpu_in
  1775.         cmp     al, 0xfe
  1776.         jnz     @b
  1777. @@:
  1778.         call    .is_output
  1779.         test    al, al
  1780.         jnz     @b
  1781.         mov     dx, word [midisp]
  1782.         mov     al, 0x3f
  1783.         out     dx, al
  1784.         ret
  1785. .smn1:
  1786.         dec     ebx
  1787.         jnz     .ret
  1788. @@:
  1789.         call    .get_mpu_in
  1790.         call    .is_output
  1791.         test    al, al
  1792.         jnz     @b
  1793.         mov     al, bl
  1794.         call    .put_mpu_out
  1795. .ret:
  1796.         ret
  1797.  
  1798. .is_input:
  1799.         push    edx
  1800.         mov     dx, word [midisp]
  1801.         in      al, dx
  1802.         and     al, 0x80
  1803.         pop     edx
  1804.         ret
  1805.  
  1806. .is_output:
  1807.         push    edx
  1808.         mov     dx, word [midisp]
  1809.         in      al, dx
  1810.         and     al, 0x40
  1811.         pop     edx
  1812.         ret
  1813.  
  1814. .get_mpu_in:
  1815.         push    edx
  1816.         mov     dx, word [mididp]
  1817.         in      al, dx
  1818.         pop     edx
  1819.         ret
  1820.  
  1821. .put_mpu_out:
  1822.         push    edx
  1823.         mov     dx, word [mididp]
  1824.         out     dx, al
  1825.         pop     edx
  1826.         ret
  1827.  
  1828. ;-----------------------------------------------------------------------------
  1829. sys_end:
  1830. ; restore default cursor before killing
  1831.         pusha
  1832.         mov     ecx, [current_slot]
  1833.         mov     ecx, [ecx + APPDATA.window]
  1834.         call    restore_default_cursor_before_killing
  1835.         popa
  1836. ;--------------------------------------
  1837. ; kill all sockets this process owns
  1838.         pusha
  1839.         mov     edx, [current_slot]
  1840.         mov     edx, [edx + APPDATA.tid]
  1841.         call    socket_process_end
  1842.         popa
  1843. ;--------------------------------------
  1844.         mov     ecx, [current_slot]
  1845.         mov     eax, [ecx + APPDATA.tls_base]
  1846.         test    eax, eax
  1847.         jz      @F
  1848.  
  1849.         stdcall user_free, eax
  1850. @@:
  1851.  
  1852.         mov     eax, [current_slot]
  1853.         mov     [eax + APPDATA.state], TSTATE_ZOMBIE
  1854.         call    wakeup_osloop
  1855.  
  1856. .waitterm:            ; wait here for termination
  1857.         call    change_task
  1858.         jmp     .waitterm
  1859. ;------------------------------------------------------------------------------
  1860. align 4
  1861. ; ecx - ptr WDATA
  1862. restore_default_cursor_before_killing:
  1863.         pushfd
  1864.         cli
  1865.         mov     eax, [def_cursor]
  1866.         mov     [ecx + WDATA.cursor], eax
  1867.  
  1868.         movzx   eax, word [MOUSE_Y]
  1869.         movzx   ebx, word [MOUSE_X]
  1870.         mov     eax, [d_width_calc_area + eax*4]
  1871.  
  1872.         add     eax, [_display.win_map]
  1873.         movzx   edx, byte [ebx + eax]
  1874.         shl     edx, BSF sizeof.WDATA
  1875.         mov     esi, [window_data + edx + WDATA.cursor]
  1876.  
  1877.         cmp     esi, [current_cursor]
  1878.         je      @f
  1879.  
  1880.         cmp     [_display.select_cursor], 0
  1881.         jz      @f
  1882.  
  1883.         stdcall [_display.select_cursor], esi
  1884.         mov     [current_cursor], esi
  1885. @@:
  1886.         mov     [redrawmouse_unconditional], 1
  1887.         call    wakeup_osloop
  1888.         popfd
  1889.         ret
  1890. ;------------------------------------------------------------------------------
  1891. iglobal
  1892. align 4
  1893. sys_system_table:
  1894.         dd      sysfn_deactivate        ; 1 = deactivate window
  1895.         dd      sysfn_terminate         ; 2 = terminate thread
  1896.         dd      sysfn_activate          ; 3 = activate window
  1897.         dd      sysfn_getidletime       ; 4 = get idle time
  1898.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1899.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1900.         dd      sysfn_getactive         ; 7 = get active window
  1901.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1902.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1903.         dd      sysfn_minimize          ; 10 = minimize window
  1904.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1905.         dd      undefined_syscall       ; 12 = get last pressed key. function removed. sysfn_lastkey
  1906.         dd      sysfn_getversion        ; 13 = get kernel version
  1907.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1908.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1909.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1910.         dd      sysfn_getallmem         ; 17 = get total memory size
  1911.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1912.                                         ;                 instead of slot
  1913.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  1914.         dd      sysfn_meminfo           ; 20 = get extended memory info
  1915.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  1916.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  1917.         dd      sysfn_min_windows       ; 23 = minimize all windows
  1918.         dd      sysfn_set_screen_sizes  ; 24 = set screen sizes for Vesa
  1919.  
  1920.         dd      sysfn_zmodif            ; 25 = get/set window z modifier  ;Fantomer
  1921. sysfn_num = ($ - sys_system_table)/4
  1922. endg
  1923. ;------------------------------------------------------------------------------
  1924. sys_system:
  1925.         dec     ebx
  1926.         cmp     ebx, sysfn_num
  1927.         jae     @f
  1928.         jmp     dword [sys_system_table + ebx*4]
  1929. @@:
  1930.         ret
  1931. ;------------------------------------------------------------------------------
  1932. sysfn_shutdown:          ; 18.9 = system shutdown
  1933.         cmp     ecx, SYSTEM_SHUTDOWN
  1934.         jl      .exit_for_anyone
  1935.         cmp     ecx, SYSTEM_RESTART
  1936.         jg      .exit_for_anyone
  1937.         mov     [BOOT.shutdown_type], cl
  1938.  
  1939.         mov     eax, [thread_count]
  1940.         mov     [SYS_SHUTDOWN], al
  1941.         mov     [shutdown_processes], eax
  1942.         call    wakeup_osloop
  1943.         and     [esp + SYSCALL_STACK.eax], 0
  1944. .exit_for_anyone:
  1945.         ret
  1946.   uglobal
  1947.    shutdown_processes:
  1948.                        dd 0x0
  1949.   endg
  1950. ;------------------------------------------------------------------------------
  1951. ; in: eax -- APPDATA ptr
  1952. ; out: Z/z -- is/not kernel thread
  1953. is_kernel_thread:
  1954.         mov     eax, [eax + APPDATA.process]
  1955.         cmp     eax, [SLOT_BASE + 2*sizeof.APPDATA + APPDATA.process]       ; OS
  1956.         ret
  1957. ;------------------------------------------------------------------------------
  1958. sysfn_terminate:        ; 18.2 = TERMINATE
  1959.         push    ecx
  1960.         cmp     ecx, 2
  1961.         jb      .noprocessterminate
  1962.         mov     edx, [thread_count]
  1963.         cmp     ecx, edx
  1964.         ja      .noprocessterminate
  1965.         mov     eax, [thread_count]
  1966.         shl     ecx, BSF sizeof.APPDATA
  1967.         add     ecx, SLOT_BASE
  1968.  
  1969.         mov     edx, [ecx + APPDATA.tid]
  1970.         cmp     byte [ecx + APPDATA.state], TSTATE_FREE
  1971.         jz      .noprocessterminate
  1972.         push    eax
  1973.         mov     eax, ecx
  1974.         call    is_kernel_thread
  1975.         pop     eax
  1976.         jz      .noprocessterminate
  1977.         push    ecx edx
  1978.         mov     edx, ecx
  1979.         call    request_terminate
  1980.         pop     edx ecx
  1981.         test    eax, eax
  1982.         jz      .noprocessterminate
  1983. ;--------------------------------------
  1984. ; terminate all network sockets it used
  1985.         pusha
  1986.         mov     eax, edx     ;TODO: check function
  1987.         call    socket_process_end
  1988.         popa
  1989. ;--------------------------------------
  1990. ; restore default cursor before killing
  1991.         pusha
  1992.         mov     ecx, [esp+32]
  1993.         shl     ecx, BSF sizeof.WDATA
  1994.         add     ecx, window_data
  1995.         mov     eax, [def_cursor]
  1996.         cmp     [ecx + WDATA.cursor], eax
  1997.         je      @f
  1998.         call    restore_default_cursor_before_killing
  1999. @@:
  2000.         popa
  2001. ;--------------------------------------
  2002.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2003.         mov     [ecx + APPDATA.state], TSTATE_ZOMBIE; clear possible i40's
  2004.         call    wakeup_osloop
  2005.      ;call MEM_Heap_UnLock
  2006.  
  2007.         cmp     edx, [application_table_owner]; clear app table stat
  2008.         jne     .noatsc
  2009.         call    unlock_application_table
  2010. .noatsc:
  2011. .noprocessterminate:
  2012.         add     esp, 4
  2013.         ret
  2014. ;------------------------------------------------------------------------------
  2015. sysfn_terminate2:
  2016.         call    lock_application_table
  2017.         mov     eax, ecx
  2018.         call    pid_to_slot
  2019.         test    eax, eax
  2020.         jz      .not_found
  2021.         mov     ecx, eax
  2022.         cli
  2023.         call    sysfn_terminate
  2024.         call    unlock_application_table
  2025.         sti
  2026.         and     [esp + SYSCALL_STACK.eax], 0
  2027.         ret
  2028. .not_found:
  2029.         call    unlock_application_table
  2030.         or      [esp + SYSCALL_STACK.eax], -1
  2031.         ret
  2032. ;------------------------------------------------------------------------------
  2033. sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
  2034.         cmp     ecx, 2
  2035.         jb      .nowindowdeactivate
  2036.         cmp     ecx, [thread_count]
  2037.         ja      .nowindowdeactivate
  2038.  
  2039.         movzx   esi, word [WIN_STACK + ecx*2]
  2040.         cmp     esi, 1
  2041.         je      .nowindowdeactivate ; already deactive
  2042.  
  2043.         mov     edi, ecx
  2044.         shl     edi, BSF sizeof.WDATA
  2045.         add     edi, window_data
  2046.         movzx   esi, word [WIN_STACK + ecx * 2]
  2047.         lea     esi, [WIN_POS + esi * 2]
  2048.         call    window._.window_deactivate
  2049.         call    syscall_display_settings.calculateScreen
  2050.         call    syscall_display_settings.redrawScreen
  2051. .nowindowdeactivate:
  2052.         ret
  2053. ;------------------------------------------------------------------------------
  2054. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2055.         cmp     ecx, 2
  2056.         jb      .nowindowactivate
  2057.         cmp     ecx, [thread_count]
  2058.         ja      .nowindowactivate
  2059. ;-------------------------------------
  2060. @@:
  2061. ; If the window is captured and moved by the user,
  2062. ; then you can't change the position in window stack!!!
  2063.         mov     al, [mouse.active_sys_window.action]
  2064.         and     al, WINDOW_MOVE_AND_RESIZE_FLAGS
  2065.         test    al, al
  2066.         jz      @f
  2067.         call    change_task
  2068.         jmp     @b
  2069. @@:
  2070. ;-------------------------------------
  2071.         mov     [window_minimize], 2; restore window if minimized
  2072.         call    wakeup_osloop
  2073.  
  2074.         movzx   esi, word [WIN_STACK + ecx*2]
  2075.         cmp     esi, [thread_count]
  2076.         je      .nowindowactivate; already active
  2077.  
  2078.         mov     edi, ecx
  2079.         shl     edi, BSF sizeof.WDATA
  2080.         add     edi, window_data
  2081.         movzx   esi, word [WIN_STACK + ecx * 2]
  2082.         lea     esi, [WIN_POS + esi * 2]
  2083.         call    waredraw
  2084. .nowindowactivate:
  2085.         ret
  2086. ;------------------------------------------------------------------------------
  2087. align 4
  2088. sysfn_zmodif:
  2089. ;18,25,1 - get z_modif
  2090. ;18,25,2 - set z_modif
  2091. ;edx = -1(for current task) or TID
  2092. ;esi(for 2) = new value z_modif
  2093. ;return:
  2094. ;1:   eax = z_modif
  2095. ;2: eax=0(fail),1(success) for set z_modif
  2096.  
  2097.         cmp     edx, -1
  2098.         jne     @f
  2099.         mov     edx, [current_slot_idx]
  2100.      @@:
  2101.         cmp     edx, [thread_count]
  2102.         ja      .fail
  2103.         cmp     edx, 1
  2104.         je      .fail
  2105.  
  2106.         mov     eax, edx
  2107.         shl     edx, BSF sizeof.WDATA
  2108.         add     edx, window_data
  2109.  
  2110.         test    [edx + WDATA.fl_wstate], WSTATE_USED
  2111.         jz      .fail
  2112.  
  2113.         cmp     ecx, 1
  2114.         jnz     .set_zmod
  2115.  
  2116.         movzx   eax, [edx + WDATA.z_modif]
  2117.         jmp     .exit
  2118.  
  2119. .set_zmod:
  2120.         cmp     ecx, 2
  2121.         jnz     .fail
  2122.  
  2123.         mov     ebx, esi
  2124.         mov     esi, eax
  2125.  
  2126.         cmp     bl, ZPOS_ALWAYS_TOP
  2127.         jg      .fail
  2128.  
  2129.         mov     [edx + WDATA.z_modif], bl
  2130.  
  2131.         mov     eax, [edx + WDATA.box.left]
  2132.         mov     ebx, [edx + WDATA.box.top]
  2133.         mov     ecx, [edx + WDATA.box.width]
  2134.         mov     edx, [edx + WDATA.box.height]
  2135.         add     ecx, eax
  2136.         add     edx, ebx
  2137.         call    window._.set_screen
  2138.         call    window._.set_top_wnd
  2139.         call    window._.redraw_top_wnd
  2140.  
  2141.         shl     esi, BSF sizeof.WDATA
  2142.         mov     [esi + window_data + WDATA.fl_redraw], WSTATE_REDRAW
  2143.  
  2144.  
  2145.         mov     eax, 1
  2146.         jmp     .exit
  2147. .fail:
  2148.         xor     eax, eax
  2149. .exit:
  2150.         mov     [esp + SYSCALL_STACK.eax], eax
  2151.         ret
  2152.  
  2153. ;------------------------------------------------------------------------------
  2154. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2155.         mov     eax, [SLOT_BASE + sizeof.APPDATA + APPDATA.cpu_usage]
  2156.         mov     [esp + SYSCALL_STACK.eax], eax
  2157.         ret
  2158. ;------------------------------------------------------------------------------
  2159. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2160.         mov     eax, dword [cpu_freq]
  2161.         mov     [esp + SYSCALL_STACK.eax], eax
  2162.         ret
  2163. ;------------------------------------------------------------------------------
  2164. get_cpu_freq:
  2165.         mov     eax, dword [cpu_freq]
  2166.         mov     edx, dword [cpu_freq+4]
  2167.         ret
  2168. ;  SAVE ramdisk to /hd/1/kolibri.img
  2169. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2170.    include 'blkdev/rdsave.inc'
  2171. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2172. ;------------------------------------------------------------------------------
  2173. align 4
  2174. sysfn_getactive:        ; 18.7 = get active window
  2175.         mov     eax, [thread_count]
  2176.         movzx   eax, word [WIN_POS + eax*2]
  2177.         mov     [esp + SYSCALL_STACK.eax], eax
  2178.         ret
  2179. ;------------------------------------------------------------------------------
  2180. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2181. ;     cmp  ecx,1
  2182.         dec     ecx
  2183.         jnz     .set_flag
  2184.         movzx   eax, byte [sound_flag]; get sound_flag
  2185.         mov     [esp + SYSCALL_STACK.eax], eax
  2186.         ret
  2187. .set_flag:
  2188. ;     cmp  ecx,2
  2189.         dec     ecx
  2190.         jnz     .err
  2191.         xor     byte [sound_flag], 1
  2192. .err:
  2193.         ret
  2194. ;------------------------------------------------------------------------------
  2195. sysfn_minimize:         ; 18.10 = minimize window
  2196.         mov     [window_minimize], 1
  2197.         call    wakeup_osloop
  2198.         ret
  2199. ;------------------------------------------------------------------------------
  2200. align 4
  2201. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2202.         dec     ecx
  2203.         jnz     .exit
  2204. .small_table:
  2205.         stdcall is_region_userspace, edx, DRIVE_DATA_SIZE
  2206.         jnz     .exit
  2207.         mov     edi, edx
  2208.         mov     esi, DRIVE_DATA
  2209.         mov     ecx, DRIVE_DATA_SIZE ;10
  2210.         cld
  2211.         rep movsb
  2212. .exit:
  2213.         ret
  2214. ;------------------------------------------------------------------------------
  2215. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2216.         ; if given memory address belongs to kernel then error
  2217.         stdcall is_region_userspace, ecx, version_inf.size
  2218.         jnz     .addr_error
  2219.  
  2220.         mov     edi, ecx
  2221.         mov     esi, version_inf
  2222.         mov     ecx, version_inf.size
  2223.         rep movsb
  2224.         ret
  2225. .addr_error:    ; if given memory address is illegal
  2226.         mov     [esp + SYSCALL_STACK.eax], -1
  2227.         ret
  2228. ;------------------------------------------------------------------------------
  2229. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2230.         ;wait retrace functions
  2231.         mov     edx, 0x3da
  2232. .loop:
  2233.         in      al, dx
  2234.         test    al, 1000b
  2235.         jz      .loop
  2236.         and     [esp + SYSCALL_STACK.eax], 0
  2237.         ret
  2238. ;------------------------------------------------------------------------------
  2239. align 4
  2240. sysfn_centermouse:      ; 18.15 = mouse centered
  2241.         mov     eax, [_display.width]
  2242.         shr     eax, 1
  2243.         mov     [MOUSE_X], ax
  2244.         mov     eax, [_display.height]
  2245.         shr     eax, 1
  2246.         mov     [MOUSE_Y], ax
  2247.         call    wakeup_osloop
  2248.         xor     eax, eax
  2249.         and     [esp + SYSCALL_STACK.eax], eax
  2250.         ret
  2251. ;------------------------------------------------------------------------------
  2252. sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
  2253.         cmp     ecx, 8
  2254.         jnc     @f
  2255.         jmp     dword [.table + ecx*4]
  2256. .get_mouse_acceleration:
  2257.         xor     eax, eax
  2258.         mov     ax, [mouse_speed_factor]
  2259.         mov     [esp + SYSCALL_STACK.eax], eax
  2260.         ret
  2261. .set_mouse_acceleration:
  2262.         mov     [mouse_speed_factor], dx
  2263.         ret
  2264. .get_mouse_delay:
  2265.         xor     eax, eax
  2266.         mov     al, [mouse_delay]
  2267.         mov     [esp + SYSCALL_STACK.eax], eax
  2268.         ret
  2269. .set_mouse_delay:
  2270.         mov     [mouse_delay], dl
  2271. @@:
  2272.         ret
  2273. .set_pointer_position:
  2274.         cmp     dx, word[_display.height]
  2275.         jae     @b
  2276.         rol     edx, 16
  2277.         cmp     dx, word[_display.width]
  2278.         jae     @b
  2279.         mov     [MOUSE_X], edx
  2280.         mov     [mouse_active], 1
  2281.         jmp     wakeup_osloop
  2282. .set_mouse_button:
  2283.         mov     [BTN_DOWN], edx
  2284.         mov     [mouse_active], 1
  2285.         jmp     wakeup_osloop
  2286. .get_doubleclick_delay:
  2287.         xor     eax, eax
  2288.         mov     al, [mouse_doubleclick_delay]
  2289.         mov     [esp + SYSCALL_STACK.eax], eax
  2290.         ret
  2291. .set_doubleclick_delay:
  2292.         mov     [mouse_doubleclick_delay], dl
  2293.         ret
  2294. align 4
  2295. .table:
  2296. dd      .get_mouse_acceleration
  2297. dd      .set_mouse_acceleration
  2298. dd      .get_mouse_delay
  2299. dd      .set_mouse_delay
  2300. dd      .set_pointer_position
  2301. dd      .set_mouse_button
  2302. dd      .get_doubleclick_delay
  2303. dd      .set_doubleclick_delay
  2304. ;------------------------------------------------------------------------------
  2305. sysfn_getfreemem:
  2306.         mov     eax, [pg_data.pages_free]
  2307.         shl     eax, 2
  2308.         mov     [esp + SYSCALL_STACK.eax], eax
  2309.         ret
  2310. ;------------------------------------------------------------------------------
  2311. sysfn_getallmem:
  2312.         mov     eax, [MEM_AMOUNT]
  2313.         shr     eax, 10
  2314.         mov     [esp + SYSCALL_STACK.eax], eax
  2315.         ret
  2316. ;------------------------------------------------------------------------------
  2317. sysfn_pid_to_slot:
  2318.         mov     eax, ecx
  2319.         call    pid_to_slot
  2320.         mov     [esp + SYSCALL_STACK.eax], eax
  2321.         ret
  2322. ;------------------------------------------------------------------------------
  2323. sysfn_min_rest_window:
  2324.         pushad
  2325.         mov     eax, edx ; ebx - operating
  2326.         shr     ecx, 1
  2327.         jnc     @f
  2328.         call    pid_to_slot
  2329. @@:
  2330.         or      eax, eax ; eax - number of slot
  2331.         jz      .error
  2332.         cmp     eax, max_processes    ; varify maximal slot number
  2333.         ja      .error
  2334.         movzx   eax, word [WIN_STACK + eax*2]
  2335.         shr     ecx, 1
  2336.         jc      .restore
  2337.  ; .minimize:
  2338.         call    minimize_window
  2339.         jmp     .exit
  2340. .restore:
  2341.         call    restore_minimized_window
  2342. .exit:
  2343.         popad
  2344.         xor     eax, eax
  2345.         mov     [esp + SYSCALL_STACK.eax], eax
  2346.         ret
  2347. .error:
  2348.         popad
  2349.         xor     eax, eax
  2350.         dec     eax
  2351.         mov     [esp + SYSCALL_STACK.eax], eax
  2352.         ret
  2353. ;------------------------------------------------------------------------------
  2354. sysfn_min_windows:
  2355.         call    minimize_all_window
  2356.         mov     [esp + SYSCALL_STACK.eax], eax
  2357.         call    change_task
  2358.         ret
  2359. ;------------------------------------------------------------------------------
  2360. sysfn_set_screen_sizes:
  2361.         cmp     [SCR_MODE], word 0x13
  2362.         jbe     .exit
  2363.  
  2364.         cmp     [_display.select_cursor], select_cursor
  2365.         jne     .exit
  2366.  
  2367.         cmp     ecx, [display_width_standard]
  2368.         ja      .exit
  2369.  
  2370.         cmp     edx, [display_height_standard]
  2371.         ja      .exit
  2372.  
  2373.         pushfd
  2374.         cli
  2375.         mov     eax, ecx
  2376.         mov     ecx, [_display.lfb_pitch]
  2377.         mov     [_display.width], eax
  2378.         dec     eax
  2379.         mov     [_display.height], edx
  2380.         dec     edx
  2381. ; eax - new Screen_Max_X
  2382. ; edx - new Screen_Max_Y
  2383.         mov     [do_not_touch_winmap], 1
  2384.         call    set_screen
  2385.         mov     [do_not_touch_winmap], 0
  2386.         popfd
  2387.         call    change_task
  2388. .exit:
  2389.         ret
  2390. ;------------------------------------------------------------------------------
  2391. uglobal
  2392. screen_workarea RECT
  2393. display_width_standard dd 0
  2394. display_height_standard dd 0
  2395. do_not_touch_winmap db 0
  2396. window_minimize db 0
  2397. sound_flag      db 0
  2398.  
  2399. endg
  2400.  
  2401. iglobal
  2402. version_inf:
  2403.         db 0,7,7,0  ; version 0.7.7.0
  2404.         db 0
  2405. .rev    dd __REV__
  2406. .size = $ - version_inf
  2407. endg
  2408. ;------------------------------------------------------------------------------
  2409. align 4
  2410. sys_cachetodiskette:
  2411.         cmp     ebx, 1
  2412.         jb      .no_floppy_save
  2413.         cmp     ebx, 2
  2414.         ja      .no_floppy_save
  2415.         call    save_image
  2416.         mov     [esp + SYSCALL_STACK.eax], eax
  2417.         ret
  2418. .no_floppy_save:
  2419.         mov     [esp + SYSCALL_STACK.eax], 1
  2420.         ret
  2421. ;------------------------------------------------------------------------------
  2422. align 4
  2423. sys_cpuusage:
  2424.  
  2425. ;  RETURN: process_information structure
  2426. ;
  2427.         ; if given memory address belongs to kernel then error
  2428.         stdcall is_region_userspace, ebx, sizeof.process_information
  2429.         jnz     .addr_error
  2430.  
  2431.         cmp     ecx, -1 ; who am I ?
  2432.         jne     .no_who_am_i
  2433.         mov     ecx, [current_slot_idx]
  2434. .no_who_am_i:
  2435.         jecxz   .empty_slot
  2436.         cmp     ecx, max_processes
  2437.         ja      .empty_slot
  2438.         mov     edx, ecx
  2439.         shl     edx, BSF sizeof.APPDATA
  2440.         cmp     [SLOT_BASE+edx+APPDATA.state], TSTATE_FREE
  2441.         jnz     .thread_found
  2442. .empty_slot:
  2443.         ; zero buffer for an empty slot
  2444.         push    edi
  2445.         xor     eax, eax
  2446.         mov     edi, ebx
  2447.         movi    ecx, sizeof.process_information
  2448.         rep stosb
  2449.         pop     edi
  2450.         jmp     .nofillbuf
  2451. .thread_found:
  2452. ; +4: word: position of the window of thread in the window stack
  2453.         mov     ax, [WIN_STACK + ecx * 2]
  2454.         mov     [ebx+process_information.window_stack_position], ax
  2455. ; +6: word: number of the thread slot, which window has in the window stack
  2456. ;           position ecx (has no relation to the specific thread)
  2457.         mov     ax, [WIN_POS + ecx * 2]
  2458.         mov     [ebx+process_information.window_stack_value], ax
  2459.  
  2460.         shl     ecx, BSF sizeof.APPDATA
  2461.  
  2462. ; +0: dword: memory usage
  2463.         mov     eax, [SLOT_BASE + ecx + APPDATA.cpu_usage]
  2464.         mov     [ebx+process_information.cpu_usage], eax
  2465. ; +10: 11 bytes: name of the process
  2466.         push    ecx
  2467.         lea     eax, [SLOT_BASE + ecx + APPDATA.app_name]
  2468.         add     ebx, process_information.process_name
  2469.         mov     ecx, 11
  2470.         call    memmove
  2471.         pop     ecx
  2472.  
  2473. ; +22: address of the process in memory
  2474. ; +26: size of used memory - 1
  2475.         push    edi
  2476.         lea     edi, [ebx+12]
  2477.         xor     eax, eax
  2478.         mov     edx, 0x100000*16
  2479.         cmp     ecx, 1 shl BSF sizeof.APPDATA
  2480.         je      .os_mem
  2481.         mov     edx, [SLOT_BASE + ecx + APPDATA.process]
  2482.         mov     edx, [edx + PROC.mem_used]
  2483.         mov     eax, std_application_base_address
  2484. .os_mem:
  2485.         stosd
  2486.         lea     eax, [edx-1]
  2487.         stosd
  2488.  
  2489.         mov     edx, [SLOT_BASE + ecx + APPDATA.window]
  2490.  
  2491. ; +30: PID/TID
  2492.         mov     eax, [SLOT_BASE + ecx + APPDATA.tid]
  2493.         stosd
  2494.  
  2495.     ; window position and size
  2496.         push    esi
  2497.         lea     esi, [edx + WDATA.box]
  2498.         movsd
  2499.         movsd
  2500.         movsd
  2501.         movsd
  2502.  
  2503.     ; Process state (+50)
  2504.         movzx   eax, byte [SLOT_BASE + ecx + APPDATA.state]
  2505.         stosd
  2506.  
  2507.     ; Window client area box
  2508.         lea     esi, [edx + WDATA.clientbox]
  2509.         movsd
  2510.         movsd
  2511.         movsd
  2512.         movsd
  2513.  
  2514.     ; Window state
  2515.         mov     al, [edx + WDATA.fl_wstate]
  2516.         stosb
  2517.  
  2518.     ; Event mask (+71)
  2519.         mov     eax, [SLOT_BASE + ecx + APPDATA.event_mask]
  2520.         stosd
  2521.  
  2522.     ; Keyboard mode (+75)
  2523.         mov     al, [SLOT_BASE + ecx + APPDATA.keyboard_mode]
  2524.         stosb
  2525.  
  2526.         pop     esi
  2527.         pop     edi
  2528.  
  2529. .nofillbuf:
  2530.     ; return number of processes
  2531.  
  2532.         mov     eax, [thread_count]
  2533.         mov     [esp + SYSCALL_STACK.eax], eax
  2534.         ret
  2535.  
  2536. .addr_error:    ; if given memory address is illegal
  2537.         mov     [esp + SYSCALL_STACK.eax], -1
  2538.         ret
  2539.  
  2540.  
  2541. ; redraw status
  2542. align 4
  2543. sys_redrawstat:
  2544.         cmp     ebx, 1
  2545.         jne     .no_widgets_away
  2546.         ; buttons away
  2547.         mov     ecx, [current_slot_idx]
  2548. .sys_newba2:
  2549.         mov     edi, [BTN_ADDR]
  2550.         cmp     [edi], dword 0  ; empty button list ?
  2551.         je      .end_of_buttons_away
  2552.         movzx   ebx, word [edi]
  2553.         inc     ebx
  2554.         mov     eax, edi
  2555. .sys_newba:
  2556.         dec     ebx
  2557.         jz      .end_of_buttons_away
  2558.  
  2559.         add     eax, 0x10
  2560.         cmp     cx, [eax]
  2561.         jnz     .sys_newba
  2562.  
  2563.         push    eax ebx ecx
  2564.         mov     ecx, ebx
  2565.         inc     ecx
  2566.         shl     ecx, 4
  2567.         mov     ebx, eax
  2568.         add     eax, 0x10
  2569.         call    memmove
  2570.         dec     dword [edi]
  2571.         pop     ecx ebx eax
  2572.  
  2573.         jmp     .sys_newba2
  2574.  
  2575. .end_of_buttons_away:
  2576.         ret
  2577.  
  2578. .no_widgets_away:
  2579.  
  2580.         cmp     ebx, 2
  2581.         jnz     .srl1
  2582.  
  2583.         mov     edx, [current_slot]      ; return whole screen draw area for this app
  2584.         mov     edx, [edx + APPDATA.window]
  2585.         mov     [edx + WDATA.draw_data.left], 0
  2586.         mov     [edx + WDATA.draw_data.top], 0
  2587.         mov     eax, [_display.width]
  2588.         dec     eax
  2589.         mov     [edx + WDATA.draw_data.right], eax
  2590.         mov     eax, [_display.height]
  2591.         dec     eax
  2592.         mov     [edx + WDATA.draw_data.bottom], eax
  2593.  
  2594. .srl1:
  2595.         ret
  2596.  
  2597. ;ok - 100% work
  2598. ;nt - not tested
  2599. ;---------------------------------------------------------------------------------------------
  2600. ;eax
  2601. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  2602. ;1 - change task. Ret nothing. Block. ok.
  2603. ;2 - performance control
  2604. ; ebx
  2605. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  2606. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  2607. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  2608. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  2609. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  2610. ;eax
  2611. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2612. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2613. ;---------------------------------------------------------------------------------------------
  2614. iglobal
  2615. align 4
  2616. sheduler:
  2617.         dd      sys_sheduler.00
  2618.         dd      change_task
  2619.         dd      sys_sheduler.02
  2620.         dd      sys_sheduler.03
  2621.         dd      sys_sheduler.04
  2622. endg
  2623. sys_sheduler:
  2624. ;rewritten by <Lrz>  29.12.2009
  2625.         jmp     dword [sheduler + ebx*4]
  2626. ;.shed_counter:
  2627. .00:
  2628.         mov     eax, [context_counter]
  2629.         mov     [esp + SYSCALL_STACK.eax], eax
  2630.         ret
  2631.  
  2632. .02:
  2633. ;.perf_control:
  2634.         inc     ebx                     ;before ebx=2, ebx=3
  2635.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  2636.         jz      cache_disable
  2637.  
  2638.         dec     ebx                     ;ebx=2
  2639.         cmp     ebx, ecx                ;
  2640.         jz      cache_enable            ;if ecx=2 and ebx=2
  2641.  
  2642.         dec     ebx                     ;ebx=1
  2643.         cmp     ebx, ecx
  2644.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  2645.  
  2646.         dec     ebx
  2647.         test    ebx, ecx                ;ebx=0 and ecx=0
  2648.         jz      modify_pce              ;if ecx=0
  2649.  
  2650.         ret
  2651.  
  2652. .03:
  2653. ;.rdmsr_instr:
  2654. ;now counter in ecx
  2655. ;(edx:eax) esi:edi => edx:esi
  2656.         mov     eax, esi
  2657.         mov     ecx, edx
  2658.         rdmsr
  2659.         mov     [esp + SYSCALL_STACK.eax], eax
  2660.         mov     [esp + SYSCALL_STACK.ebx], edx           ;ret in ebx?
  2661.         ret
  2662.  
  2663. .04:
  2664. ;.wrmsr_instr:
  2665. ;now counter in ecx
  2666. ;(edx:eax) esi:edi => edx:esi
  2667.         ; Fast Call MSR can't be destroy
  2668.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  2669.         ; включаются/выключаются расширенные возможности
  2670.         cmp     edx, MSR_SYSENTER_CS
  2671.         je      @f
  2672.         cmp     edx, MSR_SYSENTER_ESP
  2673.         je      @f
  2674.         cmp     edx, MSR_SYSENTER_EIP
  2675.         je      @f
  2676.         cmp     edx, MSR_AMD_STAR
  2677.         je      @f
  2678.  
  2679.         mov     eax, esi
  2680.         mov     ecx, edx
  2681.         wrmsr
  2682.         ; mov   [esp + 32], eax
  2683.         ; mov   [esp + 20], edx ;ret in ebx?
  2684. @@:
  2685.         ret
  2686.  
  2687. cache_disable:
  2688.         mov     eax, cr0
  2689.         or      eax, 01100000_00000000_00000000_00000000b
  2690.         mov     cr0, eax
  2691.         wbinvd  ;set MESI
  2692.         ret
  2693.  
  2694. cache_enable:
  2695.         mov     eax, cr0
  2696.         and     eax, 10011111_11111111_11111111_11111111b
  2697.         mov     cr0, eax
  2698.         ret
  2699.  
  2700. is_cache_enabled:
  2701.         mov     eax, cr0
  2702.         and     eax, 01100000_00000000_00000000_00000000b
  2703.         mov     [esp + SYSCALL_STACK.eax], eax
  2704.         ret
  2705.  
  2706. modify_pce:
  2707.         mov     eax, cr4
  2708. ;       mov ebx,0
  2709. ;       or  bx,100000000b ;pce
  2710. ;       xor eax,ebx ;invert pce
  2711.         bts     eax, 8;pce=cr4[8]
  2712.         mov     cr4, eax
  2713.         mov     [esp + SYSCALL_STACK.eax], eax
  2714.         ret
  2715. ;---------------------------------------------------------------------------------------------
  2716.  
  2717.  
  2718. iglobal
  2719.   cpustring db 'CPU',0
  2720. endg
  2721.  
  2722. uglobal
  2723. background_defined    db    0    ; diamond, 11.04.2006
  2724. endg
  2725. ;-----------------------------------------------------------------------------
  2726. align 4
  2727. checkmisc:
  2728.         cmp     [ctrl_alt_del], 1
  2729.         jne     .nocpustart
  2730.  
  2731.         mov     ebp, cpustring
  2732.         call    fs_execute_from_sysdir
  2733.  
  2734.         mov     [ctrl_alt_del], 0
  2735. ;--------------------------------------
  2736. align 4
  2737. .nocpustart:
  2738.         cmp     [mouse_active], 1
  2739.         jne     .mouse_not_active
  2740.         mov     [mouse_active], 0
  2741.  
  2742.         xor     edi, edi
  2743.         mov     ebx, window_data
  2744.  
  2745.         mov     ecx, [thread_count]
  2746.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  2747.         shl     eax, BSF sizeof.APPDATA
  2748.         push    eax
  2749.  
  2750.         movzx   eax, word [MOUSE_X]
  2751.         movzx   edx, word [MOUSE_Y]
  2752. ;--------------------------------------
  2753. align 4
  2754. .set_mouse_event:
  2755.         add     edi, sizeof.APPDATA
  2756.         add     ebx, sizeof.WDATA
  2757.         test    [SLOT_BASE + edi + APPDATA.event_mask], EVM_MOUSE_FILTER
  2758.         jz      .pos_filter
  2759.  
  2760.         cmp     edi, [esp]                      ; skip if filtration active
  2761.         jne     .skip
  2762. ;--------------------------------------
  2763. align 4
  2764. .pos_filter:
  2765.         test    [SLOT_BASE + edi + APPDATA.event_mask], EVM_CURSOR_FILTER
  2766.         jz      .set
  2767.  
  2768.         mov     esi, [ebx + WDATA.box.left]
  2769.         cmp     eax, esi
  2770.         jb      .skip
  2771.         add     esi, [ebx + WDATA.box.width]
  2772.         cmp     eax, esi
  2773.         ja      .skip
  2774.  
  2775.         mov     esi, [ebx + WDATA.box.top]
  2776.         cmp     edx, esi
  2777.         jb      .skip
  2778.         add     esi, [ebx + WDATA.box.height]
  2779.         cmp     edx, esi
  2780.         ja      .skip
  2781. ;--------------------------------------
  2782. align 4
  2783. .set:
  2784.         or      [SLOT_BASE + edi + APPDATA.occurred_events], EVENT_MOUSE
  2785. ;--------------------------------------
  2786. align 4
  2787. .skip:
  2788.         loop    .set_mouse_event
  2789.  
  2790.         pop     eax
  2791. ;--------------------------------------
  2792. align 4
  2793. .mouse_not_active:
  2794.         cmp     [REDRAW_BACKGROUND], 0  ; background update ?
  2795.         jz      nobackgr
  2796.  
  2797.         cmp     [background_defined], 0
  2798.         jz      nobackgr
  2799. ;--------------------------------------
  2800. align 4
  2801. backgr:
  2802.         mov     eax, [background_window + WDATA.draw_data.left]
  2803.         shl     eax, 16
  2804.         add     eax, [background_window + WDATA.draw_data.right]
  2805.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  2806.  
  2807.         mov     eax, [background_window + WDATA.draw_data.top]
  2808.         shl     eax, 16
  2809.         add     eax, [background_window + WDATA.draw_data.bottom]
  2810.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  2811.  
  2812.         call    drawbackground
  2813. ;        DEBUGF  1, "K : drawbackground\n"
  2814. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  2815. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  2816. ;--------- set event 5 start ----------
  2817.         push    ecx edi
  2818.         mov     edi, window_data
  2819.         mov     ecx, [thread_count]
  2820. ;--------------------------------------
  2821. align 4
  2822. set_bgr_event:
  2823.         add     edi, sizeof.WDATA
  2824.         mov     eax, [BG_Rect_X_left_right]
  2825.         mov     edx, [BG_Rect_Y_top_bottom]
  2826.         cmp     [edi + WDATA.draw_bgr_x], 0
  2827.         jz      .set
  2828. .join:
  2829.         cmp     word [edi + WDATA.draw_bgr_x], ax
  2830.         jae     @f
  2831.         mov     word [edi + WDATA.draw_bgr_x], ax
  2832. @@:
  2833.         shr     eax, 16
  2834.         cmp     word [edi + WDATA.draw_bgr_x + 2], ax
  2835.         jbe     @f
  2836.         mov     word [edi + WDATA.draw_bgr_x + 2], ax
  2837. @@:
  2838.         cmp     word [edi + WDATA.draw_bgr_y], dx
  2839.         jae     @f
  2840.         mov     word [edi + WDATA.draw_bgr_y], dx
  2841. @@:
  2842.         shr     edx, 16
  2843.         cmp     word [edi + WDATA.draw_bgr_y+2], dx
  2844.         jbe     @f
  2845.         mov     word [edi + WDATA.draw_bgr_y+2], dx
  2846. @@:
  2847.         jmp     .common
  2848. .set:
  2849.         mov     [edi + WDATA.draw_bgr_x], eax
  2850.         mov     [edi + WDATA.draw_bgr_y], edx
  2851. .common:
  2852.         mov     eax, [edi + WDATA.thread]
  2853.         test    eax, eax
  2854.         jz      @f
  2855.         or      [eax + APPDATA.occurred_events], EVENT_BACKGROUND
  2856. @@:
  2857.         loop    set_bgr_event
  2858.         pop     edi ecx
  2859. ;--------- set event 5 stop -----------
  2860.         dec     [REDRAW_BACKGROUND]     ; got new update request?
  2861.         jnz     backgr
  2862.  
  2863.         xor     eax, eax
  2864.         mov     [background_window + WDATA.draw_data.left], eax
  2865.         mov     [background_window + WDATA.draw_data.top], eax
  2866.         mov     [background_window + WDATA.draw_data.right], eax
  2867.         mov     [background_window + WDATA.draw_data.bottom], eax
  2868. ;--------------------------------------
  2869. align 4
  2870. nobackgr:
  2871. ; system shutdown request
  2872.         cmp     [SYS_SHUTDOWN], byte 0
  2873.         je      noshutdown
  2874.  
  2875.         mov     edx, [shutdown_processes]
  2876.  
  2877.         cmp     [SYS_SHUTDOWN], dl
  2878.         jne     noshutdown
  2879.  
  2880.         lea     ecx, [edx-1]
  2881.         mov     edx, SLOT_BASE + sizeof.APPDATA*2 ;OS_BASE+0x3040
  2882.         jecxz   no_mark_system_shutdown
  2883. ;--------------------------------------
  2884. align 4
  2885. markz:
  2886.         push    ecx edx
  2887.         cmp     [edx + APPDATA.state], TSTATE_FREE
  2888.         jz      .nokill
  2889.         cmp     [edx + APPDATA.process], sys_proc
  2890.         jz      .nokill
  2891.         call    request_terminate
  2892.         jmp     .common
  2893. .nokill:
  2894.         dec     byte [SYS_SHUTDOWN]
  2895.         xor     eax, eax
  2896. .common:
  2897.         pop     edx ecx
  2898.         test    eax, eax
  2899.         jz      @f
  2900.         mov     [edx + APPDATA.state], TSTATE_ZOMBIE
  2901. @@:
  2902.         add     edx, sizeof.APPDATA
  2903.         loop    markz
  2904.         call    wakeup_osloop
  2905. ;--------------------------------------
  2906. align 4
  2907. @@:
  2908. no_mark_system_shutdown:
  2909.         dec     byte [SYS_SHUTDOWN]
  2910.         je      system_shutdown
  2911. ;--------------------------------------
  2912. align 4
  2913. noshutdown:
  2914.         mov     eax, [thread_count]           ; termination
  2915.         mov     ebx, SLOT_BASE + sizeof.APPDATA + APPDATA.state
  2916.         mov     esi, 1
  2917. ;--------------------------------------
  2918. align 4
  2919. newct:
  2920.         mov     cl, [ebx]
  2921.         cmp     cl, TSTATE_ZOMBIE
  2922.         jz      .terminate
  2923.  
  2924.         cmp     cl, TSTATE_TERMINATING
  2925.         jnz     .noterminate
  2926. .terminate:
  2927.         pushad
  2928.         push    esi
  2929.         mov     ecx, dword[ebx - APPDATA.state + APPDATA.window]
  2930.         call    restore_default_cursor_before_killing
  2931.  
  2932.         pop     esi
  2933.         call    terminate
  2934.         popad
  2935.         cmp     byte [SYS_SHUTDOWN], 0
  2936.         jz      .noterminate
  2937.         dec     byte [SYS_SHUTDOWN]
  2938.         je      system_shutdown
  2939.  
  2940. .noterminate:
  2941.         add     ebx, sizeof.APPDATA
  2942.         inc     esi
  2943.         dec     eax
  2944.         jnz     newct
  2945.         ret
  2946. ;-----------------------------------------------------------------------------
  2947. align 4
  2948. ; eax - ptr to WDATA
  2949. redrawscreen:
  2950. ; eax , if process window_data base is eax, do not set flag/limits
  2951.  
  2952.         pushad
  2953.         push    eax
  2954.  
  2955. ;;;         mov   ebx,2
  2956. ;;;         call  delay_hs
  2957.  
  2958.          ;mov   ecx,0               ; redraw flags for apps
  2959.         xor     ecx, ecx
  2960. ;--------------------------------------
  2961. align 4
  2962. newdw2:
  2963.         inc     ecx
  2964.         push    ecx
  2965.  
  2966.         mov     eax, ecx
  2967.         shl     eax, BSF sizeof.WDATA
  2968.         add     eax, window_data
  2969.  
  2970.         cmp     eax, [esp+4]
  2971.         je      not_this_task
  2972.                                    ; check if window in redraw area
  2973.         mov     edi, eax
  2974.  
  2975.         cmp     ecx, 1             ; limit for background
  2976.         jz      bgli
  2977.  
  2978.         mov     eax, [esp+4]        ;if upper in z-position - no redraw
  2979.         test    eax, eax
  2980.         jz      @f
  2981.         mov     al, [eax + WDATA.z_modif]
  2982.         cmp     [edi + WDATA.z_modif], al
  2983.         jg      ricino
  2984.       @@:
  2985.  
  2986.         mov     eax, [edi + WDATA.box.left]
  2987.         mov     ebx, [edi + WDATA.box.top]
  2988.  
  2989.         cmp     ebx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  2990.         jae     ricino
  2991.  
  2992.         cmp     eax, [draw_limits.right] ; ecx = area x end     eax = window x start
  2993.         jae     ricino
  2994.  
  2995.         mov     eax, [edi + WDATA.box.left]
  2996.         mov     ebx, [edi + WDATA.box.top]
  2997.         mov     ecx, [edi + WDATA.box.width]
  2998.         mov     edx, [edi + WDATA.box.height]
  2999.         add     ecx, eax
  3000.         add     edx, ebx
  3001.  
  3002.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3003.         cmp     edx, eax
  3004.         jb      ricino
  3005.  
  3006.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3007.         cmp     ecx, eax
  3008.         jb      ricino
  3009. ;--------------------------------------
  3010. align 4
  3011. bgli:
  3012.         cmp     dword[esp], 1  ; check index in window_data array, 1 - idle
  3013.         jnz     .az
  3014.  
  3015.         cmp     [REDRAW_BACKGROUND], 0
  3016.         jz      .az
  3017.  
  3018.         mov     dl, 0
  3019.         mov     ebx, [draw_limits.left]
  3020.         cmp     ebx, [edi + WDATA.draw_data.left]
  3021.         jae     @f
  3022.  
  3023.         mov     [edi + WDATA.draw_data.left], ebx
  3024.         mov     dl, 1
  3025. ;--------------------------------------
  3026. align 4
  3027. @@:
  3028.         mov     ebx, [draw_limits.top]
  3029.         cmp     ebx, [edi + WDATA.draw_data.top]
  3030.         jae     @f
  3031.  
  3032.         mov     [edi + WDATA.draw_data.top], ebx
  3033.         mov     dl, 1
  3034. ;--------------------------------------
  3035. align 4
  3036. @@:
  3037.         mov     ebx, [draw_limits.right]
  3038.         cmp     ebx, [edi + WDATA.draw_data.right]
  3039.         jbe     @f
  3040.  
  3041.         mov     [edi + WDATA.draw_data.right], ebx
  3042.         mov     dl, 1
  3043. ;--------------------------------------
  3044. align 4
  3045. @@:
  3046.         mov     ebx, [draw_limits.bottom]
  3047.         cmp     ebx, [edi + WDATA.draw_data.bottom]
  3048.         jbe     @f
  3049.  
  3050.         mov     [edi + WDATA.draw_data.bottom], ebx
  3051.         mov     dl, 1
  3052. ;--------------------------------------
  3053. align 4
  3054. @@:
  3055.         add     [REDRAW_BACKGROUND], dl
  3056.         call    wakeup_osloop
  3057.         jmp     newdw8
  3058. ;--------------------------------------
  3059. align 4
  3060. .az:
  3061.         mov     ebx, [draw_limits.left]        ; set limits
  3062.         mov     [edi + WDATA.draw_data.left], ebx
  3063.         mov     ebx, [draw_limits.top]
  3064.         mov     [edi + WDATA.draw_data.top], ebx
  3065.         mov     ebx, [draw_limits.right]
  3066.         mov     [edi + WDATA.draw_data.right], ebx
  3067.         mov     ebx, [draw_limits.bottom]
  3068.         mov     [edi + WDATA.draw_data.bottom], ebx
  3069.  
  3070.         cmp     dword [esp], 1  ; check idle thread
  3071.         jne     nobgrd
  3072.         inc     [REDRAW_BACKGROUND]
  3073.         call    wakeup_osloop
  3074. ;--------------------------------------
  3075. align 4
  3076. newdw8:
  3077. nobgrd:
  3078. ;--------------------------------------
  3079.         push    edi ebp
  3080.         mov     edi, [esp+8]
  3081.         cmp     edi, 1
  3082.         je      .found
  3083.  
  3084.         mov     eax, [draw_limits.left]
  3085.         mov     ebx, [draw_limits.top]
  3086.         mov     ecx, [draw_limits.right]
  3087.         sub     ecx, eax
  3088.         test    ecx, ecx
  3089.         jz      .not_found
  3090.  
  3091.         mov     edx, [draw_limits.bottom]
  3092.         sub     edx, ebx
  3093.         test    edx, edx
  3094.         jz      .not_found
  3095.  
  3096. ; eax - x, ebx - y
  3097. ; ecx - size x, edx - size y
  3098.         add     ebx, edx
  3099. ;--------------------------------------
  3100. align 4
  3101. .start_y:
  3102.         push    ecx
  3103. ;--------------------------------------
  3104. align 4
  3105. .start_x:
  3106.         add     eax, ecx
  3107.         mov     ebp, [d_width_calc_area + ebx*4]
  3108.         add     ebp, [_display.win_map]
  3109.         movzx   ebp, byte[eax+ebp] ; get value for current point
  3110.         cmp     ebp, edi
  3111.         jne     @f
  3112.  
  3113.         pop     ecx
  3114.         jmp     .found
  3115. ;--------------------------------------
  3116. align 4
  3117. @@:
  3118.         sub     eax, ecx
  3119.  
  3120.         dec     ecx
  3121.         jns     .start_x
  3122.  
  3123.         pop     ecx
  3124.         dec     ebx
  3125.         dec     edx
  3126.         jns     .start_y
  3127. ;--------------------------------------
  3128. align 4
  3129. .not_found:
  3130.         pop     ebp edi
  3131.         jmp     ricino
  3132. ;--------------------------------------
  3133. align 4
  3134. .found:
  3135.         pop     ebp edi
  3136.  
  3137.         mov     [edi + WDATA.fl_redraw], WSTATE_REDRAW  ; mark as redraw
  3138. ;--------------------------------------
  3139. align 4
  3140. ricino:
  3141. not_this_task:
  3142.         pop     ecx
  3143.  
  3144.         cmp     ecx, [thread_count]
  3145.         jle     newdw2
  3146.  
  3147.         pop     eax
  3148.         popad
  3149.         ret
  3150. ;-----------------------------------------------------------------------------
  3151. align 4
  3152. calculatebackground:   ; background
  3153.         mov     edi, [_display.win_map]              ; set os to use all pixels
  3154.         mov     eax, 0x01010101
  3155.         mov     ecx, [_display.win_map_size]
  3156.         shr     ecx, 2
  3157.         rep stosd
  3158.         mov     byte[background_window + WDATA.z_modif], ZPOS_DESKTOP
  3159.         mov     [REDRAW_BACKGROUND], 0
  3160.         ret
  3161. ;-----------------------------------------------------------------------------
  3162. uglobal
  3163.   imax    dd 0x0
  3164. endg
  3165. ;-----------------------------------------------------------------------------
  3166. align 4
  3167. delay_ms:     ; delay in 1/1000 sec
  3168.         pushad
  3169.  
  3170.         cmp     [hpet_base], 0
  3171.         jz      .no_hpet
  3172.         mov     eax, esi
  3173.         mov     edx, 1_000_000 ; ms to ns
  3174.         mul     edx
  3175.         mov     ebx, edx
  3176.         mov     ecx, eax
  3177.  
  3178.         push    ecx
  3179.         call    get_clock_ns
  3180.         pop     ecx
  3181.         mov     edi, edx
  3182.         mov     esi, eax
  3183. .wait:
  3184.         push    ecx
  3185.         call    get_clock_ns
  3186.         pop     ecx
  3187.         sub     eax, esi
  3188.         sbb     edx, edi
  3189.         sub     eax, ecx
  3190.         sbb     edx, ebx
  3191.         jc      .wait
  3192.         jmp     .done
  3193.  
  3194. .no_hpet:
  3195.         mov     ecx, esi
  3196.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3197.         imul    ecx, 33941
  3198.         shr     ecx, 9
  3199.         ; </CPU clock fix>
  3200.  
  3201.         in      al, 0x61
  3202.         and     al, 0x10
  3203.         mov     ah, al
  3204.         cld
  3205.  
  3206. .cnt1:
  3207.         in      al, 0x61
  3208.         and     al, 0x10
  3209.         cmp     al, ah
  3210.         jz      .cnt1
  3211.  
  3212.         mov     ah, al
  3213.         loop    .cnt1
  3214.  
  3215. .done:
  3216.         popad
  3217.         ret
  3218. ;-----------------------------------------------------------------------------
  3219. align 4
  3220. set_app_param:
  3221.         mov     edi, [current_slot]
  3222.         xchg    ebx, [edi + APPDATA.event_mask] ; set new event mask
  3223.         mov     [esp + SYSCALL_STACK.eax], ebx  ; return old mask value
  3224.         ret
  3225. ;-----------------------------------------------------------------------------
  3226.  
  3227. ; this is for syscall
  3228. proc delay_hs_unprotected
  3229.         call    unprotect_from_terminate
  3230.         call    delay_hs
  3231.         call    protect_from_terminate
  3232.         ret
  3233. endp
  3234.  
  3235. if 1
  3236. align 4
  3237. delay_hs:     ; delay in 1/100 secs
  3238. ; ebx = delay time
  3239.         pushad
  3240.         push    ebx
  3241.         xor     esi, esi
  3242.         mov     ecx, MANUAL_DESTROY
  3243.         call    create_event
  3244.         test    eax, eax
  3245.         jz      .done
  3246.  
  3247.         mov     ebx, edx
  3248.         mov     ecx, [esp]
  3249.         push    edx
  3250.         push    eax
  3251.         call    wait_event_timeout
  3252.         pop     eax
  3253.         pop     ebx
  3254.         call    destroy_event
  3255. .done:
  3256.         add     esp, 4
  3257.         popad
  3258.         ret
  3259.  
  3260. else
  3261.  
  3262. align 4
  3263. delay_hs:     ; delay in 1/100 secs
  3264. ; ebx = delay time
  3265.         push    ecx
  3266.         push    edx
  3267.  
  3268.         mov     edx, [timer_ticks]
  3269. ;--------------------------------------
  3270. align 4
  3271. .newtic:
  3272.         mov     ecx, [timer_ticks]
  3273.         sub     ecx, edx
  3274.         cmp     ecx, ebx
  3275.         jae     .zerodelay
  3276.  
  3277.         call    change_task
  3278.  
  3279.         jmp     .newtic
  3280. ;--------------------------------------
  3281. align 4
  3282. .zerodelay:
  3283.         pop     edx
  3284.         pop     ecx
  3285.         ret
  3286. end if
  3287.  
  3288. ;-----------------------------------------------------------------------------
  3289. align 16        ;very often call this subrutine
  3290. memmove:       ; memory move in bytes
  3291. ; eax = from
  3292. ; ebx = to
  3293. ; ecx = no of bytes
  3294.         test    ecx, ecx
  3295.         jle     .ret
  3296.  
  3297.         push    esi edi ecx
  3298.  
  3299.         mov     edi, ebx
  3300.         mov     esi, eax
  3301.  
  3302.         test    ecx, not 11b
  3303.         jz      @f
  3304.  
  3305.         push    ecx
  3306.         shr     ecx, 2
  3307.         rep movsd
  3308.         pop     ecx
  3309.         and     ecx, 11b
  3310.         jz      .finish
  3311. ;--------------------------------------
  3312. align 4
  3313. @@:
  3314.         rep movsb
  3315. ;--------------------------------------
  3316. align 4
  3317. .finish:
  3318.         pop     ecx edi esi
  3319. ;--------------------------------------
  3320. align 4
  3321. .ret:
  3322.         ret
  3323. ;-----------------------------------------------------------------------------
  3324.  
  3325. ; in: eax = port
  3326. ;     ebp = subfunction
  3327. ;          0 - set access
  3328. ;          1 - clear access
  3329. ; out: not return value
  3330. align 4
  3331. set_io_access_rights:
  3332.         push    edi eax
  3333.         mov     edi, tss._io_map_0
  3334.  
  3335.         test    ebp, ebp         ; enable access - ebp = 0
  3336.         jnz     .siar1
  3337.  
  3338.         btr     [edi], eax
  3339.         pop     eax edi
  3340.         ret
  3341. .siar1:
  3342.         bts     [edi], eax      ; disable access - ebp = 1
  3343.         pop     eax edi
  3344.         ret
  3345.  
  3346. align 4
  3347. ; @brief ReservePortArea and FreePortArea
  3348. ; @param edx number end arrea of ports (include last number of port)
  3349. ; @param ecx number start arrea of ports
  3350. ; @param ebx sub function 0 - reserve, 1 - free
  3351. ; @param eax 46 - number function
  3352. ; @returns  eax = 0 - succesful eax = 1 - error
  3353. syscall_reserveportarea:        ; ReservePortArea and FreePortArea
  3354.  
  3355.         call    r_f_port_area
  3356.         mov     [esp + SYSCALL_STACK.eax], eax
  3357.         ret
  3358.  
  3359. ;reserve/free group of ports
  3360. ;  * eax = 46 - number function
  3361. ;  * ebx = 0 - reserve, 1 - free
  3362. ;  * ecx = number start arrea of ports
  3363. ;  * edx = number end arrea of ports (include last number of port)
  3364. ;Return value:
  3365. ;  * eax = 0 - succesful
  3366. ;  * eax = 1 - error
  3367. ;  * The system has reserve this ports:
  3368. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3369. ;destroys eax,ebx, ebp
  3370. r_f_port_area:
  3371.  
  3372.         test    ebx, ebx
  3373.         jnz     .free_port_area
  3374.  
  3375.         cmp     ecx, edx      ; beginning > end ?
  3376.         ja      .err
  3377.         cmp     edx, 65536    ;test ebx, not 0xffff
  3378.         jae     .err
  3379.         mov     eax, [RESERVED_PORTS]
  3380.         test    eax, eax      ; no reserved areas ?
  3381.         je      .rpal2
  3382.         cmp     eax, 255      ; max reserved
  3383.         jae     .err
  3384.  .rpal3:
  3385.         mov     ebx, eax
  3386.         shl     ebx, 4   ;16 byte is sizeof item in RESERVED_PORTS table
  3387.         add     ebx, RESERVED_PORTS
  3388.         cmp     ecx, [ebx+8]
  3389.         ja      .rpal4
  3390.         cmp     edx, [ebx+4]
  3391.         jae     .err
  3392.  .rpal4:
  3393.         dec     eax
  3394.         jnz     .rpal3
  3395.         jmp     .rpal2
  3396. .err:
  3397.         xor     eax, eax
  3398.         inc     eax
  3399.         ret
  3400.  
  3401.    .rpal2:
  3402.      ; enable port access at port IO map
  3403.         pushad                        ; start enable io map
  3404.         mov     eax, ecx
  3405.         xor     ebp, ebp               ; enable - eax = port
  3406.         cli
  3407. .new_port_access:
  3408.         call    set_io_access_rights
  3409.  
  3410.         inc     eax
  3411.         cmp     eax, edx
  3412.         jbe     .new_port_access
  3413.  
  3414.         sti
  3415.         popad                         ; end enable io map
  3416.  
  3417.         mov     eax, [RESERVED_PORTS]
  3418.         inc     eax
  3419.         mov     [RESERVED_PORTS], eax
  3420.         shl     eax, 4
  3421.         add     eax, RESERVED_PORTS
  3422.         mov     ebx, [current_slot]
  3423.         mov     ebx, [ebx + APPDATA.tid]
  3424.         mov     [eax], ebx   ; tid
  3425.         mov     [eax+4], ecx ;start port
  3426.         mov     [eax+8], edx ;finish port
  3427.  
  3428.         xor     eax, eax
  3429.         ret
  3430.  
  3431. .free_port_area:
  3432.  
  3433.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  3434.         test    eax, eax
  3435.         jz      .frpal2
  3436.         mov     ebx, [current_slot]
  3437.         mov     ebx, [ebx + APPDATA.tid]
  3438.    .frpal3:
  3439.         mov     edi, eax
  3440.         shl     edi, 4
  3441.         add     edi, RESERVED_PORTS
  3442.         cmp     ebx, [edi]
  3443.         jne     .frpal4
  3444.         cmp     ecx, [edi+4]
  3445.         jne     .frpal4
  3446.         cmp     edx, [edi+8]
  3447.         jne     .frpal4
  3448.         jmp     .frpal1
  3449.    .frpal4:
  3450.         dec     eax
  3451.         jnz     .frpal3
  3452.    .frpal2:
  3453.         inc     eax
  3454.         ret
  3455.    .frpal1:
  3456.         push    ecx
  3457.         mov     ecx, 256
  3458.         sub     ecx, eax
  3459.         shl     ecx, 4
  3460.         mov     esi, edi
  3461.         add     esi, 16
  3462.         cld
  3463.         rep movsb
  3464.  
  3465.         dec     dword [RESERVED_PORTS]
  3466. ;disable port access at port IO map
  3467.                         ; start disable io map
  3468.         pop     eax     ;start port
  3469.         ;cmp     edx, 65536
  3470.         ;jge     no_mask_io
  3471.  
  3472.         xor     ebp, ebp
  3473.         inc     ebp
  3474. .new_port_access_disable:           ; disable - eax = port
  3475.         call    set_io_access_rights
  3476.  
  3477.         inc     eax
  3478.         cmp     eax, edx
  3479.         jbe     .new_port_access_disable
  3480.                                     ; end disable io map
  3481.         xor     eax, eax
  3482.         ret
  3483. ;-----------------------------------------------------------------------------
  3484. align 4
  3485. drawbackground:
  3486.         cmp     [BgrDrawMode], 1
  3487.         jne     .bgrstr
  3488.         call    vesa20_drawbackground_tiled
  3489.         call    __sys_draw_pointer
  3490.         ret
  3491. ;--------------------------------------
  3492. align 4
  3493. .bgrstr:
  3494.         call    vesa20_drawbackground_stretch
  3495.         call    __sys_draw_pointer
  3496.         ret
  3497. ;-----------------------------------------------------------------------------
  3498. align 4
  3499. syscall_putimage:                       ; PutImage
  3500. ; add check pointer
  3501.         push    ecx
  3502.         mov     ax, cx
  3503.         shr     ecx, 16
  3504.         imul    eax, ecx
  3505.         lea     eax, [eax*3]
  3506.         stdcall is_region_userspace, ebx, eax
  3507.         pop     ecx
  3508.         jnz     sys_putimage.exit
  3509.  
  3510. sys_putimage:
  3511.         test    ecx, 0x80008000
  3512.         jnz     .exit
  3513.         test    ecx, 0x0000FFFF
  3514.         jz      .exit
  3515.         test    ecx, 0xFFFF0000
  3516.         jnz     @f
  3517. ;--------------------------------------
  3518. align 4
  3519. .exit:
  3520.         ret
  3521. ;--------------------------------------
  3522. align 4
  3523. @@:
  3524.         mov     edi, [current_slot]
  3525.         mov     edi, [edi + APPDATA.window]
  3526.         add     dx, word[edi + WDATA.clientbox.top]
  3527.         rol     edx, 16
  3528.         add     dx, word[edi + WDATA.clientbox.left]
  3529.         rol     edx, 16
  3530. ;--------------------------------------
  3531. align 4
  3532. .forced:
  3533.         push    ebp esi 0
  3534.         mov     ebp, putimage_get24bpp
  3535.         mov     esi, putimage_init24bpp
  3536. ;--------------------------------------
  3537. align 4
  3538. sys_putimage_bpp:
  3539.         call    vesa20_putimage
  3540.         pop     ebp esi ebp
  3541.         ret
  3542. ;        jmp     [draw_pointer]
  3543. ;-----------------------------------------------------------------------------
  3544. align 4
  3545. sys_putimage_palette:
  3546. ; ebx = pointer to image
  3547. ; ecx = [xsize]*65536 + [ysize]
  3548. ; edx = [xstart]*65536 + [ystart]
  3549. ; esi = number of bits per pixel, must be 8, 24 or 32
  3550. ; edi = pointer to palette
  3551. ; ebp = row delta
  3552. ; check pointer
  3553.         push    ecx esi
  3554.         mov     ax, cx
  3555.         shr     ecx, 16
  3556.         imul    eax, ecx
  3557. ;        imul    eax, esi ; eax*count bit in 1 pixel
  3558. ;        shr     eax, 3
  3559.         stdcall is_region_userspace, ebx, eax
  3560.         pop     esi ecx
  3561.         jnz     sys_putimage.exit
  3562.  
  3563.         mov     eax, [current_slot]
  3564.         mov     eax, [eax + APPDATA.window]
  3565.         add     dx, word [eax + WDATA.clientbox.top]
  3566.         rol     edx, 16
  3567.         add     dx, word [eax + WDATA.clientbox.left]
  3568.         rol     edx, 16
  3569. ;--------------------------------------
  3570. align 4
  3571. .forced:
  3572.         cmp     esi, 1
  3573.         jnz     @f
  3574.         push    edi
  3575.         mov     eax, [edi+4]
  3576.         sub     eax, [edi]
  3577.         push    eax
  3578.         push    dword [edi]
  3579.         push    0ffffff80h
  3580.         mov     edi, esp
  3581.         call    put_mono_image
  3582.         add     esp, 12
  3583.         pop     edi
  3584.         ret
  3585. ;--------------------------------------
  3586. align 4
  3587. @@:
  3588.         cmp     esi, 2
  3589.         jnz     @f
  3590.         push    edi
  3591.         push    0ffffff80h
  3592.         mov     edi, esp
  3593.         call    put_2bit_image
  3594.         pop     eax
  3595.         pop     edi
  3596.         ret
  3597. ;--------------------------------------
  3598. align 4
  3599. @@:
  3600.         cmp     esi, 4
  3601.         jnz     @f
  3602.         push    edi
  3603.         push    0ffffff80h
  3604.         mov     edi, esp
  3605.         call    put_4bit_image
  3606.         pop     eax
  3607.         pop     edi
  3608.         ret
  3609. ;--------------------------------------
  3610. align 4
  3611. @@:
  3612.         push    ebp esi ebp
  3613.         cmp     esi, 8
  3614.         jnz     @f
  3615.         mov     ebp, putimage_get8bpp
  3616.         mov     esi, putimage_init8bpp
  3617.         jmp     sys_putimage_bpp
  3618. ;--------------------------------------
  3619. align 4
  3620. @@:
  3621.         cmp     esi, 9
  3622.         jnz     @f
  3623.         mov     ebp, putimage_get9bpp
  3624.         mov     esi, putimage_init9bpp
  3625.         jmp     sys_putimage_bpp
  3626. ;--------------------------------------
  3627. align 4
  3628. @@:
  3629.         cmp     esi, 15
  3630.         jnz     @f
  3631.         mov     ebp, putimage_get15bpp
  3632.         mov     esi, putimage_init15bpp
  3633.         jmp     sys_putimage_bpp
  3634. ;--------------------------------------
  3635. align 4
  3636. @@:
  3637.         cmp     esi, 16
  3638.         jnz     @f
  3639.         mov     ebp, putimage_get16bpp
  3640.         mov     esi, putimage_init16bpp
  3641.         jmp     sys_putimage_bpp
  3642. ;--------------------------------------
  3643. align 4
  3644. @@:
  3645.         cmp     esi, 24
  3646.         jnz     @f
  3647.         mov     ebp, putimage_get24bpp
  3648.         mov     esi, putimage_init24bpp
  3649.         jmp     sys_putimage_bpp
  3650. ;--------------------------------------
  3651. align 4
  3652. @@:
  3653.         cmp     esi, 32
  3654.         jnz     @f
  3655.         mov     ebp, putimage_get32bpp
  3656.         mov     esi, putimage_init32bpp
  3657.         jmp     sys_putimage_bpp
  3658. ;--------------------------------------
  3659. align 4
  3660. @@:
  3661.         pop     ebp esi ebp
  3662.         ret
  3663. ;-----------------------------------------------------------------------------
  3664. align 4
  3665. put_mono_image:
  3666.         push    ebp esi ebp
  3667.         mov     ebp, putimage_get1bpp
  3668.         mov     esi, putimage_init1bpp
  3669.         jmp     sys_putimage_bpp
  3670. ;-----------------------------------------------------------------------------
  3671. align 4
  3672. put_2bit_image:
  3673.         push    ebp esi ebp
  3674.         mov     ebp, putimage_get2bpp
  3675.         mov     esi, putimage_init2bpp
  3676.         jmp     sys_putimage_bpp
  3677. ;-----------------------------------------------------------------------------
  3678. align 4
  3679. put_4bit_image:
  3680.         push    ebp esi ebp
  3681.         mov     ebp, putimage_get4bpp
  3682.         mov     esi, putimage_init4bpp
  3683.         jmp     sys_putimage_bpp
  3684. ;-----------------------------------------------------------------------------
  3685. align 4
  3686. putimage_init24bpp:
  3687.         lea     eax, [eax*3]
  3688. putimage_init8bpp:
  3689. putimage_init9bpp:
  3690.         ret
  3691. ;-----------------------------------------------------------------------------
  3692. align 16
  3693. putimage_get24bpp:
  3694.         movzx   eax, byte [esi+2]
  3695.         shl     eax, 16
  3696.         mov     ax, [esi]
  3697.         add     esi, 3
  3698.         ret     4
  3699. ;-----------------------------------------------------------------------------
  3700. align 16
  3701. putimage_get8bpp:
  3702.         movzx   eax, byte [esi]
  3703.         push    edx
  3704.         mov     edx, [esp+8]
  3705.         mov     eax, [edx + eax*4]
  3706.         pop     edx
  3707.         inc     esi
  3708.         ret     4
  3709. ;-----------------------------------------------------------------------------
  3710. align 16
  3711. putimage_get9bpp:
  3712.         lodsb
  3713.         mov     ah, al
  3714.         shl     eax, 8
  3715.         mov     al, ah
  3716.         ret     4
  3717. ;-----------------------------------------------------------------------------
  3718. align 4
  3719. putimage_init1bpp:
  3720.         add     eax, ecx
  3721.         push    ecx
  3722.         add     eax, 7
  3723.         add     ecx, 7
  3724.         shr     eax, 3
  3725.         shr     ecx, 3
  3726.         sub     eax, ecx
  3727.         pop     ecx
  3728.         ret
  3729. ;-----------------------------------------------------------------------------
  3730. align 16
  3731. putimage_get1bpp:
  3732.         push    edx
  3733.         mov     edx, [esp+8]
  3734.         mov     al, [edx]
  3735.         add     al, al
  3736.         jnz     @f
  3737.         lodsb
  3738.         adc     al, al
  3739. @@:
  3740.         mov     [edx], al
  3741.         sbb     eax, eax
  3742.         and     eax, [edx+8]
  3743.         add     eax, [edx+4]
  3744.         pop     edx
  3745.         ret     4
  3746. ;-----------------------------------------------------------------------------
  3747. align 4
  3748. putimage_init2bpp:
  3749.         add     eax, ecx
  3750.         push    ecx
  3751.         add     ecx, 3
  3752.         add     eax, 3
  3753.         shr     ecx, 2
  3754.         shr     eax, 2
  3755.         sub     eax, ecx
  3756.         pop     ecx
  3757.         ret
  3758. ;-----------------------------------------------------------------------------
  3759. align 16
  3760. putimage_get2bpp:
  3761.         push    edx
  3762.         mov     edx, [esp+8]
  3763.         mov     al, [edx]
  3764.         mov     ah, al
  3765.         shr     al, 6
  3766.         shl     ah, 2
  3767.         jnz     .nonewbyte
  3768.         lodsb
  3769.         mov     ah, al
  3770.         shr     al, 6
  3771.         shl     ah, 2
  3772.         add     ah, 1
  3773. .nonewbyte:
  3774.         mov     [edx], ah
  3775.         mov     edx, [edx+4]
  3776.         movzx   eax, al
  3777.         mov     eax, [edx + eax*4]
  3778.         pop     edx
  3779.         ret     4
  3780. ;-----------------------------------------------------------------------------
  3781. align 4
  3782. putimage_init4bpp:
  3783.         add     eax, ecx
  3784.         push    ecx
  3785.         add     ecx, 1
  3786.         inc     eax      ;add   eax, 1
  3787.         shr     ecx, 1
  3788.         shr     eax, 1
  3789.         sub     eax, ecx
  3790.         pop     ecx
  3791.         ret
  3792. ;-----------------------------------------------------------------------------
  3793. align 16
  3794. putimage_get4bpp:
  3795.         push    edx
  3796.         mov     edx, [esp+8]
  3797.         add     byte [edx], 80h
  3798.         jc      @f
  3799.         movzx   eax, byte [edx+1]
  3800.         mov     edx, [edx+4]
  3801.         and     eax, 0x0F
  3802.         mov     eax, [edx + eax*4]
  3803.         pop     edx
  3804.         ret     4
  3805. @@:
  3806.         movzx   eax, byte [esi]
  3807.         add     esi, 1
  3808.         mov     [edx+1], al
  3809.         shr     eax, 4
  3810.         mov     edx, [edx+4]
  3811.         mov     eax, [edx + eax*4]
  3812.         pop     edx
  3813.         ret     4
  3814. ;-----------------------------------------------------------------------------
  3815. align 4
  3816. putimage_init32bpp:
  3817.         shl     eax, 2
  3818.         ret
  3819. ;-----------------------------------------------------------------------------
  3820. align 16
  3821. putimage_get32bpp:
  3822.         lodsd
  3823.         ret     4
  3824. ;-----------------------------------------------------------------------------
  3825. align 4
  3826. putimage_init15bpp:
  3827. putimage_init16bpp:
  3828.         add     eax, eax
  3829.         ret
  3830. ;-----------------------------------------------------------------------------
  3831. align 16
  3832. putimage_get15bpp:
  3833. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  3834.         push    ecx edx
  3835.         movzx   eax, word [esi]
  3836.         add     esi, 2
  3837.         mov     ecx, eax
  3838.         mov     edx, eax
  3839.         and     eax, 0x1F
  3840.         and     ecx, 0x1F shl 5
  3841.         and     edx, 0x1F shl 10
  3842.         shl     eax, 3
  3843.         shl     ecx, 6
  3844.         shl     edx, 9
  3845.         or      eax, ecx
  3846.         or      eax, edx
  3847.         pop     edx ecx
  3848.         ret     4
  3849. ;-----------------------------------------------------------------------------
  3850. align 16
  3851. putimage_get16bpp:
  3852. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  3853.         push    ecx edx
  3854.         movzx   eax, word [esi]
  3855.         add     esi, 2
  3856.         mov     ecx, eax
  3857.         mov     edx, eax
  3858.         and     eax, 0x1F
  3859.         and     ecx, 0x3F shl 5
  3860.         and     edx, 0x1F shl 11
  3861.         shl     eax, 3
  3862.         shl     ecx, 5
  3863.         shl     edx, 8
  3864.         or      eax, ecx
  3865.         or      eax, edx
  3866.         pop     edx ecx
  3867.         ret     4
  3868. ;-----------------------------------------------------------------------------
  3869. ;align 4
  3870. ; eax x beginning
  3871. ; ebx y beginning
  3872. ; ecx x end
  3873.         ; edx y end
  3874. ; edi color
  3875. ;__sys_drawbar:
  3876. ;        mov     esi, [current_slot]
  3877. ;        mov     esi, [esi + APPDATA.window]
  3878. ;        add     eax, [esi + WDATA.clientbox.left]
  3879. ;        add     ecx, [esi + WDATA.clientbox.left]
  3880. ;        add     ebx, [esi + WDATA.clientbox.top]
  3881. ;        add     edx, [esi + WDATA.clientbox.top]
  3882. ;--------------------------------------
  3883. ;align 4
  3884. ;.forced:
  3885. ;        call    vesa20_drawbar
  3886. ;        call    [draw_pointer]
  3887. ;        ret
  3888. ;-----------------------------------------------------------------------------
  3889. if used _rdtsc
  3890. _rdtsc:
  3891.         bt      [cpu_caps], CAPS_TSC
  3892.         jnc     .ret_rdtsc
  3893.         rdtsc
  3894.         ret
  3895.    .ret_rdtsc:
  3896.         mov     edx, 0xffffffff
  3897.         mov     eax, 0xffffffff
  3898.         ret
  3899. end if
  3900.  
  3901. sys_msg_board_str:
  3902.  
  3903.         pushad
  3904.    @@:
  3905.         cmp     [esi], byte 0
  3906.         je      @f
  3907.         mov     ebx, 1
  3908.         movzx   ecx, byte [esi]
  3909.         call    sys_msg_board
  3910.         inc     esi
  3911.         jmp     @b
  3912.    @@:
  3913.         popad
  3914.         ret
  3915.  
  3916. sys_msg_board_byte:
  3917. ; in: al = byte to display
  3918. ; out: nothing
  3919. ; destroys: nothing
  3920.         pushad
  3921.         mov     ecx, 2
  3922.         shl     eax, 24
  3923.         jmp     @f
  3924.  
  3925. sys_msg_board_word:
  3926. ; in: ax = word to display
  3927. ; out: nothing
  3928. ; destroys: nothing
  3929.         pushad
  3930.         mov     ecx, 4
  3931.         shl     eax, 16
  3932.         jmp     @f
  3933.  
  3934. sys_msg_board_dword:
  3935. ; in: eax = dword to display
  3936. ; out: nothing
  3937. ; destroys: nothing
  3938.         pushad
  3939.         mov     ecx, 8
  3940. @@:
  3941.         push    ecx
  3942.         rol     eax, 4
  3943.         push    eax
  3944.         and     al, 0xF
  3945.         cmp     al, 10
  3946.         sbb     al, 69h
  3947.         das
  3948.         mov     cl, al
  3949.         xor     ebx, ebx
  3950.         inc     ebx
  3951.         call    sys_msg_board
  3952.         pop     eax
  3953.         pop     ecx
  3954.         loop    @b
  3955.         popad
  3956.         ret
  3957.  
  3958. msg_board_data_size = 65536 ; Must be power of two
  3959.  
  3960. uglobal
  3961. msg_board_data  rb  msg_board_data_size
  3962. msg_board_count dd  ?
  3963. endg
  3964.  
  3965. iglobal
  3966. msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
  3967. endg
  3968.  
  3969. sys_msg_board:
  3970. ; ebx=1 -> write, cl = byte to write
  3971. ; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
  3972.         push    eax ebx
  3973.         mov     eax, ebx
  3974.         mov     ebx, ecx
  3975.         mov     ecx, [msg_board_count]
  3976.         cmp     eax, 1
  3977.         jne     .read
  3978.  
  3979. if defined debug_com_base
  3980.         push    dx ax
  3981. @@: ; wait for empty transmit register
  3982.         mov     dx, debug_com_base+5
  3983.         in      al, dx
  3984.         test    al, 1 shl 5
  3985.         jz      @r
  3986.         mov     dx, debug_com_base      ; Output the byte
  3987.         mov     al, bl
  3988.         out     dx, al
  3989.         pop     ax dx
  3990. end if
  3991.  
  3992.         mov     [msg_board_data+ecx], bl
  3993.         cmp     byte [debug_direct_print], 1
  3994.         jnz     .end
  3995.         pusha
  3996.         lea     edx, [msg_board_data+ecx]
  3997.         mov     ecx, 0x40FFFFFF
  3998.         mov     ebx, [msg_board_pos]
  3999.         mov     edi, 1
  4000.         mov     esi, edi ;1
  4001.         call    dtext
  4002.         popa
  4003.         add     word [msg_board_pos+2], 6
  4004.         cmp     word [msg_board_pos+2], 105*6
  4005.         jnc     @f
  4006.         cmp     bl, 10
  4007.         jnz     .end
  4008. @@:
  4009.         mov     word [msg_board_pos+2], 42*6
  4010.         add     word [msg_board_pos], 10
  4011.         mov     eax, [_display.height]
  4012.         sub     eax, 10
  4013.         cmp     ax, word [msg_board_pos]
  4014.         jnc     @f
  4015.         mov     word [msg_board_pos], 10
  4016. @@:
  4017.         pusha
  4018.         mov     eax, [msg_board_pos]
  4019.         movzx   ebx, ax
  4020.         shr     eax, 16
  4021.         mov     edx, 105*6
  4022.         xor     ecx, ecx
  4023.         mov     edi, 1
  4024.         mov     esi, 9
  4025. @@:
  4026.         call    hline
  4027.         inc     ebx
  4028.         dec     esi
  4029.         jnz     @b
  4030.         popa
  4031. .end:
  4032.         inc     ecx
  4033.         and     ecx, msg_board_data_size - 1
  4034.         mov     [msg_board_count], ecx
  4035. .ret:
  4036.         pop     ebx eax
  4037.         ret
  4038.  
  4039. .read:
  4040.         cmp     eax, 2
  4041.         jne     .ret
  4042.         add     esp, 8  ; returning data in ebx and eax, so no need to restore them
  4043.         test    ecx, ecx
  4044.         jnz     @f
  4045.         mov     [esp + SYSCALL_STACK.eax], ecx
  4046.         mov     [esp + SYSCALL_STACK.ebx], ecx
  4047.         ret
  4048. @@:
  4049.         mov     eax, msg_board_data+1
  4050.         mov     ebx, msg_board_data
  4051.         movzx   edx, byte [ebx]
  4052.         call    memmove
  4053.         dec     [msg_board_count]
  4054.         mov     [esp + SYSCALL_STACK.eax], edx
  4055.         mov     [esp + SYSCALL_STACK.ebx], 1
  4056.         ret
  4057.  
  4058. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4059. ;; 61 sys function.                                                ;;
  4060. ;; in eax=61,ebx in [1..3]                                         ;;
  4061. ;; out eax                                                         ;;
  4062. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4063. iglobal
  4064. align 4
  4065. f61call:
  4066.            dd sys_gs.1   ; resolution
  4067.            dd sys_gs.2   ; bits per pixel
  4068.            dd sys_gs.3   ; bytes per scanline
  4069. endg
  4070.  
  4071.  
  4072. align 4
  4073.  
  4074. sys_gs:                         ; direct screen access
  4075.         dec     ebx
  4076.         cmp     ebx, 2
  4077.         ja      .not_support
  4078.         jmp     dword [f61call + ebx*4]
  4079. .not_support:
  4080.         or      [esp + SYSCALL_STACK.eax], -1
  4081.         ret
  4082.  
  4083.  
  4084. .1:                             ; resolution
  4085.         mov     eax, [_display.width]
  4086.         shl     eax, 16
  4087.         mov     ax, word [_display.height]
  4088.         mov     [esp + SYSCALL_STACK.eax], eax
  4089.         ret
  4090. .2:                             ; bits per pixel
  4091.         mov     eax, [_display.bits_per_pixel]
  4092.         mov     [esp + SYSCALL_STACK.eax], eax
  4093.         ret
  4094. .3:                             ; bytes per scanline
  4095.         mov     eax, [_display.lfb_pitch]
  4096.         mov     [esp + SYSCALL_STACK.eax], eax
  4097.         ret
  4098.  
  4099. align 4
  4100. syscall_getscreensize:                  ; GetScreenSize
  4101.         mov     ax, word [_display.width]
  4102.         dec     ax
  4103.         shl     eax, 16
  4104.         mov     ax, word [_display.height]
  4105.         dec     ax
  4106.         mov     [esp + SYSCALL_STACK.eax], eax
  4107.         ret
  4108. ;-----------------------------------------------------------------------------
  4109. align 4
  4110. syscall_cdaudio:
  4111. ; ECX - position of CD/DVD-drive
  4112. ; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
  4113. ; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
  4114. ; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
  4115.         cmp     ecx, 11
  4116.         ja      .exit
  4117.  
  4118.         mov     eax, ecx
  4119.         shr     eax, 2
  4120.         lea     eax, [eax*5]
  4121.         mov     al, [eax + DRIVE_DATA + 1]
  4122.  
  4123.         push    ecx ebx
  4124.         mov     ebx, ecx
  4125.         and     ebx, 11b
  4126.         shl     ebx, 1
  4127.         mov     cl, 6
  4128.         sub     cl, bl
  4129.         shr     al, cl
  4130.         test    al, 2 ; it's not an ATAPI device
  4131.         pop     ebx ecx
  4132.  
  4133.         jz      .exit
  4134.  
  4135.         cmp     ebx, 4
  4136.         je      .eject
  4137.  
  4138.         cmp     ebx, 5
  4139.         je      .load
  4140. ;--------------------------------------
  4141. .exit:
  4142.         ret
  4143. ;--------------------------------------
  4144. .load:
  4145.         call    .reserve
  4146.         call    LoadMedium
  4147.         jmp     .free
  4148. ;--------------------------------------
  4149. .eject:
  4150.         call    .reserve
  4151.         call    clear_CD_cache
  4152.         call    allow_medium_removal
  4153.         call    EjectMedium
  4154.         jmp     .free
  4155. ;--------------------------------------
  4156. .reserve:
  4157.         call    reserve_cd
  4158.  
  4159.         mov     ebx, ecx
  4160.         inc     ebx
  4161.         mov     [cdpos], ebx
  4162.  
  4163.         mov     eax, ecx
  4164.         shr     eax, 1
  4165.         and     eax, 1
  4166.         inc     eax
  4167.         mov     [ChannelNumber], al
  4168.         mov     eax, ecx
  4169.         and     eax, 1
  4170.         mov     [DiskNumber], al
  4171.         call    reserve_cd_channel
  4172.         ret
  4173. ;--------------------------------------
  4174. .free:
  4175.         call    free_cd_channel
  4176.         and     [cd_status], 0
  4177.         ret
  4178. ;-----------------------------------------------------------------------------
  4179. align 4
  4180. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  4181.         xor     eax, eax
  4182.  
  4183.         cmp     ebx, [_display.width]
  4184.         jae     .store
  4185.         cmp     ecx, [_display.height]
  4186.         jae     .store
  4187. ;--------------------------------------
  4188.         mov     eax, [d_width_calc_area + ecx*4]
  4189.         add     eax, [_display.win_map]
  4190.         movzx   eax, byte[eax+ebx]        ; get value for current point
  4191. ;--------------------------------------
  4192. align 4
  4193. .store:
  4194.         mov     [esp + SYSCALL_STACK.eax], eax
  4195.         ret
  4196. ;-----------------------------------------------------------------------------
  4197. align 4
  4198. syscall_getpixel:                       ; GetPixel
  4199.         mov     ecx, [_display.width]
  4200.         xor     edx, edx
  4201.         mov     eax, ebx
  4202.         div     ecx
  4203.         mov     ebx, edx
  4204.         xchg    eax, ebx
  4205.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4206.         call    dword [GETPIXEL]; eax - x, ebx - y
  4207.         mov     [esp + SYSCALL_STACK.eax], ecx
  4208.         ret
  4209. ;-----------------------------------------------------------------------------
  4210. align 4
  4211. syscall_getarea:
  4212. ;eax = 36
  4213. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4214. ;ecx = [size x]*65536 + [size y]
  4215. ;edx = [start x]*65536 + [start y]
  4216.         pushad
  4217.         mov     edi, ebx
  4218.         mov     eax, edx
  4219.         shr     eax, 16
  4220.         mov     ebx, edx
  4221.         and     ebx, 0xffff
  4222.         dec     eax
  4223.         dec     ebx
  4224.      ; eax - x, ebx - y
  4225.         mov     edx, ecx
  4226.  
  4227.         shr     ecx, 16
  4228.         and     edx, 0xffff
  4229.         mov     esi, ecx
  4230.      ; ecx - size x, edx - size y
  4231.  
  4232.         mov     ebp, edx
  4233.         lea     ebp, [ebp*3]
  4234.         imul    ebp, esi
  4235.         stdcall is_region_userspace, edi, ebp
  4236.         jnz     .exit
  4237.  
  4238.         mov     ebp, edx
  4239.         dec     ebp
  4240.         lea     ebp, [ebp*3]
  4241.  
  4242.         imul    ebp, esi
  4243.  
  4244.         mov     esi, ecx
  4245.         dec     esi
  4246.         lea     esi, [esi*3]
  4247.  
  4248.         add     ebp, esi
  4249.         add     ebp, edi
  4250.  
  4251.         add     ebx, edx
  4252. ;--------------------------------------
  4253. align 4
  4254. .start_y:
  4255.         push    ecx edx
  4256. ;--------------------------------------
  4257. align 4
  4258. .start_x:
  4259.         push    eax ebx ecx
  4260.         add     eax, ecx
  4261.  
  4262.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4263.         call    dword [GETPIXEL]; eax - x, ebx - y
  4264.  
  4265.         mov     [ebp], cx
  4266.         shr     ecx, 16
  4267.         mov     [ebp+2], cl
  4268.  
  4269.         pop     ecx ebx eax
  4270.         sub     ebp, 3
  4271.         dec     ecx
  4272.         jnz     .start_x
  4273.         pop     edx ecx
  4274.         dec     ebx
  4275.         dec     edx
  4276.         jnz     .start_y
  4277.  
  4278. .exit:
  4279.         popad
  4280.         ret
  4281. ;-----------------------------------------------------------------------------
  4282.  
  4283. align 4
  4284. syscall_threads:
  4285. ; eax = 51
  4286. ; if ebx = 1  CreateThreads
  4287. ; ecx = thread entry point
  4288. ; edx = thread stack pointer
  4289. ; return eax = pid
  4290.  
  4291. ; if ebx = 2 GetCurrentThreadId
  4292. ; return eax = current id slot
  4293.  
  4294. ; if ebx = 3 GetThreadPriority
  4295. ; ecx = -1 curr slot  or  id slot
  4296. ; return eax = priority
  4297.  
  4298. ; if ebx = 4 SetThreadPriority
  4299. ; ecx = -1 curr slot or id slot
  4300. ; edx = set priority
  4301. ; return eax = old priority
  4302.  
  4303.         dec     ebx
  4304.         jz      .create
  4305.         mov     eax, [current_slot_idx]
  4306.         dec     ebx
  4307.         jz      .end  ;get_curr_slot
  4308.  
  4309.         test    ecx, ecx  ;-1 curr slot
  4310.         jl      .cur_slot
  4311.         mov     eax, ecx
  4312. .cur_slot:
  4313.         shl     eax, BSF sizeof.APPDATA
  4314.         add     eax, SLOT_BASE
  4315.  
  4316.         test    ecx, ecx  ;-1 curr slot
  4317.         jl      .curr_slot
  4318.  
  4319.         mov     esi, .err_exit
  4320.         mov     edi, CONTROL_EXCEPTION
  4321.  
  4322.         movzx   ecx, [eax+APPDATA.state]
  4323.         test    ecx, ecx
  4324.         jnz     .check_state
  4325.  
  4326.         cmp     [eax+APPDATA.tid], ecx
  4327.         jnz     .curr_slot
  4328.         jmp     .err_exit
  4329.  
  4330. .check_state:
  4331.         sub     ecx, 3 ;TSTATE_ZOMBIE
  4332.         jl      .curr_slot
  4333.         je      .err_exit
  4334.         dec     ecx ; 4 TSTATE_TERMINATING
  4335.         dec     ecx ; 5 TSTATE_WAITING
  4336.         jnz     .err_exit
  4337.  
  4338. .curr_slot:
  4339.         dec     ebx
  4340.         jz      .get_priority
  4341.         dec     ebx
  4342.         jz      .set_priority
  4343.  
  4344. .err_exit:
  4345.         push    -1
  4346.         pop     eax
  4347.         jmp     .end
  4348.  
  4349. .set_priority:  ;;sysfn 51,4
  4350.         mov     dh, dl
  4351.         lock xchg word [eax+APPDATA.def_priority], dx
  4352.         movzx   eax, dl ;old priority
  4353.         jmp     .end
  4354.  
  4355. .get_priority:  ;;sysfn 51,3
  4356.         movzx   eax, [eax+APPDATA.def_priority]
  4357.         jmp     .end
  4358.  
  4359. .create:        ;sysfn 51,1
  4360.         call    new_sys_threads
  4361.  
  4362. .end:
  4363.         mov     [esp + SYSCALL_STACK.eax], eax
  4364.         ret
  4365.  
  4366. ;------------------------------------------------------------------------------
  4367. align 4
  4368. calculate_fast_getting_offset_for_WinMapAddress:
  4369. ; calculate data area for fast getting offset to _WinMapAddress
  4370.         xor     eax, eax
  4371.         mov     ecx, [_display.height]
  4372.         mov     edi, d_width_calc_area
  4373.         cld
  4374. @@:
  4375.         stosd
  4376.         add     eax, [_display.width]
  4377.         dec     ecx
  4378.         jnz     @r
  4379.         ret
  4380. ;------------------------------------------------------------------------------
  4381. align 4
  4382. calculate_fast_getting_offset_for_LFB:
  4383. ; calculate data area for fast getting offset to LFB
  4384.         xor     eax, eax
  4385.         mov     ecx, [_display.height]
  4386.         mov     edi, BPSLine_calc_area
  4387.         cld
  4388. @@:
  4389.         stosd
  4390.         add     eax, [_display.lfb_pitch]
  4391.         dec     ecx
  4392.         jnz     @r
  4393.         ret
  4394. ;------------------------------------------------------------------------------
  4395. align 4
  4396. set_screen:
  4397. ; in:
  4398. ; eax - new Screen_Max_X
  4399. ; ecx - new BytesPerScanLine
  4400. ; edx - new Screen_Max_Y
  4401.  
  4402.         pushfd
  4403.         cli
  4404.  
  4405.         mov     [_display.lfb_pitch], ecx
  4406.  
  4407.         mov     [screen_workarea.right], eax
  4408.         mov     [screen_workarea.bottom], edx
  4409.  
  4410.         push    ebx
  4411.         push    esi
  4412.         push    edi
  4413.  
  4414.         pushad
  4415.  
  4416.         cmp     [do_not_touch_winmap], 1
  4417.         je      @f
  4418.  
  4419.         stdcall kernel_free, [_display.win_map]
  4420.  
  4421.         mov     eax, [_display.width]
  4422.         mul     [_display.height]
  4423.         mov     [_display.win_map_size], eax
  4424.  
  4425.         stdcall kernel_alloc, eax
  4426.         mov     [_display.win_map], eax
  4427.         test    eax, eax
  4428.         jz      .epic_fail
  4429. ; store for f.18.24
  4430.         mov     eax, [_display.width]
  4431.         mov     [display_width_standard], eax
  4432.  
  4433.         mov     eax, [_display.height]
  4434.         mov     [display_height_standard], eax
  4435. @@:
  4436.         call    calculate_fast_getting_offset_for_WinMapAddress
  4437. ; for Qemu or non standart video cards
  4438. ; Unfortunately [BytesPerScanLine] does not always
  4439. ;                             equal to [_display.width] * [ScreenBPP] / 8
  4440.         call    calculate_fast_getting_offset_for_LFB
  4441.         popad
  4442.  
  4443.         call    repos_windows
  4444.         xor     eax, eax
  4445.         xor     ebx, ebx
  4446.         mov     ecx, [_display.width]
  4447.         mov     edx, [_display.height]
  4448.         dec     ecx
  4449.         dec     edx
  4450.         call    calculatescreen
  4451.         pop     edi
  4452.         pop     esi
  4453.         pop     ebx
  4454.  
  4455.         popfd
  4456.         ret
  4457.  
  4458. .epic_fail:
  4459.         hlt                     ; Houston, we've had a problem
  4460.  
  4461. ; --------------- APM ---------------------
  4462. uglobal
  4463. apm_entry       dp      0
  4464. apm_vf          dd      0
  4465. endg
  4466.  
  4467. align 4
  4468. sys_apm:
  4469.         xor     eax, eax
  4470.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4471.         jne     @f
  4472.         inc     eax
  4473.         or      dword [esp + 44], eax   ; error
  4474.         add     eax, 7
  4475.         mov     [esp + SYSCALL_STACK.eax], eax  ; 32-bit protected-mode
  4476.                                                 ; interface not supported
  4477.         ret
  4478.  
  4479. @@:
  4480. ;       xchg    eax, ecx
  4481. ;       xchg    ebx, ecx
  4482.  
  4483.         cmp     dx, 3
  4484.         ja      @f
  4485.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4486.         mov     eax, [apm_vf]
  4487.         mov     [esp + SYSCALL_STACK.eax], eax
  4488.         shr     eax, 16
  4489.         mov     [esp + SYSCALL_STACK.ecx], eax
  4490.         ret
  4491.  
  4492. @@:
  4493.  
  4494.         mov     esi, [master_tab+(OS_BASE shr 20)]
  4495.         xchg    [master_tab], esi
  4496.         push    esi
  4497.         mov     edi, cr3
  4498.         mov     cr3, edi                ;flush TLB
  4499.  
  4500.         call    pword [apm_entry]       ;call APM BIOS
  4501.  
  4502.         xchg    eax, [esp]
  4503.         mov     [master_tab], eax
  4504.         mov     eax, cr3
  4505.         mov     cr3, eax
  4506.         pop     eax
  4507.  
  4508.         mov     [esp + SYSCALL_STACK.edi], edi
  4509.         mov     [esp + SYSCALL_STACK.esi], esi
  4510.         mov     [esp + SYSCALL_STACK.ebx], ebx
  4511.         mov     [esp + SYSCALL_STACK.edx], edx
  4512.         mov     [esp + SYSCALL_STACK.ecx], ecx
  4513.         mov     [esp + SYSCALL_STACK.eax], eax
  4514.         setc    al
  4515.         and     [esp + 44], byte 0xfe
  4516.         or      [esp + 44], al
  4517.         ret
  4518. ; -----------------------------------------
  4519.  
  4520. align 4
  4521. undefined_syscall:                      ; Undefined system call
  4522.         mov     [esp + SYSCALL_STACK.eax], -1
  4523.         ret
  4524.  
  4525. align 4
  4526. ; @brief Check if given memory region lays in lower 2gb (userspace memory) or not
  4527. ; @param base Base address of region
  4528. ; @param len Lenght of region
  4529. ; @return ZF = 1 if region in userspace memory,
  4530. ;         ZF = 0 otherwise
  4531. proc is_region_userspace stdcall, base:dword, len:dword
  4532.         push    eax
  4533.         mov     eax, [base]
  4534.  
  4535.         cmp     eax, OS_BASE-1
  4536.         ja      @f              ; zf
  4537.  
  4538.         add     eax, [len]
  4539.         jc      @f              ; zf
  4540.         cmp     eax, OS_BASE
  4541.         ja      @f              ; zf
  4542.  
  4543.         cmp     eax, eax        ; ZF
  4544. @@:
  4545.         pop     eax
  4546.         ret
  4547. endp
  4548.  
  4549. align 4
  4550. ; @brief Check whether given string lays in userspace memory, i.e. below OS_BASE
  4551. ; @param base Base address of string
  4552. ; @return ZF = 1 if string in userspace memory,
  4553. ;         zf = 0 otherwise
  4554. proc is_string_userspace stdcall, base:dword
  4555.         push    eax ecx edi
  4556.         xor     eax, eax
  4557.         mov     edi, [base]
  4558.  
  4559.         mov     ecx, OS_BASE-1
  4560.         sub     ecx, edi
  4561.         jb      .done           ; zf
  4562.         inc     ecx
  4563.         cmp     ecx, 0x10000    ; don't allow strings larger than 64k?
  4564.         jbe     @f
  4565.         mov     ecx, 0x10000
  4566. @@:
  4567.         repnz scasb
  4568. .done:
  4569.         pop     edi ecx eax
  4570.         ret
  4571. endp
  4572.  
  4573. if ~ lang eq sp
  4574. diff16 "end of .text segment",0,$
  4575. end if
  4576.  
  4577. include "data32.inc"
  4578.  
  4579. __REV__ = __REV
  4580.  
  4581. if ~ lang eq sp
  4582. diff16 "end of kernel code",0,$
  4583. end if
  4584.