Subversion Repositories Kolibri OS

Rev

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

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