Subversion Repositories Kolibri OS

Rev

Rev 9925 | Rev 9930 | 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: 9926 $
  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, PAGE_SIZE
  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.         sti
  1077.  
  1078.         call    mtrr_validate
  1079.  
  1080.         jmp     osloop
  1081.         ; Fly :)
  1082.  
  1083. uglobal
  1084. align 4
  1085. ap_initialized  dd      0
  1086. endg
  1087.  
  1088. ap_init_high:
  1089.         mov     ax, os_stack
  1090.         mov     bx, app_data
  1091.         mov     cx, app_tls
  1092.         mov     ss, ax
  1093.         mov     ds, bx
  1094.         mov     es, bx
  1095.         mov     fs, cx
  1096.         mov     gs, bx
  1097.         xor     esp, esp
  1098.         mov     eax, sys_proc - OS_BASE + PROC.pdt_0
  1099.         mov     cr3, eax
  1100.         lock inc [ap_initialized]
  1101.         jmp     idle_loop
  1102.  
  1103.  
  1104. include 'unpacker.inc'
  1105.  
  1106. align 4
  1107. boot_log:
  1108.         pushad
  1109.  
  1110.         mov     ebx, 10*65536
  1111.         mov     bx, word [boot_y]
  1112.         add     [boot_y], dword 10
  1113.         mov     ecx, 0x80ffffff; ASCIIZ string with white color
  1114.         xor     edi, edi
  1115.         mov     edx, esi
  1116.         inc     edi
  1117.         call    dtext
  1118.  
  1119.         mov     [novesachecksum], 1000
  1120.         call    checkVga_N13
  1121.  
  1122.         popad
  1123.  
  1124.         ret
  1125.  
  1126. ;-----------------------------------------------------------------------------
  1127. ; Register ramdisk file system
  1128. register_ramdisk:
  1129.         mov     esi, boot_initramdisk
  1130.         call    boot_log
  1131.         call    ramdisk_init
  1132.         ret
  1133.  
  1134. ; in: edx -> APPDATA for OS/IDLE slot
  1135. ; in: ebx = stack base
  1136. proc setup_os_slot
  1137.         xor     eax, eax
  1138.         mov     ecx, sizeof.APPDATA/4
  1139.         mov     edi, edx
  1140.         rep stosd
  1141.  
  1142.         mov     eax, tss+0x80
  1143.         call    get_pg_addr
  1144.         inc     eax
  1145.         mov     [edx + APPDATA.io_map], eax
  1146.         mov     eax, tss+0x1080
  1147.         call    get_pg_addr
  1148.         inc     eax
  1149.         mov     [edx + APPDATA.io_map + 4], eax
  1150.  
  1151.         mov     dword [edx + APPDATA.pl0_stack], ebx
  1152.         lea     edi, [ebx + RING0_STACK_SIZE]
  1153.         mov     dword [edx + APPDATA.fpu_state], edi
  1154.         mov     dword [edx + APPDATA.saved_esp0], edi
  1155.         mov     dword [edx + APPDATA.saved_esp], edi
  1156.         mov     dword [edx + APPDATA.terminate_protection], 1 ; make unkillable
  1157.  
  1158.         mov     esi, fpu_data
  1159.         mov     ecx, [xsave_area_size]
  1160.         add     ecx, 3
  1161.         shr     ecx, 2
  1162.         rep movsd
  1163.  
  1164.         lea     eax, [edx + APP_EV_OFFSET]
  1165.         mov     dword [edx + APPDATA.fd_ev], eax
  1166.         mov     dword [edx + APPDATA.bk_ev], eax
  1167.  
  1168.         lea     eax, [edx + APP_OBJ_OFFSET]
  1169.         mov     dword [edx + APPDATA.fd_obj], eax
  1170.         mov     dword [edx + APPDATA.bk_obj], eax
  1171.  
  1172.         mov     dword [edx + APPDATA.cur_dir], sysdir_path-2
  1173.  
  1174.         mov     [edx + APPDATA.process], sys_proc
  1175.  
  1176.         lea     ebx, [edx + APPDATA.list]
  1177.         lea     ecx, [sys_proc + PROC.thr_list]
  1178.         list_add_tail ebx, ecx
  1179.  
  1180.         mov     [edx + APPDATA.wnd_number], dh
  1181.         mov     byte [edx + APPDATA.tid], dh
  1182.  
  1183.         ret
  1184. endp
  1185.  
  1186. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1187. ;                                                                    ;
  1188. ;                    MAIN OS LOOP START                              ;
  1189. ;                                                                    ;
  1190. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1191. align 32
  1192. osloop:
  1193.         mov     edx, osloop_has_work?
  1194.         xor     ecx, ecx
  1195.         call    Wait_events
  1196.         xor     eax, eax
  1197.         xchg    eax, [osloop_nonperiodic_work]
  1198.         test    eax, eax
  1199.         jz      .no_periodic
  1200.  
  1201.         call    __sys_draw_pointer
  1202.         call    window_check_events
  1203.         call    mouse_check_events
  1204.         call    checkmisc
  1205.         call    checkVga_N13
  1206. ;--------------------------------------
  1207. .no_periodic:
  1208.         call    stack_handler
  1209.         call    check_fdd_motor_status
  1210.         call    check_ATAPI_device_event
  1211.         call    check_lights_state
  1212.         call    check_timers
  1213.  
  1214.         jmp     osloop
  1215. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1216. ;                                                                    ;
  1217. ;                      MAIN OS LOOP END                              ;
  1218. ;                                                                    ;
  1219. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1220. proc osloop_has_work?
  1221.         cmp     [osloop_nonperiodic_work], 0
  1222.         jnz     .yes
  1223.         call    stack_handler_has_work?
  1224.         jnz     .yes
  1225.         call    check_fdd_motor_status_has_work?
  1226.         jnz     .yes
  1227.         call    check_ATAPI_device_event_has_work?
  1228.         jnz     .yes
  1229.         call    check_lights_state_has_work?
  1230.         jnz     .yes
  1231.         call    check_timers_has_work?
  1232.         jnz     .yes
  1233. .no:
  1234.         xor     eax, eax
  1235.         ret
  1236. .yes:
  1237.         xor     eax, eax
  1238.         inc     eax
  1239.         ret
  1240. endp
  1241.  
  1242. proc wakeup_osloop
  1243.         mov     [osloop_nonperiodic_work], 1
  1244.         ret
  1245. endp
  1246.  
  1247. uglobal
  1248. align 4
  1249. osloop_nonperiodic_work dd      ?
  1250. endg
  1251.  
  1252. uglobal
  1253. align 64
  1254. idle_addr       rb      64
  1255. endg
  1256.  
  1257. idle_thread:
  1258.         sti
  1259.  
  1260. ; The following code can be executed by all CPUs in the system.
  1261. ; All other parts of the kernel do not expect multi-CPU.
  1262. ; Also, APs don't even have a stack here.
  1263. ; Beware. Don't do anything here. Anything at all.
  1264. idle_loop:
  1265.         cmp     [use_mwait_for_idle], 0
  1266.         jnz     .mwait
  1267.  
  1268. .hlt:
  1269.         hlt
  1270.         jmp     .hlt
  1271.  
  1272. .mwait:
  1273.         mov     eax, idle_addr
  1274.         xor     ecx, ecx
  1275.         xor     edx, edx
  1276.         monitor
  1277.         xor     ecx, ecx
  1278.         mov     eax, 20h        ; or 10h
  1279.         mwait
  1280.         jmp     .mwait
  1281.  
  1282.  
  1283. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1284. ;                                                                      ;
  1285. ;                   INCLUDED SYSTEM FILES                              ;
  1286. ;                                                                      ;
  1287. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1288.  
  1289.  
  1290. include "kernel32.inc"
  1291.  
  1292.  
  1293. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1294. ;                                                                      ;
  1295. ;                       KERNEL FUNCTIONS                               ;
  1296. ;                                                                      ;
  1297. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1298.  
  1299. reserve_irqs_ports:
  1300.  
  1301.  
  1302. ; RESERVE PORTS
  1303.         mov     eax, RESERVED_PORTS
  1304.         mov     ecx, 1
  1305.  
  1306.         mov     [eax], dword 4
  1307.  
  1308.         mov     [eax+16], ecx
  1309.         mov     [eax+16+4], dword 0
  1310.         mov     [eax+16+8], dword 0x2D
  1311.  
  1312.         mov     [eax+32], ecx
  1313.         mov     [eax+32+4], dword 0x30
  1314.         mov     [eax+32+8], dword 0x4D
  1315.  
  1316.         mov     [eax+48], ecx
  1317.         mov     [eax+48+4], dword 0x50
  1318.         mov     [eax+48+8], dword 0xDF
  1319.  
  1320.         mov     [eax+64], ecx
  1321.         mov     [eax+64+4], dword 0xE5
  1322.         mov     [eax+64+8], dword 0xFF
  1323.  
  1324.         ret
  1325.  
  1326.  
  1327. iglobal
  1328.   process_number dd 0x2
  1329. endg
  1330.  
  1331. set_variables:
  1332.  
  1333.         mov     ecx, 0x16                    ; flush port 0x60
  1334. @@:
  1335.         in      al, 0x60
  1336.         loop    @b
  1337.         push    eax
  1338.  
  1339.         mov     ax, [BOOT.y_res]
  1340.         shr     ax, 1
  1341.         shl     eax, 16
  1342.         mov     ax, [BOOT.x_res]
  1343.         shr     ax, 1
  1344.         mov     [MOUSE_X], eax
  1345.         call    wakeup_osloop
  1346.  
  1347.         xor     eax, eax
  1348.         mov     [BTN_ADDR], dword BUTTON_INFO ; address of button list
  1349.  
  1350.         mov     byte [KEY_COUNT], al              ; keyboard buffer
  1351.         mov     byte [BTN_COUNT], al              ; button buffer
  1352. ;        mov   [MOUSE_X],dword 100*65536+100    ; mouse x/y
  1353.  
  1354.         pop     eax
  1355.         ret
  1356. ;-----------------------------------------------------------------------------
  1357.  
  1358. align 4
  1359. display_number:
  1360. ; add check pointers
  1361.         test    bl, bl
  1362.         jz      @f
  1363.         bt      ebx, 30  ; check 30 bit
  1364.         jb      @f
  1365.         stdcall is_region_userspace, ecx, 4
  1366.         jz      @f
  1367.         ret
  1368. @@:
  1369.         jz      @f
  1370.         stdcall is_region_userspace, ecx, 8
  1371.         jz      @f
  1372.         ret
  1373. @@:
  1374.         test    esi, 0x08000000
  1375.         jz      @f
  1376.         stdcall is_region_userspace, edi, 1
  1377.         jz      @f
  1378.         ret
  1379. @@:
  1380. ;It is not optimization
  1381.         mov     eax, ebx
  1382.         mov     ebx, ecx
  1383.         mov     ecx, edx
  1384.         mov     edx, esi
  1385.         mov     esi, edi
  1386. ; eax = print type, al=0 -> ebx is number
  1387. ;                   al=1 -> ebx is pointer
  1388. ;                   ah=0 -> display decimal
  1389. ;                   ah=1 -> display hexadecimal
  1390. ;                   ah=2 -> display binary
  1391. ;                   eax bits 16-21 = number of digits to display (0-32)
  1392. ;                   eax bits 22-31 = reserved
  1393. ;
  1394. ; ebx = number or pointer
  1395. ; ecx = x shl 16 + y
  1396. ; edx = color
  1397.         xor     edi, edi
  1398. display_number_force:
  1399.         push    eax
  1400.         and     eax, 0x3fffffff
  1401.         cmp     eax, 0xffff     ; length > 0 ?
  1402.         pop     eax
  1403.         jge     .cont_displ
  1404.         ret
  1405.    .cont_displ:
  1406.         push    eax
  1407.         and     eax, 0x3fffffff
  1408.         cmp     eax, 61*0x10000  ; length <= 60 ?
  1409.         pop     eax
  1410.         jb      .cont_displ2
  1411.         ret
  1412.    .cont_displ2:
  1413.  
  1414.         pushad
  1415.  
  1416.         cmp     al, 1            ; ecx is a pointer ?
  1417.         jne     @f
  1418.         mov     ebp, ebx
  1419.         add     ebp, 4
  1420.         mov     ebp, [ebp] ;[ebp + std_application_base_address]
  1421.         mov     ebx, [ebx] ;[ebx + std_application_base_address]
  1422. @@:
  1423.         sub     esp, 64
  1424.  
  1425.         test    ah, ah            ; DECIMAL
  1426.         jnz     .no_display_desnum
  1427.  
  1428.         shr     eax, 16
  1429.         and     eax, 0xC03f
  1430. ;     and   eax,0x3f
  1431.         push    eax
  1432.         and     eax, 0x3f
  1433.         mov     edi, esp
  1434.         add     edi, 4+64-1
  1435.         mov     ecx, eax
  1436.         mov     eax, ebx
  1437.         mov     ebx, 10
  1438. @@:
  1439.         xor     edx, edx
  1440.         call    division_64_bits
  1441.         div     ebx
  1442.         add     dl, 48
  1443.         mov     [edi], dl
  1444.         dec     edi
  1445.         loop    @b
  1446.  
  1447.         pop     eax
  1448.         call    normalize_number
  1449.         call    draw_num_text
  1450.         add     esp, 64
  1451.         popad
  1452.         ret
  1453. .no_display_desnum:
  1454.  
  1455.         cmp     ah, 0x01         ; HEXADECIMAL
  1456.         jne     .no_display_hexnum
  1457.  
  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. @@:
  1469.         xor     edx, edx
  1470.         call    division_64_bits
  1471.         div     ebx
  1472.    ;hexletters = __fdo_hexdigits
  1473.         add     edx, __fdo_hexdigits ;hexletters
  1474.         mov     dl, [edx]
  1475.         mov     [edi], dl
  1476.         dec     edi
  1477.         loop    @b
  1478.  
  1479.         pop     eax
  1480.         call    normalize_number
  1481.         call    draw_num_text
  1482.         add     esp, 64
  1483.         popad
  1484.         ret
  1485. .no_display_hexnum:
  1486.  
  1487.         cmp     ah, 0x02         ; BINARY
  1488.         jne     .no_display_binnum
  1489.  
  1490.         shr     eax, 16
  1491.         and     eax, 0xC03f
  1492. ;     and   eax,0x3f
  1493.         push    eax
  1494.         and     eax, 0x3f
  1495.         mov     edi, esp
  1496.         add     edi, 4+64-1
  1497.         mov     ecx, eax
  1498.         mov     eax, ebx
  1499.         mov     ebx, 2
  1500. @@:
  1501.         xor     edx, edx
  1502.         call    division_64_bits
  1503.         div     ebx
  1504.         add     dl, 48
  1505.         mov     [edi], dl
  1506.         dec     edi
  1507.         loop    @b
  1508.  
  1509.         pop     eax
  1510.         call    normalize_number
  1511.         call    draw_num_text
  1512.         add     esp, 64
  1513.         popad
  1514.         ret
  1515. .no_display_binnum:
  1516.  
  1517.         add     esp, 64
  1518.         popad
  1519.         ret
  1520.  
  1521. normalize_number:
  1522.         test    ah, 0x80
  1523.         jz      .continue
  1524.         mov     ecx, 48
  1525.         and     eax, 0x3f
  1526. @@:
  1527.         inc     edi
  1528.         cmp     [edi], cl
  1529.         jne     .continue
  1530.         dec     eax
  1531.         cmp     eax, 1
  1532.         ja      @r
  1533.         mov     al, 1
  1534. .continue:
  1535.         and     eax, 0x3f
  1536.         ret
  1537.  
  1538. division_64_bits:
  1539.         test    [esp+1+4], byte 0x40
  1540.         jz      .continue
  1541.         push    eax
  1542.         mov     eax, ebp
  1543.         div     ebx
  1544.         mov     ebp, eax
  1545.         pop     eax
  1546. .continue:
  1547.         ret
  1548.  
  1549. draw_num_text:
  1550.         mov     esi, eax
  1551.         mov     edx, 64+4
  1552.         sub     edx, eax
  1553.         add     edx, esp
  1554.         mov     ebx, [esp+64+32-8+4]
  1555. ; add window start x & y
  1556.  
  1557.         mov     ecx, [current_slot_idx]
  1558.         shl     ecx, BSF sizeof.WDATA
  1559.  
  1560.         mov     eax, [window_data + ecx + WDATA.box.left]
  1561.         add     eax, [window_data + ecx + WDATA.clientbox.left]
  1562.         shl     eax, 16
  1563.         add     eax, [window_data + ecx + WDATA.box.top]
  1564.         add     eax, [window_data + ecx + WDATA.clientbox.top]
  1565.         add     ebx, eax
  1566.         mov     ecx, [esp+64+32-12+4]
  1567.         mov     eax, [esp+64+8]         ; background color (if given)
  1568.         mov     edi, [esp+64+4]
  1569.         and     ecx, 5FFFFFFFh
  1570.         bt      ecx, 27
  1571.         jnc     @f
  1572.         mov     edi, eax
  1573. @@:
  1574.         jmp     dtext
  1575. ;-----------------------------------------------------------------------------
  1576. align 4
  1577. sys_setup:
  1578. ;  1 = roland mpu midi base , base io address
  1579. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1580. ;  3 = not used
  1581. ;  4 = not used
  1582. ;  5 = system language, 1eng 2fi 3ger 4rus
  1583. ;  6 = not used
  1584. ;  7 = not used
  1585. ;  8 = not used
  1586. ;  9 = not used
  1587. ; 10 = not used
  1588. ; 11 = enable lba read
  1589. ; 12 = enable pci access
  1590. ;-----------------------------------------------------------------------------
  1591.         and     [esp + SYSCALL_STACK.eax], dword 0
  1592. ; F.21.1 - set MPU MIDI base port
  1593.         dec     ebx
  1594.         jnz     @f
  1595.  
  1596.         cmp     ecx, 0x100
  1597.         jb      @f
  1598.  
  1599.         mov     esi, 65535
  1600.         cmp     esi, ecx
  1601.         jb      @f
  1602.  
  1603.         mov     [midi_base], cx
  1604.         mov     word [mididp], cx
  1605.         inc     cx
  1606.         mov     word [midisp], cx
  1607.         ret
  1608. ;--------------------------------------
  1609. @@:
  1610. ; F.21.2 - set keyboard layout
  1611.         dec     ebx
  1612.         jnz     @f
  1613.  
  1614.         mov     eax, edx
  1615. ; 1 = normal layout
  1616.         dec     ecx
  1617.         jnz     .shift
  1618.  
  1619.         mov     ebx, keymap
  1620.         mov     ecx, 128
  1621.         call    memmove
  1622.         ret
  1623. ;--------------------------------------
  1624. .shift:
  1625. ; 2 = layout at pressed Shift
  1626.         dec     ecx
  1627.         jnz     .alt
  1628.  
  1629.         mov     ebx, keymap_shift
  1630.         mov     ecx, 128
  1631.         call    memmove
  1632.         ret
  1633. ;--------------------------------------
  1634. .alt:
  1635. ; 3 = layout at pressed Alt
  1636.         dec     ecx
  1637.         jnz     .country
  1638.  
  1639.         mov     ebx, keymap_alt
  1640.         mov     ecx, 128
  1641.         call    memmove
  1642.         ret
  1643. ;--------------------------------------
  1644. .country:
  1645. ; country identifier
  1646.         sub     ecx, 6
  1647.         jnz     .error
  1648.  
  1649.         mov     word [keyboard], dx
  1650.         ret
  1651. ;--------------------------------------
  1652. @@:
  1653. ; F.21.5 - set system language
  1654.         sub     ebx, 3
  1655.         jnz     @f
  1656.  
  1657.         mov     [syslang], ecx
  1658.         ret
  1659. ;--------------------------------------
  1660. @@:
  1661. ; F.21.11 - enable/disable low-level access to HD
  1662.         and     ecx, 1
  1663.         sub     ebx, 6
  1664.         jnz     @f
  1665.  
  1666.         mov     [lba_read_enabled], ecx
  1667.         ret
  1668. ;--------------------------------------
  1669. @@:
  1670. ; F.21.12 - enable/disable low-level access to PCI
  1671.         dec     ebx
  1672.         jnz     .error
  1673.  
  1674.         mov     [pci_access_enabled], ecx
  1675.         ret
  1676. ;--------------------------------------
  1677. .error:
  1678.         or      [esp + SYSCALL_STACK.eax], dword -1
  1679.         ret
  1680. ;-----------------------------------------------------------------------------
  1681. align 4
  1682. sys_getsetup:
  1683. ;  1 = roland mpu midi base , base io address
  1684. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1685. ;  3 = not used
  1686. ;  4 = not used
  1687. ;  5 = system language, 1eng 2fi 3ger 4rus
  1688. ;  6 = not used
  1689. ;  7 = not used
  1690. ;  8 = not used
  1691. ;  9 = get hs timer tic
  1692. ; 10 = not used
  1693. ; 11 = get the state "lba read"
  1694. ; 12 = get the state "pci access"
  1695. ;-----------------------------------------------------------------------------
  1696. ; F.26.1 - get MPU MIDI base port
  1697.         dec     ebx
  1698.         jnz     @f
  1699.  
  1700.         movzx   eax, [midi_base]
  1701.         mov     [esp + SYSCALL_STACK.eax], eax
  1702.         ret
  1703. ;--------------------------------------
  1704. @@:
  1705. ; F.26.2 - get keyboard layout
  1706.         dec     ebx
  1707.         jnz     @f
  1708.  
  1709.         mov     ebx, edx
  1710.         ; if given memory address belongs to kernel then error
  1711.         stdcall is_region_userspace, ebx, 128
  1712.         jnz     .addr_error
  1713. ; 1 = normal layout
  1714.         dec     ecx
  1715.         jnz     .shift
  1716.  
  1717.         mov     eax, keymap
  1718.         mov     ecx, 128
  1719.         call    memmove
  1720.         ret
  1721. ;--------------------------------------
  1722. .shift:
  1723. ; 2 = layout with pressed Shift
  1724.         dec     ecx
  1725.         jnz     .alt
  1726.  
  1727.         mov     eax, keymap_shift
  1728.         mov     ecx, 128
  1729.         call    memmove
  1730.         ret
  1731. ;--------------------------------------
  1732. .alt:
  1733. ; 3 = layout with pressed Alt
  1734.         dec     ecx
  1735.         jne     .country
  1736.  
  1737.         mov     eax, keymap_alt
  1738.         mov     ecx, 128
  1739.         call    memmove
  1740.         ret
  1741. ;--------------------------------------
  1742. .country:
  1743. ; 9 = country identifier
  1744.         sub     ecx, 6
  1745.         jnz     .error
  1746.  
  1747.         movzx   eax, word [keyboard]
  1748.         mov     [esp + SYSCALL_STACK.eax], eax
  1749.         ret
  1750.  
  1751. .addr_error:    ; if given memory address is illegal
  1752.         or      dword [esp + SYSCALL_STACK.eax], -1
  1753.         ret        
  1754. ;--------------------------------------
  1755. @@:
  1756. ; F.26.5 - get system language
  1757.         sub     ebx, 3
  1758.         jnz     @f
  1759.  
  1760.         mov     eax, [syslang]
  1761.         mov     [esp + SYSCALL_STACK.eax], eax
  1762.         ret
  1763. ;--------------------------------------
  1764. @@:
  1765. ; F.26.9 - get the value of the time counter
  1766.         sub     ebx, 4
  1767.         jnz     @f
  1768.  
  1769.         mov     eax, [timer_ticks]
  1770.         mov     [esp + SYSCALL_STACK.eax], eax
  1771.         ret
  1772. ;--------------------------------------
  1773. @@:
  1774. ; F.26.10 - get the time from kernel launch in nanoseconds
  1775.         dec     ebx
  1776.         jnz     @f
  1777.  
  1778.         call    get_clock_ns
  1779.         mov     [esp + SYSCALL_STACK.edx], edx
  1780.         mov     [esp + SYSCALL_STACK.eax], eax
  1781.         ret
  1782. ;--------------------------------------
  1783. @@:
  1784. ; F.26.11 - Find out whether low-level HD access is enabled
  1785.         dec     ebx
  1786.         jnz     @f
  1787.  
  1788.         mov     eax, [lba_read_enabled]
  1789.         mov     [esp + SYSCALL_STACK.eax], eax
  1790.         ret
  1791. ;--------------------------------------
  1792. @@:
  1793. ; F.26.12 - Find out whether low-level PCI access is enabled
  1794.         dec     ebx
  1795.         jnz     .error
  1796.  
  1797.         mov     eax, [pci_access_enabled]
  1798.         mov     [esp + SYSCALL_STACK.eax], eax
  1799.         ret
  1800. ;--------------------------------------
  1801. .error:
  1802.         or      [esp + SYSCALL_STACK.eax], dword -1
  1803.         ret
  1804. ;-----------------------------------------------------------------------------
  1805. get_timer_ticks:
  1806.         mov     eax, [timer_ticks]
  1807.         ret
  1808. ;-----------------------------------------------------------------------------
  1809. iglobal
  1810. midi_base dw 0
  1811. endg
  1812.  
  1813. align 4
  1814. sys_midi:
  1815.         cmp     word [mididp], 0
  1816.         jnz     @f
  1817.         mov     [esp + SYSCALL_STACK.eax], 1
  1818.         ret
  1819. @@:
  1820.         and     [esp + SYSCALL_STACK.eax], 0
  1821.         dec     ebx
  1822.         jnz     .smn1
  1823.  ;    call setuart
  1824. @@:
  1825.         call    .is_output
  1826.         test    al, al
  1827.         jnz     @b
  1828.         mov     dx, word [midisp]
  1829.         mov     al, 0xff
  1830.         out     dx, al
  1831. @@:
  1832.         mov     dx, word [midisp]
  1833.         mov     al, 0xff
  1834.         out     dx, al
  1835.         call    .is_input
  1836.         test    al, al
  1837.         jnz     @b
  1838.         call    .get_mpu_in
  1839.         cmp     al, 0xfe
  1840.         jnz     @b
  1841. @@:
  1842.         call    .is_output
  1843.         test    al, al
  1844.         jnz     @b
  1845.         mov     dx, word [midisp]
  1846.         mov     al, 0x3f
  1847.         out     dx, al
  1848.         ret
  1849. .smn1:
  1850.         dec     ebx
  1851.         jnz     .ret
  1852. @@:
  1853.         call    .get_mpu_in
  1854.         call    .is_output
  1855.         test    al, al
  1856.         jnz     @b
  1857.         mov     al, bl
  1858.         call    .put_mpu_out
  1859. .ret:
  1860.         ret
  1861.  
  1862. .is_input:
  1863.         push    edx
  1864.         mov     dx, word [midisp]
  1865.         in      al, dx
  1866.         and     al, 0x80
  1867.         pop     edx
  1868.         ret
  1869.  
  1870. .is_output:
  1871.         push    edx
  1872.         mov     dx, word [midisp]
  1873.         in      al, dx
  1874.         and     al, 0x40
  1875.         pop     edx
  1876.         ret
  1877.  
  1878. .get_mpu_in:
  1879.         push    edx
  1880.         mov     dx, word [mididp]
  1881.         in      al, dx
  1882.         pop     edx
  1883.         ret
  1884.  
  1885. .put_mpu_out:
  1886.         push    edx
  1887.         mov     dx, word [mididp]
  1888.         out     dx, al
  1889.         pop     edx
  1890.         ret
  1891.  
  1892. ;-----------------------------------------------------------------------------
  1893. sys_end:
  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.  
  2032.         mov     edx, [ecx + APPDATA.tid]
  2033.         cmp     byte [ecx + APPDATA.state], TSTATE_FREE
  2034.         jz      .noprocessterminate
  2035.         push    eax
  2036.         mov     eax, ecx
  2037.         call    is_kernel_thread
  2038.         pop     eax
  2039.         jz      .noprocessterminate
  2040.         push    ecx edx
  2041.         mov     edx, ecx
  2042.         call    request_terminate
  2043.         pop     edx ecx
  2044.         test    eax, eax
  2045.         jz      .noprocessterminate
  2046. ;--------------------------------------
  2047. ; terminate all network sockets it used
  2048.         pusha
  2049.         mov     eax, edx     ;TODO: check function
  2050.         call    socket_process_end
  2051.         popa
  2052. ;--------------------------------------
  2053.         cmp     [_display.select_cursor], 0
  2054.         je      .restore_end
  2055. ; restore default cursor before killing
  2056.         pusha
  2057.         mov     ecx, [esp+32]
  2058.         shl     ecx, BSF sizeof.APPDATA
  2059.         add     ecx, SLOT_BASE
  2060.         mov     eax, [def_cursor]
  2061.         cmp     [ecx + APPDATA.cursor], eax
  2062.         je      @f
  2063.         call    restore_default_cursor_before_killing
  2064. @@:
  2065.         popa
  2066. .restore_end:
  2067. ;--------------------------------------
  2068.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2069.         mov     [ecx + APPDATA.state], TSTATE_ZOMBIE; clear possible i40's
  2070.         call    wakeup_osloop
  2071.      ;call MEM_Heap_UnLock
  2072.  
  2073.         cmp     edx, [application_table_owner]; clear app table stat
  2074.         jne     .noatsc
  2075.         call    unlock_application_table
  2076. .noatsc:
  2077. .noprocessterminate:
  2078.         add     esp, 4
  2079.         ret
  2080. ;------------------------------------------------------------------------------
  2081. sysfn_terminate2:
  2082.         call    lock_application_table
  2083.         mov     eax, ecx
  2084.         call    pid_to_slot
  2085.         test    eax, eax
  2086.         jz      .not_found
  2087.         mov     ecx, eax
  2088.         cli
  2089.         call    sysfn_terminate
  2090.         call    unlock_application_table
  2091.         sti
  2092.         and     dword [esp + SYSCALL_STACK.eax], 0
  2093.         ret
  2094. .not_found:
  2095.         call    unlock_application_table
  2096.         or      dword [esp + SYSCALL_STACK.eax], -1
  2097.         ret
  2098. ;------------------------------------------------------------------------------
  2099. sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
  2100.         cmp     ecx, 2
  2101.         jb      .nowindowdeactivate
  2102.         cmp     ecx, [thread_count]
  2103.         ja      .nowindowdeactivate
  2104.  
  2105.         movzx   esi, word [WIN_STACK + ecx*2]
  2106.         cmp     esi, 1
  2107.         je      .nowindowdeactivate ; already deactive
  2108.  
  2109.         mov     edi, ecx
  2110.         shl     edi, BSF sizeof.WDATA
  2111.         add     edi, window_data
  2112.         movzx   esi, word [WIN_STACK + ecx * 2]
  2113.         lea     esi, [WIN_POS + esi * 2]
  2114.         call    window._.window_deactivate
  2115.         call    syscall_display_settings.calculateScreen
  2116.         call    syscall_display_settings.redrawScreen
  2117. .nowindowdeactivate:
  2118.         ret
  2119. ;------------------------------------------------------------------------------
  2120. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2121.         cmp     ecx, 2
  2122.         jb      .nowindowactivate
  2123.         cmp     ecx, [thread_count]
  2124.         ja      .nowindowactivate
  2125. ;-------------------------------------
  2126. @@:
  2127. ; If the window is captured and moved by the user,
  2128. ; then you can't change the position in window stack!!!
  2129.         mov     al, [mouse.active_sys_window.action]
  2130.         and     al, WINDOW_MOVE_AND_RESIZE_FLAGS
  2131.         test    al, al
  2132.         jz      @f
  2133.         call    change_task
  2134.         jmp     @b
  2135. @@:
  2136. ;-------------------------------------
  2137.         mov     [window_minimize], 2; restore window if minimized
  2138.         call    wakeup_osloop
  2139.  
  2140.         movzx   esi, word [WIN_STACK + ecx*2]
  2141.         cmp     esi, [thread_count]
  2142.         je      .nowindowactivate; already active
  2143.  
  2144.         mov     edi, ecx
  2145.         shl     edi, BSF sizeof.WDATA
  2146.         add     edi, window_data
  2147.         movzx   esi, word [WIN_STACK + ecx * 2]
  2148.         lea     esi, [WIN_POS + esi * 2]
  2149.         call    waredraw
  2150. .nowindowactivate:
  2151.         ret
  2152. ;------------------------------------------------------------------------------
  2153. align 4
  2154. sysfn_zmodif:
  2155. ;18,25,1 - get z_modif
  2156. ;18,25,2 - set z_modif
  2157. ;edx = -1(for current task) or TID
  2158. ;esi(for 2) = new value z_modif
  2159. ;return:
  2160. ;1:   eax = z_modif
  2161. ;2: eax=0(fail),1(success) for set z_modif
  2162.  
  2163.         cmp     edx, -1
  2164.         jne     @f
  2165.         mov     edx, [current_slot_idx]
  2166.      @@:
  2167.         cmp     edx, [thread_count]
  2168.         ja      .fail
  2169.         cmp     edx, 1
  2170.         je      .fail
  2171.  
  2172.         mov     eax, edx
  2173.         shl     edx, BSF sizeof.WDATA
  2174.  
  2175.         cmp     [edx*(sizeof.APPDATA/sizeof.WDATA) + SLOT_BASE + APPDATA.state], TSTATE_FREE
  2176.         je      .fail
  2177.  
  2178.         cmp     ecx, 1
  2179.         jnz     .set_zmod
  2180.  
  2181.         mov     al, [window_data + edx + WDATA.z_modif]
  2182.         jmp     .exit
  2183.  
  2184. .set_zmod:
  2185.         cmp     ecx, 2
  2186.         jnz     .fail
  2187.  
  2188.         mov     ebx, esi
  2189.         mov     esi, eax
  2190.  
  2191.         cmp     bl, ZPOS_ALWAYS_TOP
  2192.         jg      .fail
  2193.  
  2194.         mov     [window_data + edx + WDATA.z_modif], bl
  2195.  
  2196.         mov     eax, [window_data + edx + WDATA.box.left]
  2197.         mov     ebx, [window_data + edx + WDATA.box.top]
  2198.         mov     ecx, [window_data + edx + WDATA.box.width]
  2199.         mov     edx, [window_data + edx + WDATA.box.height]
  2200.         add     ecx, eax
  2201.         add     edx, ebx
  2202.         call    window._.set_screen
  2203.         call    window._.set_top_wnd
  2204.         call    window._.redraw_top_wnd
  2205.  
  2206.         shl     esi, BSF sizeof.WDATA
  2207.         mov     [esi + window_data + WDATA.fl_redraw], 1
  2208.  
  2209.  
  2210.         mov     eax, 1
  2211.         jmp     .exit
  2212. .fail:
  2213.         xor     eax, eax
  2214. .exit:
  2215.         mov     [esp + SYSCALL_STACK.eax], eax
  2216.         ret
  2217.  
  2218. ;------------------------------------------------------------------------------
  2219. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2220.         ;mov     eax, [TASK_TABLE+32+TASKDATA.cpu_usage]
  2221.         mov     eax, [SLOT_BASE + APPDATA.cpu_usage]
  2222.         mov     [esp + SYSCALL_STACK.eax], eax
  2223.         ret
  2224. ;------------------------------------------------------------------------------
  2225. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2226.         mov     eax, dword [cpu_freq]
  2227.         mov     [esp + SYSCALL_STACK.eax], eax
  2228.         ret
  2229. ;------------------------------------------------------------------------------
  2230. get_cpu_freq:
  2231.         mov     eax, dword [cpu_freq]
  2232.         mov     edx, dword [cpu_freq+4]
  2233.         ret
  2234. ;  SAVE ramdisk to /hd/1/kolibri.img
  2235. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2236.    include 'blkdev/rdsave.inc'
  2237. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2238. ;------------------------------------------------------------------------------
  2239. align 4
  2240. sysfn_getactive:        ; 18.7 = get active window
  2241.         mov     eax, [thread_count]
  2242.         movzx   eax, word [WIN_POS + eax*2]
  2243.         mov     [esp + SYSCALL_STACK.eax], eax
  2244.         ret
  2245. ;------------------------------------------------------------------------------
  2246. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2247. ;     cmp  ecx,1
  2248.         dec     ecx
  2249.         jnz     .set_flag
  2250.         movzx   eax, byte [sound_flag]; get sound_flag
  2251.         mov     [esp + SYSCALL_STACK.eax], eax
  2252.         ret
  2253. .set_flag:
  2254. ;     cmp  ecx,2
  2255.         dec     ecx
  2256.         jnz     .err
  2257.         xor     byte [sound_flag], 1
  2258. .err:
  2259.         ret
  2260. ;------------------------------------------------------------------------------
  2261. sysfn_minimize:         ; 18.10 = minimize window
  2262.         mov     [window_minimize], 1
  2263.         call    wakeup_osloop
  2264.         ret
  2265. ;------------------------------------------------------------------------------
  2266. align 4
  2267. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2268.         dec     ecx
  2269.         jnz     .exit
  2270. .small_table:
  2271.         stdcall is_region_userspace, edx, DRIVE_DATA_SIZE
  2272.         jnz     .exit
  2273.         mov     edi, edx
  2274.         mov     esi, DRIVE_DATA
  2275.         mov     ecx, DRIVE_DATA_SIZE ;10
  2276.         cld
  2277.         rep movsb
  2278. .exit:
  2279.         ret
  2280. ;------------------------------------------------------------------------------
  2281. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2282.         ; if given memory address belongs to kernel then error
  2283.         stdcall is_region_userspace, ecx, version_inf.size
  2284.         jnz     .addr_error
  2285.  
  2286.         mov     edi, ecx
  2287.         mov     esi, version_inf
  2288.         mov     ecx, version_inf.size
  2289.         rep movsb
  2290.         ret
  2291. .addr_error:    ; if given memory address is illegal
  2292.         mov     dword [esp + SYSCALL_STACK.eax], -1
  2293.         ret  
  2294. ;------------------------------------------------------------------------------
  2295. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2296.         ;wait retrace functions
  2297.         mov     edx, 0x3da
  2298. .loop:
  2299.         in      al, dx
  2300.         test    al, 1000b
  2301.         jz      .loop
  2302.         and     [esp + SYSCALL_STACK.eax], dword 0
  2303.         ret
  2304. ;------------------------------------------------------------------------------
  2305. align 4
  2306. sysfn_centermouse:      ; 18.15 = mouse centered
  2307.         mov     eax, [_display.width]
  2308.         shr     eax, 1
  2309.         mov     [MOUSE_X], ax
  2310.         mov     eax, [_display.height]
  2311.         shr     eax, 1
  2312.         mov     [MOUSE_Y], ax
  2313.         call    wakeup_osloop
  2314.         xor     eax, eax
  2315.         and     [esp + SYSCALL_STACK.eax], eax
  2316.         ret
  2317. ;------------------------------------------------------------------------------
  2318. sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
  2319.         cmp     ecx, 8
  2320.         jnc     @f
  2321.         jmp     dword [.table + ecx*4]
  2322. .get_mouse_acceleration:
  2323.         xor     eax, eax
  2324.         mov     ax, [mouse_speed_factor]
  2325.         mov     [esp + SYSCALL_STACK.eax], eax
  2326.         ret
  2327. .set_mouse_acceleration:
  2328.         mov     [mouse_speed_factor], dx
  2329.         ret
  2330. .get_mouse_delay:
  2331.         xor     eax, eax
  2332.         mov     al, [mouse_delay]
  2333.         mov     [esp + SYSCALL_STACK.eax], eax
  2334.         ret
  2335. .set_mouse_delay:
  2336.         mov     [mouse_delay], dl
  2337. @@:
  2338.         ret
  2339. .set_pointer_position:
  2340.         cmp     dx, word[_display.height]
  2341.         jae     @b
  2342.         rol     edx, 16
  2343.         cmp     dx, word[_display.width]
  2344.         jae     @b
  2345.         mov     [MOUSE_X], edx
  2346.         mov     [mouse_active], 1
  2347.         jmp     wakeup_osloop
  2348. .set_mouse_button:
  2349.         mov     [BTN_DOWN], edx
  2350.         mov     [mouse_active], 1
  2351.         jmp     wakeup_osloop
  2352. .get_doubleclick_delay:
  2353.         xor     eax, eax
  2354.         mov     al, [mouse_doubleclick_delay]
  2355.         mov     [esp + SYSCALL_STACK.eax], eax
  2356.         ret
  2357. .set_doubleclick_delay:
  2358.         mov     [mouse_doubleclick_delay], dl
  2359.         ret
  2360. align 4
  2361. .table:
  2362. dd      .get_mouse_acceleration
  2363. dd      .set_mouse_acceleration
  2364. dd      .get_mouse_delay
  2365. dd      .set_mouse_delay
  2366. dd      .set_pointer_position
  2367. dd      .set_mouse_button
  2368. dd      .get_doubleclick_delay
  2369. dd      .set_doubleclick_delay
  2370. ;------------------------------------------------------------------------------
  2371. sysfn_getfreemem:
  2372.         mov     eax, [pg_data.pages_free]
  2373.         shl     eax, 2
  2374.         mov     [esp + SYSCALL_STACK.eax], eax
  2375.         ret
  2376. ;------------------------------------------------------------------------------
  2377. sysfn_getallmem:
  2378.         mov     eax, [MEM_AMOUNT]
  2379.         shr     eax, 10
  2380.         mov     [esp + SYSCALL_STACK.eax], eax
  2381.         ret
  2382. ;------------------------------------------------------------------------------
  2383. sysfn_pid_to_slot:
  2384.         mov     eax, ecx
  2385.         call    pid_to_slot
  2386.         mov     [esp + SYSCALL_STACK.eax], eax
  2387.         ret
  2388. ;------------------------------------------------------------------------------
  2389. sysfn_min_rest_window:
  2390.         pushad
  2391.         mov     eax, edx ; ebx - operating
  2392.         shr     ecx, 1
  2393.         jnc     @f
  2394.         call    pid_to_slot
  2395. @@:
  2396.         or      eax, eax ; eax - number of slot
  2397.         jz      .error
  2398.         cmp     eax, max_processes    ; varify maximal slot number
  2399.         ja      .error
  2400.         movzx   eax, word [WIN_STACK + eax*2]
  2401.         shr     ecx, 1
  2402.         jc      .restore
  2403.  ; .minimize:
  2404.         call    minimize_window
  2405.         jmp     .exit
  2406. .restore:
  2407.         call    restore_minimized_window
  2408. .exit:
  2409.         popad
  2410.         xor     eax, eax
  2411.         mov     [esp + SYSCALL_STACK.eax], eax
  2412.         ret
  2413. .error:
  2414.         popad
  2415.         xor     eax, eax
  2416.         dec     eax
  2417.         mov     [esp + SYSCALL_STACK.eax], eax
  2418.         ret
  2419. ;------------------------------------------------------------------------------
  2420. sysfn_min_windows:
  2421.         call    minimize_all_window
  2422.         mov     [esp + SYSCALL_STACK.eax], eax
  2423.         call    change_task
  2424.         ret
  2425. ;------------------------------------------------------------------------------
  2426. sysfn_set_screen_sizes:
  2427.         cmp     [SCR_MODE], word 0x13
  2428.         jbe     .exit
  2429.  
  2430.         cmp     [_display.select_cursor], select_cursor
  2431.         jne     .exit
  2432.  
  2433.         cmp     ecx, [display_width_standard]
  2434.         ja      .exit
  2435.  
  2436.         cmp     edx, [display_height_standard]
  2437.         ja      .exit
  2438.  
  2439.         pushfd
  2440.         cli
  2441.         mov     eax, ecx
  2442.         mov     ecx, [_display.lfb_pitch]
  2443.         mov     [_display.width], eax
  2444.         dec     eax
  2445.         mov     [_display.height], edx
  2446.         dec     edx
  2447. ; eax - new Screen_Max_X
  2448. ; edx - new Screen_Max_Y
  2449.         mov     [do_not_touch_winmap], 1
  2450.         call    set_screen
  2451.         mov     [do_not_touch_winmap], 0
  2452.         popfd
  2453.         call    change_task
  2454. .exit:
  2455.         ret
  2456. ;------------------------------------------------------------------------------
  2457. uglobal
  2458. screen_workarea RECT
  2459. display_width_standard dd 0
  2460. display_height_standard dd 0
  2461. do_not_touch_winmap db 0
  2462. window_minimize db 0
  2463. sound_flag      db 0
  2464.  
  2465. endg
  2466.  
  2467. iglobal
  2468. version_inf:
  2469.         db 0,7,7,0  ; version 0.7.7.0
  2470.         db 0
  2471. .rev    dd __REV__
  2472. .size = $ - version_inf
  2473. endg
  2474. ;------------------------------------------------------------------------------
  2475. align 4
  2476. sys_cachetodiskette:
  2477.         cmp     ebx, 1
  2478.         jb      .no_floppy_save
  2479.         cmp     ebx, 2
  2480.         ja      .no_floppy_save
  2481.         call    save_image
  2482.         mov     [esp + SYSCALL_STACK.eax], eax
  2483.         ret
  2484. .no_floppy_save:
  2485.         mov     [esp + SYSCALL_STACK.eax], dword 1
  2486.         ret
  2487. ;------------------------------------------------------------------------------
  2488. align 4
  2489. sys_cpuusage:
  2490.  
  2491. ;  RETURN:
  2492. ;
  2493. ;  +00 dword     process cpu usage
  2494. ;  +04  word     position in windowing stack
  2495. ;  +06  word     windowing stack value at current position (cpu nro)
  2496. ;  +10 12 bytes  name
  2497. ;  +22 dword     start in mem
  2498. ;  +26 dword     used mem
  2499. ;  +30 dword     PID , process idenfification number
  2500. ;
  2501.         ; if given memory address belongs to kernel then error
  2502.         stdcall is_region_userspace, ebx, 0x4C
  2503.         jnz     .addr_error
  2504.  
  2505.         cmp     ecx, -1 ; who am I ?
  2506.         jne     .no_who_am_i
  2507.         mov     ecx, [current_slot_idx]
  2508.   .no_who_am_i:
  2509.         cmp     ecx, max_processes
  2510.         ja      .nofillbuf
  2511.  
  2512. ; +4: word: position of the window of thread in the window stack
  2513.         mov     ax, [WIN_STACK + ecx * 2]
  2514.         mov     [ebx+4], ax
  2515. ; +6: word: number of the thread slot, which window has in the window stack
  2516. ;           position ecx (has no relation to the specific thread)
  2517.         mov     ax, [WIN_POS + ecx * 2]
  2518.         mov     [ebx+6], ax
  2519.  
  2520.         shl     ecx, BSF sizeof.APPDATA
  2521.  
  2522. ; +0: dword: memory usage
  2523.         ;mov     eax, [ecx+TASK_TABLE+TASKDATA.cpu_usage]
  2524.         mov     eax, [SLOT_BASE + ecx - sizeof.APPDATA + APPDATA.cpu_usage]
  2525.         mov     [ebx], eax
  2526. ; +10: 11 bytes: name of the process
  2527.         push    ecx
  2528.         lea     eax, [SLOT_BASE + ecx + APPDATA.app_name]
  2529.         add     ebx, 10
  2530.         mov     ecx, 11
  2531.         call    memmove
  2532.         pop     ecx
  2533.  
  2534.         shr     ecx, (BSF sizeof.APPDATA - BSF sizeof.WDATA)
  2535. ; +22: address of the process in memory
  2536. ; +26: size of used memory - 1
  2537.         push    edi
  2538.         lea     edi, [ebx+12]
  2539.         xor     eax, eax
  2540.         mov     edx, 0x100000*16
  2541.         cmp     ecx, 1 shl BSF sizeof.WDATA
  2542.         je      .os_mem
  2543.         mov     edx, [SLOT_BASE + ecx*(sizeof.APPDATA/sizeof.WDATA) + APPDATA.process]
  2544.         mov     edx, [edx + PROC.mem_used]
  2545.         mov     eax, std_application_base_address
  2546. .os_mem:
  2547.         stosd
  2548.         lea     eax, [edx-1]
  2549.         stosd
  2550.  
  2551. ; +30: PID/TID
  2552.         mov     eax, [SLOT_BASE + ecx*(sizeof.APPDATA/sizeof.WDATA) + APPDATA.tid]
  2553.         stosd
  2554.  
  2555.     ; window position and size
  2556.         push    esi
  2557.         lea     esi, [window_data + ecx + WDATA.box]
  2558.         movsd
  2559.         movsd
  2560.         movsd
  2561.         movsd
  2562.  
  2563.     ; Process state (+50)
  2564.         movzx   eax, byte [SLOT_BASE + ecx*(sizeof.APPDATA/sizeof.WDATA) + APPDATA.state]
  2565.         stosd
  2566.  
  2567.     ; Window client area box
  2568.         lea     esi, [window_data + ecx + WDATA.clientbox]
  2569.         movsd
  2570.         movsd
  2571.         movsd
  2572.         movsd
  2573.  
  2574.     ; Window state
  2575.         mov     al, [window_data + ecx + WDATA.fl_wstate]
  2576.         stosb
  2577.  
  2578.     ; Event mask (+71)
  2579.         mov     EAX, dword [SLOT_BASE + ecx*(sizeof.APPDATA/sizeof.WDATA) + APPDATA.event_mask]
  2580.         stosd
  2581.  
  2582.     ; Keyboard mode (+75)
  2583.         mov     al, byte [SLOT_BASE + ecx*(sizeof.APPDATA/sizeof.WDATA) + APPDATA.keyboard_mode]
  2584.         stosb
  2585.  
  2586.         pop     esi
  2587.         pop     edi
  2588.  
  2589. .nofillbuf:
  2590.     ; return number of processes
  2591.  
  2592.         mov     eax, [thread_count]
  2593.         mov     [esp + SYSCALL_STACK.eax], eax
  2594.         ret
  2595.  
  2596. .addr_error:    ; if given memory address is illegal
  2597.         mov     dword [esp + SYSCALL_STACK.eax], -1
  2598.         ret  
  2599.  
  2600.  
  2601. ; redraw status
  2602. align 4
  2603. sys_redrawstat:
  2604.         cmp     ebx, 1
  2605.         jne     .no_widgets_away
  2606.         ; buttons away
  2607.         mov     ecx, [current_slot_idx]
  2608. .sys_newba2:
  2609.         mov     edi, [BTN_ADDR]
  2610.         cmp     [edi], dword 0  ; empty button list ?
  2611.         je      .end_of_buttons_away
  2612.         movzx   ebx, word [edi]
  2613.         inc     ebx
  2614.         mov     eax, edi
  2615. .sys_newba:
  2616.         dec     ebx
  2617.         jz      .end_of_buttons_away
  2618.  
  2619.         add     eax, 0x10
  2620.         cmp     cx, [eax]
  2621.         jnz     .sys_newba
  2622.  
  2623.         push    eax ebx ecx
  2624.         mov     ecx, ebx
  2625.         inc     ecx
  2626.         shl     ecx, 4
  2627.         mov     ebx, eax
  2628.         add     eax, 0x10
  2629.         call    memmove
  2630.         dec     dword [edi]
  2631.         pop     ecx ebx eax
  2632.  
  2633.         jmp     .sys_newba2
  2634.  
  2635. .end_of_buttons_away:
  2636.         ret
  2637.  
  2638. .no_widgets_away:
  2639.  
  2640.         cmp     ebx, 2
  2641.         jnz     .srl1
  2642.  
  2643.         mov     edx, [current_slot_idx]      ; return whole screen draw area for this app
  2644.         shl     edx, BSF sizeof.WDATA
  2645.         mov     [draw_data + edx + RECT.left], 0
  2646.         mov     [draw_data + edx + RECT.top], 0
  2647.         mov     eax, [_display.width]
  2648.         dec     eax
  2649.         mov     [draw_data + edx + RECT.right], eax
  2650.         mov     eax, [_display.height]
  2651.         dec     eax
  2652.         mov     [draw_data + edx + RECT.bottom], eax
  2653.  
  2654. .srl1:
  2655.         ret
  2656.  
  2657. ;ok - 100% work
  2658. ;nt - not tested
  2659. ;---------------------------------------------------------------------------------------------
  2660. ;eax
  2661. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  2662. ;1 - change task. Ret nothing. Block. ok.
  2663. ;2 - performance control
  2664. ; ebx
  2665. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  2666. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  2667. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  2668. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  2669. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  2670. ;eax
  2671. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2672. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2673. ;---------------------------------------------------------------------------------------------
  2674. iglobal
  2675. align 4
  2676. sheduler:
  2677.         dd      sys_sheduler.00
  2678.         dd      change_task
  2679.         dd      sys_sheduler.02
  2680.         dd      sys_sheduler.03
  2681.         dd      sys_sheduler.04
  2682. endg
  2683. sys_sheduler:
  2684. ;rewritten by <Lrz>  29.12.2009
  2685.         jmp     dword [sheduler + ebx*4]
  2686. ;.shed_counter:
  2687. .00:
  2688.         mov     eax, [context_counter]
  2689.         mov     [esp + SYSCALL_STACK.eax], eax
  2690.         ret
  2691.  
  2692. .02:
  2693. ;.perf_control:
  2694.         inc     ebx                     ;before ebx=2, ebx=3
  2695.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  2696.         jz      cache_disable
  2697.  
  2698.         dec     ebx                     ;ebx=2
  2699.         cmp     ebx, ecx                ;
  2700.         jz      cache_enable            ;if ecx=2 and ebx=2
  2701.  
  2702.         dec     ebx                     ;ebx=1
  2703.         cmp     ebx, ecx
  2704.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  2705.  
  2706.         dec     ebx
  2707.         test    ebx, ecx                ;ebx=0 and ecx=0
  2708.         jz      modify_pce              ;if ecx=0
  2709.  
  2710.         ret
  2711.  
  2712. .03:
  2713. ;.rdmsr_instr:
  2714. ;now counter in ecx
  2715. ;(edx:eax) esi:edi => edx:esi
  2716.         mov     eax, esi
  2717.         mov     ecx, edx
  2718.         rdmsr
  2719.         mov     [esp + SYSCALL_STACK.eax], eax
  2720.         mov     [esp + SYSCALL_STACK.ebx], edx           ;ret in ebx?
  2721.         ret
  2722.  
  2723. .04:
  2724. ;.wrmsr_instr:
  2725. ;now counter in ecx
  2726. ;(edx:eax) esi:edi => edx:esi
  2727.         ; Fast Call MSR can't be destroy
  2728.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  2729.         ; включаются/выключаются расширенные возможности
  2730.         cmp     edx, MSR_SYSENTER_CS
  2731.         je      @f
  2732.         cmp     edx, MSR_SYSENTER_ESP
  2733.         je      @f
  2734.         cmp     edx, MSR_SYSENTER_EIP
  2735.         je      @f
  2736.         cmp     edx, MSR_AMD_STAR
  2737.         je      @f
  2738.  
  2739.         mov     eax, esi
  2740.         mov     ecx, edx
  2741.         wrmsr
  2742.         ; mov   [esp + 32], eax
  2743.         ; mov   [esp + 20], edx ;ret in ebx?
  2744. @@:
  2745.         ret
  2746.  
  2747. cache_disable:
  2748.         mov     eax, cr0
  2749.         or      eax, 01100000_00000000_00000000_00000000b
  2750.         mov     cr0, eax
  2751.         wbinvd  ;set MESI
  2752.         ret
  2753.  
  2754. cache_enable:
  2755.         mov     eax, cr0
  2756.         and     eax, 10011111_11111111_11111111_11111111b
  2757.         mov     cr0, eax
  2758.         ret
  2759.  
  2760. is_cache_enabled:
  2761.         mov     eax, cr0
  2762.         mov     ebx, eax
  2763.         and     eax, 01100000_00000000_00000000_00000000b
  2764.         jz      cache_disabled
  2765.         mov     [esp + SYSCALL_STACK.eax], ebx
  2766. cache_disabled:
  2767.         mov     dword [esp + SYSCALL_STACK.eax], eax;0
  2768.         ret
  2769.  
  2770. modify_pce:
  2771.         mov     eax, cr4
  2772. ;       mov ebx,0
  2773. ;       or  bx,100000000b ;pce
  2774. ;       xor eax,ebx ;invert pce
  2775.         bts     eax, 8;pce=cr4[8]
  2776.         mov     cr4, eax
  2777.         mov     [esp + SYSCALL_STACK.eax], eax
  2778.         ret
  2779. ;---------------------------------------------------------------------------------------------
  2780.  
  2781.  
  2782. iglobal
  2783.   cpustring db 'CPU',0
  2784. endg
  2785.  
  2786. uglobal
  2787. background_defined    db    0    ; diamond, 11.04.2006
  2788. endg
  2789. ;-----------------------------------------------------------------------------
  2790. align 4
  2791. checkmisc:
  2792.         cmp     [ctrl_alt_del], 1
  2793.         jne     .nocpustart
  2794.  
  2795.         mov     ebp, cpustring
  2796.         call    fs_execute_from_sysdir
  2797.  
  2798.         mov     [ctrl_alt_del], 0
  2799. ;--------------------------------------
  2800. align 4
  2801. .nocpustart:
  2802.         cmp     [mouse_active], 1
  2803.         jne     .mouse_not_active
  2804.         mov     [mouse_active], 0
  2805.  
  2806.         xor     edi, edi
  2807.         mov     ebx, window_data
  2808.  
  2809.         mov     ecx, [thread_count]
  2810.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  2811.         shl     eax, BSF sizeof.APPDATA
  2812.         push    eax
  2813.  
  2814.         movzx   eax, word [MOUSE_X]
  2815.         movzx   edx, word [MOUSE_Y]
  2816. ;--------------------------------------
  2817. align 4
  2818. .set_mouse_event:
  2819.         add     edi, sizeof.APPDATA
  2820.         add     ebx, sizeof.WDATA
  2821.         test    [SLOT_BASE + edi + APPDATA.event_mask], EVM_MOUSE_FILTER
  2822.         jz      .pos_filter
  2823.  
  2824.         cmp     edi, [esp]                      ; skip if filtration active
  2825.         jne     .skip
  2826. ;--------------------------------------
  2827. align 4
  2828. .pos_filter:
  2829.         test    [SLOT_BASE + edi + APPDATA.event_mask], EVM_CURSOR_FILTER
  2830.         jz      .set
  2831.  
  2832.         mov     esi, [ebx + WDATA.box.left]
  2833.         cmp     eax, esi
  2834.         jb      .skip
  2835.         add     esi, [ebx + WDATA.box.width]
  2836.         cmp     eax, esi
  2837.         ja      .skip
  2838.  
  2839.         mov     esi, [ebx + WDATA.box.top]
  2840.         cmp     edx, esi
  2841.         jb      .skip
  2842.         add     esi, [ebx + WDATA.box.height]
  2843.         cmp     edx, esi
  2844.         ja      .skip
  2845. ;--------------------------------------
  2846. align 4
  2847. .set:
  2848.         or      [SLOT_BASE + edi + APPDATA.occurred_events], EVENT_MOUSE
  2849. ;--------------------------------------
  2850. align 4
  2851. .skip:
  2852.         loop    .set_mouse_event
  2853.  
  2854.         pop     eax
  2855. ;--------------------------------------
  2856. align 4
  2857. .mouse_not_active:
  2858.         cmp     [REDRAW_BACKGROUND], 0  ; background update ?
  2859.         jz      nobackgr
  2860.  
  2861.         cmp     [background_defined], 0
  2862.         jz      nobackgr
  2863. ;--------------------------------------
  2864. align 4
  2865. backgr:
  2866.         mov     eax, [draw_data + sizeof.WDATA + RECT.left]
  2867.         shl     eax, 16
  2868.         add     eax, [draw_data + sizeof.WDATA + RECT.right]
  2869.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  2870.  
  2871.         mov     eax, [draw_data + sizeof.WDATA + RECT.top]
  2872.         shl     eax, 16
  2873.         add     eax, [draw_data + sizeof.WDATA + RECT.bottom]
  2874.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  2875.  
  2876.         call    drawbackground
  2877. ;        DEBUGF  1, "K : drawbackground\n"
  2878. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  2879. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  2880. ;--------- set event 5 start ----------
  2881.         push    ecx edi
  2882.         xor     edi, edi
  2883.         mov     ecx, [thread_count]
  2884. ;--------------------------------------
  2885. align 4
  2886. set_bgr_event:
  2887.         add     edi, sizeof.APPDATA
  2888.         mov     eax, [BG_Rect_X_left_right]
  2889.         mov     edx, [BG_Rect_Y_top_bottom]
  2890.         cmp     [SLOT_BASE + edi + APPDATA.draw_bgr_x], 0
  2891.         jz      .set
  2892. .join:
  2893.         cmp     word [SLOT_BASE + edi + APPDATA.draw_bgr_x], ax
  2894.         jae     @f
  2895.         mov     word [SLOT_BASE + edi + APPDATA.draw_bgr_x], ax
  2896. @@:
  2897.         shr     eax, 16
  2898.         cmp     word [SLOT_BASE + edi + APPDATA.draw_bgr_x + 2], ax
  2899.         jbe     @f
  2900.         mov     word [SLOT_BASE + edi + APPDATA.draw_bgr_x + 2], ax
  2901. @@:
  2902.         cmp     word [SLOT_BASE + edi + APPDATA.draw_bgr_y], dx
  2903.         jae     @f
  2904.         mov     word [SLOT_BASE + edi + APPDATA.draw_bgr_y], dx
  2905. @@:
  2906.         shr     edx, 16
  2907.         cmp     word [SLOT_BASE + edi + APPDATA.draw_bgr_y+2], dx
  2908.         jbe     @f
  2909.         mov     word [SLOT_BASE + edi + APPDATA.draw_bgr_y+2], dx
  2910. @@:
  2911.         jmp     .common
  2912. .set:
  2913.         mov     [SLOT_BASE + edi + APPDATA.draw_bgr_x], eax
  2914.         mov     [SLOT_BASE + edi + APPDATA.draw_bgr_y], edx
  2915. .common:
  2916.         or      [SLOT_BASE + edi + APPDATA.occurred_events], EVENT_BACKGROUND
  2917.         loop    set_bgr_event
  2918.         pop     edi ecx
  2919. ;--------- set event 5 stop -----------
  2920.         dec     [REDRAW_BACKGROUND]     ; got new update request?
  2921.         jnz     backgr
  2922.  
  2923.         xor     eax, eax
  2924.         mov     [draw_data + sizeof.WDATA + RECT.left], eax
  2925.         mov     [draw_data + sizeof.WDATA + RECT.top], eax
  2926.         mov     [draw_data + sizeof.WDATA + RECT.right], eax
  2927.         mov     [draw_data + sizeof.WDATA + RECT.bottom], eax
  2928. ;--------------------------------------
  2929. align 4
  2930. nobackgr:
  2931. ; system shutdown request
  2932.         cmp     [SYS_SHUTDOWN], byte 0
  2933.         je      noshutdown
  2934.  
  2935.         mov     edx, [shutdown_processes]
  2936.  
  2937.         cmp     [SYS_SHUTDOWN], dl
  2938.         jne     noshutdown
  2939.  
  2940.         lea     ecx, [edx-1]
  2941.         mov     edx, SLOT_BASE + sizeof.APPDATA ;OS_BASE+0x3040
  2942.         jecxz   no_mark_system_shutdown
  2943. ;--------------------------------------
  2944. align 4
  2945. markz:
  2946.         push    ecx edx
  2947.         cmp     [edx + APPDATA.state], TSTATE_FREE
  2948.         jz      .nokill
  2949.         cmp     [edx + APPDATA.process], sys_proc
  2950.         jz      .nokill
  2951.         call    request_terminate
  2952.         jmp     .common
  2953. .nokill:
  2954.         dec     byte [SYS_SHUTDOWN]
  2955.         xor     eax, eax
  2956. .common:
  2957.         pop     edx ecx
  2958.         test    eax, eax
  2959.         jz      @f
  2960.         mov     [edx + APPDATA.state], TSTATE_ZOMBIE
  2961. @@:
  2962.         add     edx, sizeof.APPDATA
  2963.         loop    markz
  2964.         call    wakeup_osloop
  2965. ;--------------------------------------
  2966. align 4
  2967. @@:
  2968. no_mark_system_shutdown:
  2969.         dec     byte [SYS_SHUTDOWN]
  2970.         je      system_shutdown
  2971. ;--------------------------------------
  2972. align 4
  2973. noshutdown:
  2974.         mov     eax, [thread_count]           ; termination
  2975.         mov     ebx, SLOT_BASE + sizeof.APPDATA + APPDATA.state
  2976.         mov     esi, 1
  2977. ;--------------------------------------
  2978. align 4
  2979. newct:
  2980.         mov     cl, [ebx]
  2981.         cmp     cl, TSTATE_ZOMBIE
  2982.         jz      .terminate
  2983.  
  2984.         cmp     cl, TSTATE_TERMINATING
  2985.         jnz     .noterminate
  2986. .terminate:
  2987.         pushad
  2988.         mov     ecx, eax
  2989.         shl     ecx, BSF sizeof.APPDATA
  2990.         add     ecx, SLOT_BASE
  2991.         call    restore_default_cursor_before_killing
  2992.         popad
  2993.  
  2994.         pushad
  2995.         call    terminate
  2996.         popad
  2997.         cmp     byte [SYS_SHUTDOWN], 0
  2998.         jz      .noterminate
  2999.         dec     byte [SYS_SHUTDOWN]
  3000.         je      system_shutdown
  3001.  
  3002. .noterminate:
  3003.         add     ebx, sizeof.APPDATA
  3004.         inc     esi
  3005.         dec     eax
  3006.         jnz     newct
  3007.         ret
  3008. ;-----------------------------------------------------------------------------
  3009. align 4
  3010. redrawscreen:
  3011. ; eax , if process window_data base is eax, do not set flag/limits
  3012.  
  3013.         pushad
  3014.         push    eax
  3015.  
  3016. ;;;         mov   ebx,2
  3017. ;;;         call  delay_hs
  3018.  
  3019.          ;mov   ecx,0               ; redraw flags for apps
  3020.         xor     ecx, ecx
  3021. ;--------------------------------------
  3022. align 4
  3023. newdw2:
  3024.         inc     ecx
  3025.         push    ecx
  3026.  
  3027.         mov     eax, ecx
  3028.         shl     eax, BSF sizeof.WDATA
  3029.         add     eax, window_data
  3030.  
  3031.         cmp     eax, [esp+4]
  3032.         je      not_this_task
  3033.                                    ; check if window in redraw area
  3034.         mov     edi, eax
  3035.  
  3036.         cmp     ecx, 1             ; limit for background
  3037.         jz      bgli
  3038.  
  3039.         mov     eax, [esp+4]        ;if upper in z-position - no redraw
  3040.         test    eax, eax
  3041.         jz      @f
  3042.         mov     al, [eax + WDATA.z_modif]
  3043.         cmp     [edi + WDATA.z_modif], al
  3044.         jg      ricino
  3045.       @@:
  3046.  
  3047.         mov     eax, [edi + WDATA.box.left]
  3048.         mov     ebx, [edi + WDATA.box.top]
  3049.  
  3050.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3051.         cmp     ecx, ebx
  3052.         jb      ricino
  3053.  
  3054.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3055.         cmp     ecx, eax
  3056.         jb      ricino
  3057.  
  3058.         mov     eax, [edi + WDATA.box.left]
  3059.         mov     ebx, [edi + WDATA.box.top]
  3060.         mov     ecx, [edi + WDATA.box.width]
  3061.         mov     edx, [edi + WDATA.box.height]
  3062.         add     ecx, eax
  3063.         add     edx, ebx
  3064.  
  3065.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3066.         cmp     edx, eax
  3067.         jb      ricino
  3068.  
  3069.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3070.         cmp     ecx, eax
  3071.         jb      ricino
  3072. ;--------------------------------------
  3073. align 4
  3074. bgli:
  3075.         cmp     dword[esp], 1
  3076.         jnz     .az
  3077.  
  3078.         cmp     [REDRAW_BACKGROUND], 0
  3079.         jz      .az
  3080.  
  3081.         mov     dl, 0
  3082.         lea     eax, [edi + draw_data - window_data]
  3083.         mov     ebx, [draw_limits.left]
  3084.         cmp     ebx, [eax + RECT.left]
  3085.         jae     @f
  3086.  
  3087.         mov     [eax + RECT.left], ebx
  3088.         mov     dl, 1
  3089. ;--------------------------------------
  3090. align 4
  3091. @@:
  3092.         mov     ebx, [draw_limits.top]
  3093.         cmp     ebx, [eax + RECT.top]
  3094.         jae     @f
  3095.  
  3096.         mov     [eax + RECT.top], ebx
  3097.         mov     dl, 1
  3098. ;--------------------------------------
  3099. align 4
  3100. @@:
  3101.         mov     ebx, [draw_limits.right]
  3102.         cmp     ebx, [eax + RECT.right]
  3103.         jbe     @f
  3104.  
  3105.         mov     [eax + RECT.right], ebx
  3106.         mov     dl, 1
  3107. ;--------------------------------------
  3108. align 4
  3109. @@:
  3110.         mov     ebx, [draw_limits.bottom]
  3111.         cmp     ebx, [eax + RECT.bottom]
  3112.         jbe     @f
  3113.  
  3114.         mov     [eax + RECT.bottom], ebx
  3115.         mov     dl, 1
  3116. ;--------------------------------------
  3117. align 4
  3118. @@:
  3119.         add     [REDRAW_BACKGROUND], dl
  3120.         call    wakeup_osloop
  3121.         jmp     newdw8
  3122. ;--------------------------------------
  3123. align 4
  3124. .az:
  3125.         mov     eax, edi
  3126.         add     eax, draw_data-window_data
  3127.  
  3128.         mov     ebx, [draw_limits.left]        ; set limits
  3129.         mov     [eax + RECT.left], ebx
  3130.         mov     ebx, [draw_limits.top]
  3131.         mov     [eax + RECT.top], ebx
  3132.         mov     ebx, [draw_limits.right]
  3133.         mov     [eax + RECT.right], ebx
  3134.         mov     ebx, [draw_limits.bottom]
  3135.         mov     [eax + RECT.bottom], ebx
  3136.  
  3137.         sub     eax, draw_data-window_data
  3138.  
  3139.         cmp     dword [esp], 1
  3140.         jne     nobgrd
  3141.         inc     [REDRAW_BACKGROUND]
  3142.         call    wakeup_osloop
  3143. ;--------------------------------------
  3144. align 4
  3145. newdw8:
  3146. nobgrd:
  3147. ;--------------------------------------
  3148.         push    eax  edi ebp
  3149.         mov     edi, [esp+12]
  3150.         cmp     edi, 1
  3151.         je      .found
  3152.  
  3153.         mov     eax, [draw_limits.left]
  3154.         mov     ebx, [draw_limits.top]
  3155.         mov     ecx, [draw_limits.right]
  3156.         sub     ecx, eax
  3157.         test    ecx, ecx
  3158.         jz      .not_found
  3159.  
  3160.         mov     edx, [draw_limits.bottom]
  3161.         sub     edx, ebx
  3162.         test    edx, edx
  3163.         jz      .not_found
  3164.  
  3165. ; eax - x, ebx - y
  3166. ; ecx - size x, edx - size y
  3167.         add     ebx, edx
  3168. ;--------------------------------------
  3169. align 4
  3170. .start_y:
  3171.         push    ecx
  3172. ;--------------------------------------
  3173. align 4
  3174. .start_x:
  3175.         add     eax, ecx
  3176.         mov     ebp, [d_width_calc_area + ebx*4]
  3177.         add     ebp, [_display.win_map]
  3178.         movzx   ebp, byte[eax+ebp] ; get value for current point
  3179.         cmp     ebp, edi
  3180.         jne     @f
  3181.  
  3182.         pop     ecx
  3183.         jmp     .found
  3184. ;--------------------------------------
  3185. align 4
  3186. @@:
  3187.         sub     eax, ecx
  3188.  
  3189.         dec     ecx
  3190.         jns     .start_x
  3191.  
  3192.         pop     ecx
  3193.         dec     ebx
  3194.         dec     edx
  3195.         jns     .start_y
  3196. ;--------------------------------------
  3197. align 4
  3198. .not_found:
  3199.         pop     ebp edi eax
  3200.         jmp     ricino
  3201. ;--------------------------------------
  3202. align 4
  3203. .found:
  3204.         pop     ebp edi eax
  3205.  
  3206.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3207. ;--------------------------------------
  3208. align 4
  3209. ricino:
  3210. not_this_task:
  3211.         pop     ecx
  3212.  
  3213.         cmp     ecx, [thread_count]
  3214.         jle     newdw2
  3215.  
  3216.         pop     eax
  3217.         popad
  3218.         ret
  3219. ;-----------------------------------------------------------------------------
  3220. align 4
  3221. calculatebackground:   ; background
  3222.         mov     edi, [_display.win_map]              ; set os to use all pixels
  3223.         mov     eax, 0x01010101
  3224.         mov     ecx, [_display.win_map_size]
  3225.         shr     ecx, 2
  3226.         rep stosd
  3227.         mov     byte[window_data + sizeof.WDATA + WDATA.z_modif], ZPOS_DESKTOP
  3228.         mov     [REDRAW_BACKGROUND], 0
  3229.         ret
  3230. ;-----------------------------------------------------------------------------
  3231. uglobal
  3232.   imax    dd 0x0
  3233. endg
  3234. ;-----------------------------------------------------------------------------
  3235. align 4
  3236. delay_ms:     ; delay in 1/1000 sec
  3237.         pushad
  3238.  
  3239.         cmp     [hpet_base], 0
  3240.         jz      .no_hpet
  3241.         mov     eax, esi
  3242.         mov     edx, 1_000_000 ; ms to ns
  3243.         mul     edx
  3244.         mov     ebx, edx
  3245.         mov     ecx, eax
  3246.  
  3247.         push    ecx
  3248.         call    get_clock_ns
  3249.         pop     ecx
  3250.         mov     edi, edx
  3251.         mov     esi, eax
  3252. .wait:
  3253.         push    ecx
  3254.         call    get_clock_ns
  3255.         pop     ecx
  3256.         sub     eax, esi
  3257.         sbb     edx, edi
  3258.         sub     eax, ecx
  3259.         sbb     edx, ebx
  3260.         jc      .wait
  3261.         jmp     .done
  3262.  
  3263. .no_hpet:
  3264.         mov     ecx, esi
  3265.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3266.         imul    ecx, 33941
  3267.         shr     ecx, 9
  3268.         ; </CPU clock fix>
  3269.  
  3270.         in      al, 0x61
  3271.         and     al, 0x10
  3272.         mov     ah, al
  3273.         cld
  3274.  
  3275. .cnt1:
  3276.         in      al, 0x61
  3277.         and     al, 0x10
  3278.         cmp     al, ah
  3279.         jz      .cnt1
  3280.  
  3281.         mov     ah, al
  3282.         loop    .cnt1
  3283.  
  3284. .done:
  3285.         popad
  3286.         ret
  3287. ;-----------------------------------------------------------------------------
  3288. align 4
  3289. set_app_param:
  3290.         mov     edi, [current_slot]
  3291.         xchg    ebx, [edi + APPDATA.event_mask] ; set new event mask
  3292.         mov     [esp + SYSCALL_STACK.eax], ebx  ; return old mask value
  3293.         ret
  3294. ;-----------------------------------------------------------------------------
  3295.  
  3296. ; this is for syscall
  3297. proc delay_hs_unprotected
  3298.         call    unprotect_from_terminate
  3299.         call    delay_hs
  3300.         call    protect_from_terminate
  3301.         ret
  3302. endp
  3303.  
  3304. if 1
  3305. align 4
  3306. delay_hs:     ; delay in 1/100 secs
  3307. ; ebx = delay time
  3308.         pushad
  3309.         push    ebx
  3310.         xor     esi, esi
  3311.         mov     ecx, MANUAL_DESTROY
  3312.         call    create_event
  3313.         test    eax, eax
  3314.         jz      .done
  3315.  
  3316.         mov     ebx, edx
  3317.         mov     ecx, [esp]
  3318.         push    edx
  3319.         push    eax
  3320.         call    wait_event_timeout
  3321.         pop     eax
  3322.         pop     ebx
  3323.         call    destroy_event
  3324. .done:
  3325.         add     esp, 4
  3326.         popad
  3327.         ret
  3328.  
  3329. else
  3330.  
  3331. align 4
  3332. delay_hs:     ; delay in 1/100 secs
  3333. ; ebx = delay time
  3334.         push    ecx
  3335.         push    edx
  3336.  
  3337.         mov     edx, [timer_ticks]
  3338. ;--------------------------------------
  3339. align 4
  3340. .newtic:
  3341.         mov     ecx, [timer_ticks]
  3342.         sub     ecx, edx
  3343.         cmp     ecx, ebx
  3344.         jae     .zerodelay
  3345.  
  3346.         call    change_task
  3347.  
  3348.         jmp     .newtic
  3349. ;--------------------------------------
  3350. align 4
  3351. .zerodelay:
  3352.         pop     edx
  3353.         pop     ecx
  3354.         ret
  3355. end if
  3356.  
  3357. ;-----------------------------------------------------------------------------
  3358. align 16        ;very often call this subrutine
  3359. memmove:       ; memory move in bytes
  3360. ; eax = from
  3361. ; ebx = to
  3362. ; ecx = no of bytes
  3363.         test    ecx, ecx
  3364.         jle     .ret
  3365.  
  3366.         push    esi edi ecx
  3367.  
  3368.         mov     edi, ebx
  3369.         mov     esi, eax
  3370.  
  3371.         test    ecx, not 11b
  3372.         jz      @f
  3373.  
  3374.         push    ecx
  3375.         shr     ecx, 2
  3376.         rep movsd
  3377.         pop     ecx
  3378.         and     ecx, 11b
  3379.         jz      .finish
  3380. ;--------------------------------------
  3381. align 4
  3382. @@:
  3383.         rep movsb
  3384. ;--------------------------------------
  3385. align 4
  3386. .finish:
  3387.         pop     ecx edi esi
  3388. ;--------------------------------------
  3389. align 4
  3390. .ret:
  3391.         ret
  3392. ;-----------------------------------------------------------------------------
  3393.  
  3394. ; in: eax = port
  3395. ;     ebp = subfunction
  3396. ;          0 - set access
  3397. ;          1 - clear access
  3398. ; out: not return value
  3399. align 4
  3400. set_io_access_rights:
  3401.         push    edi eax
  3402.         mov     edi, tss._io_map_0
  3403.  
  3404.         test    ebp, ebp         ; enable access - ebp = 0
  3405.         jnz     .siar1
  3406.  
  3407.         btr     [edi], eax
  3408.         pop     eax edi
  3409.         ret
  3410. .siar1:
  3411.         bts     [edi], eax      ; disable access - ebp = 1
  3412.         pop     eax edi
  3413.         ret
  3414.  
  3415. align 4
  3416. ; @brief ReservePortArea and FreePortArea
  3417. ; @param edx number end arrea of ports (include last number of port)
  3418. ; @param ecx number start arrea of ports
  3419. ; @param ebx sub function 0 - reserve, 1 - free
  3420. ; @param eax 46 - number function
  3421. ; @returns  eax = 0 - succesful eax = 1 - error
  3422. syscall_reserveportarea:        ; ReservePortArea and FreePortArea
  3423.  
  3424.         call    r_f_port_area
  3425.         mov     [esp + SYSCALL_STACK.eax], eax
  3426.         ret
  3427.  
  3428. ;reserve/free group of ports
  3429. ;  * eax = 46 - number function
  3430. ;  * ebx = 0 - reserve, 1 - free
  3431. ;  * ecx = number start arrea of ports
  3432. ;  * edx = number end arrea of ports (include last number of port)
  3433. ;Return value:
  3434. ;  * eax = 0 - succesful
  3435. ;  * eax = 1 - error
  3436. ;  * The system has reserve this ports:
  3437. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3438. ;destroys eax,ebx, ebp
  3439. r_f_port_area:
  3440.  
  3441.         test    ebx, ebx
  3442.         jnz     .free_port_area
  3443.  
  3444.         cmp     ecx, edx      ; beginning > end ?
  3445.         ja      .err
  3446.         cmp     edx, 65536    ;test ebx, not 0xffff
  3447.         jae     .err
  3448.         mov     eax, [RESERVED_PORTS]
  3449.         test    eax, eax      ; no reserved areas ?
  3450.         je      .rpal2
  3451.         cmp     eax, 255      ; max reserved
  3452.         jae     .err
  3453.  .rpal3:
  3454.         mov     ebx, eax
  3455.         shl     ebx, 4   ;16 byte is sizeof item in RESERVED_PORTS table
  3456.         add     ebx, RESERVED_PORTS
  3457.         cmp     ecx, [ebx+8]
  3458.         ja      .rpal4
  3459.         cmp     edx, [ebx+4]
  3460.         jae     .err
  3461.  .rpal4:
  3462.         dec     eax
  3463.         jnz     .rpal3
  3464.         jmp     .rpal2
  3465. .err:
  3466.         xor     eax, eax
  3467.         inc     eax
  3468.         ret
  3469.  
  3470.    .rpal2:
  3471.      ; enable port access at port IO map
  3472.         pushad                        ; start enable io map
  3473.         mov     eax, ecx
  3474.         xor     ebp, ebp               ; enable - eax = port
  3475.         cli
  3476. .new_port_access:
  3477.         call    set_io_access_rights
  3478.  
  3479.         inc     eax
  3480.         cmp     eax, edx
  3481.         jbe     .new_port_access
  3482.  
  3483.         sti
  3484.         popad                         ; end enable io map
  3485.  
  3486.         mov     eax, [RESERVED_PORTS]
  3487.         inc     eax
  3488.         mov     [RESERVED_PORTS], eax
  3489.         shl     eax, 4
  3490.         add     eax, RESERVED_PORTS
  3491.         mov     ebx, [current_slot]
  3492.         mov     ebx, [ebx + APPDATA.tid]
  3493.         mov     [eax], ebx   ; tid
  3494.         mov     [eax+4], ecx ;start port
  3495.         mov     [eax+8], edx ;finish port
  3496.  
  3497.         xor     eax, eax
  3498.         ret
  3499.  
  3500. .free_port_area:
  3501.  
  3502.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  3503.         test    eax, eax
  3504.         jz      .frpal2
  3505.         mov     ebx, [current_slot]
  3506.         mov     ebx, [ebx + APPDATA.tid]
  3507.    .frpal3:
  3508.         mov     edi, eax
  3509.         shl     edi, 4
  3510.         add     edi, RESERVED_PORTS
  3511.         cmp     ebx, [edi]
  3512.         jne     .frpal4
  3513.         cmp     ecx, [edi+4]
  3514.         jne     .frpal4
  3515.         cmp     edx, [edi+8]
  3516.         jne     .frpal4
  3517.         jmp     .frpal1
  3518.    .frpal4:
  3519.         dec     eax
  3520.         jnz     .frpal3
  3521.    .frpal2:
  3522.         inc     eax
  3523.         ret
  3524.    .frpal1:
  3525.         push    ecx
  3526.         mov     ecx, 256
  3527.         sub     ecx, eax
  3528.         shl     ecx, 4
  3529.         mov     esi, edi
  3530.         add     esi, 16
  3531.         cld
  3532.         rep movsb
  3533.  
  3534.         dec     dword [RESERVED_PORTS]
  3535. ;disable port access at port IO map
  3536.                         ; start disable io map
  3537.         pop     eax     ;start port
  3538.         ;cmp     edx, 65536
  3539.         ;jge     no_mask_io
  3540.  
  3541.         xor     ebp, ebp
  3542.         inc     ebp
  3543. .new_port_access_disable:           ; disable - eax = port
  3544.         call    set_io_access_rights
  3545.  
  3546.         inc     eax
  3547.         cmp     eax, edx
  3548.         jbe     .new_port_access_disable
  3549.                                     ; end disable io map
  3550.         xor     eax, eax
  3551.         ret
  3552. ;-----------------------------------------------------------------------------
  3553. align 4
  3554. drawbackground:
  3555.         cmp     [BgrDrawMode], dword 1
  3556.         jne     .bgrstr
  3557.         call    vesa20_drawbackground_tiled
  3558. ;        call    [draw_pointer]
  3559.         call    __sys_draw_pointer
  3560.         ret
  3561. ;--------------------------------------
  3562. align 4
  3563. .bgrstr:
  3564.         call    vesa20_drawbackground_stretch
  3565. ;        call    [draw_pointer]
  3566.         call    __sys_draw_pointer
  3567.         ret
  3568. ;-----------------------------------------------------------------------------
  3569. align 4
  3570. syscall_putimage:                       ; PutImage
  3571. ; add check pointer
  3572.         push    ecx
  3573.         mov     ax, cx
  3574.         shr     ecx, 16
  3575.         imul    eax, ecx
  3576.         lea     eax, [eax*3]
  3577.         stdcall is_region_userspace, ebx, eax
  3578.         pop     ecx
  3579.         jnz     sys_putimage.exit
  3580.  
  3581. sys_putimage:
  3582.         test    ecx, 0x80008000
  3583.         jnz     .exit
  3584.         test    ecx, 0x0000FFFF
  3585.         jz      .exit
  3586.         test    ecx, 0xFFFF0000
  3587.         jnz     @f
  3588. ;--------------------------------------
  3589. align 4
  3590. .exit:
  3591.         ret
  3592. ;--------------------------------------
  3593. align 4
  3594. @@:
  3595.         mov     edi, [current_slot_idx]
  3596.         shl     edi, BSF sizeof.WDATA
  3597.         add     dx, word[window_data + edi + WDATA.clientbox.top]
  3598.         rol     edx, 16
  3599.         add     dx, word[window_data + edi + WDATA.clientbox.left]
  3600.         rol     edx, 16
  3601. ;--------------------------------------
  3602. align 4
  3603. .forced:
  3604.         push    ebp esi 0
  3605.         mov     ebp, putimage_get24bpp
  3606.         mov     esi, putimage_init24bpp
  3607. ;--------------------------------------
  3608. align 4
  3609. sys_putimage_bpp:
  3610.         call    vesa20_putimage
  3611.         pop     ebp esi ebp
  3612.         ret
  3613. ;        jmp     [draw_pointer]
  3614. ;-----------------------------------------------------------------------------
  3615. align 4
  3616. sys_putimage_palette:
  3617. ; ebx = pointer to image
  3618. ; ecx = [xsize]*65536 + [ysize]
  3619. ; edx = [xstart]*65536 + [ystart]
  3620. ; esi = number of bits per pixel, must be 8, 24 or 32
  3621. ; edi = pointer to palette
  3622. ; ebp = row delta
  3623. ; check pointer
  3624.         push    ecx esi
  3625.         mov     ax, cx
  3626.         shr     ecx, 16
  3627.         imul    eax, ecx
  3628. ;        imul    eax, esi ; eax*count bit in 1 pixel
  3629. ;        shr     eax, 3
  3630.         stdcall is_region_userspace, ebx, eax
  3631.         pop     esi ecx
  3632.         jnz     sys_putimage.exit
  3633.  
  3634.         mov     eax, [current_slot_idx]
  3635.         shl     eax, BSF sizeof.WDATA
  3636.         add     dx, word [window_data + eax + WDATA.clientbox.top]
  3637.         rol     edx, 16
  3638.         add     dx, word [window_data + eax + WDATA.clientbox.left]
  3639.         rol     edx, 16
  3640. ;--------------------------------------
  3641. align 4
  3642. .forced:
  3643.         cmp     esi, 1
  3644.         jnz     @f
  3645.         push    edi
  3646.         mov     eax, [edi+4]
  3647.         sub     eax, [edi]
  3648.         push    eax
  3649.         push    dword [edi]
  3650.         push    0ffffff80h
  3651.         mov     edi, esp
  3652.         call    put_mono_image
  3653.         add     esp, 12
  3654.         pop     edi
  3655.         ret
  3656. ;--------------------------------------
  3657. align 4
  3658. @@:
  3659.         cmp     esi, 2
  3660.         jnz     @f
  3661.         push    edi
  3662.         push    0ffffff80h
  3663.         mov     edi, esp
  3664.         call    put_2bit_image
  3665.         pop     eax
  3666.         pop     edi
  3667.         ret
  3668. ;--------------------------------------
  3669. align 4
  3670. @@:
  3671.         cmp     esi, 4
  3672.         jnz     @f
  3673.         push    edi
  3674.         push    0ffffff80h
  3675.         mov     edi, esp
  3676.         call    put_4bit_image
  3677.         pop     eax
  3678.         pop     edi
  3679.         ret
  3680. ;--------------------------------------
  3681. align 4
  3682. @@:
  3683.         push    ebp esi ebp
  3684.         cmp     esi, 8
  3685.         jnz     @f
  3686.         mov     ebp, putimage_get8bpp
  3687.         mov     esi, putimage_init8bpp
  3688.         jmp     sys_putimage_bpp
  3689. ;--------------------------------------
  3690. align 4
  3691. @@:
  3692.         cmp     esi, 9
  3693.         jnz     @f
  3694.         mov     ebp, putimage_get9bpp
  3695.         mov     esi, putimage_init9bpp
  3696.         jmp     sys_putimage_bpp
  3697. ;--------------------------------------
  3698. align 4
  3699. @@:
  3700.         cmp     esi, 15
  3701.         jnz     @f
  3702.         mov     ebp, putimage_get15bpp
  3703.         mov     esi, putimage_init15bpp
  3704.         jmp     sys_putimage_bpp
  3705. ;--------------------------------------
  3706. align 4
  3707. @@:
  3708.         cmp     esi, 16
  3709.         jnz     @f
  3710.         mov     ebp, putimage_get16bpp
  3711.         mov     esi, putimage_init16bpp
  3712.         jmp     sys_putimage_bpp
  3713. ;--------------------------------------
  3714. align 4
  3715. @@:
  3716.         cmp     esi, 24
  3717.         jnz     @f
  3718.         mov     ebp, putimage_get24bpp
  3719.         mov     esi, putimage_init24bpp
  3720.         jmp     sys_putimage_bpp
  3721. ;--------------------------------------
  3722. align 4
  3723. @@:
  3724.         cmp     esi, 32
  3725.         jnz     @f
  3726.         mov     ebp, putimage_get32bpp
  3727.         mov     esi, putimage_init32bpp
  3728.         jmp     sys_putimage_bpp
  3729. ;--------------------------------------
  3730. align 4
  3731. @@:
  3732.         pop     ebp esi ebp
  3733.         ret
  3734. ;-----------------------------------------------------------------------------
  3735. align 4
  3736. put_mono_image:
  3737.         push    ebp esi ebp
  3738.         mov     ebp, putimage_get1bpp
  3739.         mov     esi, putimage_init1bpp
  3740.         jmp     sys_putimage_bpp
  3741. ;-----------------------------------------------------------------------------
  3742. align 4
  3743. put_2bit_image:
  3744.         push    ebp esi ebp
  3745.         mov     ebp, putimage_get2bpp
  3746.         mov     esi, putimage_init2bpp
  3747.         jmp     sys_putimage_bpp
  3748. ;-----------------------------------------------------------------------------
  3749. align 4
  3750. put_4bit_image:
  3751.         push    ebp esi ebp
  3752.         mov     ebp, putimage_get4bpp
  3753.         mov     esi, putimage_init4bpp
  3754.         jmp     sys_putimage_bpp
  3755. ;-----------------------------------------------------------------------------
  3756. align 4
  3757. putimage_init24bpp:
  3758.         lea     eax, [eax*3]
  3759. putimage_init8bpp:
  3760. putimage_init9bpp:
  3761.         ret
  3762. ;-----------------------------------------------------------------------------
  3763. align 16
  3764. putimage_get24bpp:
  3765.         movzx   eax, byte [esi+2]
  3766.         shl     eax, 16
  3767.         mov     ax, [esi]
  3768.         add     esi, 3
  3769.         ret     4
  3770. ;-----------------------------------------------------------------------------
  3771. align 16
  3772. putimage_get8bpp:
  3773.         movzx   eax, byte [esi]
  3774.         push    edx
  3775.         mov     edx, [esp+8]
  3776.         mov     eax, [edx + eax*4]
  3777.         pop     edx
  3778.         inc     esi
  3779.         ret     4
  3780. ;-----------------------------------------------------------------------------
  3781. align 16
  3782. putimage_get9bpp:
  3783.         lodsb
  3784.         mov     ah, al
  3785.         shl     eax, 8
  3786.         mov     al, ah
  3787.         ret     4
  3788. ;-----------------------------------------------------------------------------
  3789. align 4
  3790. putimage_init1bpp:
  3791.         add     eax, ecx
  3792.         push    ecx
  3793.         add     eax, 7
  3794.         add     ecx, 7
  3795.         shr     eax, 3
  3796.         shr     ecx, 3
  3797.         sub     eax, ecx
  3798.         pop     ecx
  3799.         ret
  3800. ;-----------------------------------------------------------------------------
  3801. align 16
  3802. putimage_get1bpp:
  3803.         push    edx
  3804.         mov     edx, [esp+8]
  3805.         mov     al, [edx]
  3806.         add     al, al
  3807.         jnz     @f
  3808.         lodsb
  3809.         adc     al, al
  3810. @@:
  3811.         mov     [edx], al
  3812.         sbb     eax, eax
  3813.         and     eax, [edx+8]
  3814.         add     eax, [edx+4]
  3815.         pop     edx
  3816.         ret     4
  3817. ;-----------------------------------------------------------------------------
  3818. align 4
  3819. putimage_init2bpp:
  3820.         add     eax, ecx
  3821.         push    ecx
  3822.         add     ecx, 3
  3823.         add     eax, 3
  3824.         shr     ecx, 2
  3825.         shr     eax, 2
  3826.         sub     eax, ecx
  3827.         pop     ecx
  3828.         ret
  3829. ;-----------------------------------------------------------------------------
  3830. align 16
  3831. putimage_get2bpp:
  3832.         push    edx
  3833.         mov     edx, [esp+8]
  3834.         mov     al, [edx]
  3835.         mov     ah, al
  3836.         shr     al, 6
  3837.         shl     ah, 2
  3838.         jnz     .nonewbyte
  3839.         lodsb
  3840.         mov     ah, al
  3841.         shr     al, 6
  3842.         shl     ah, 2
  3843.         add     ah, 1
  3844. .nonewbyte:
  3845.         mov     [edx], ah
  3846.         mov     edx, [edx+4]
  3847.         movzx   eax, al
  3848.         mov     eax, [edx + eax*4]
  3849.         pop     edx
  3850.         ret     4
  3851. ;-----------------------------------------------------------------------------
  3852. align 4
  3853. putimage_init4bpp:
  3854.         add     eax, ecx
  3855.         push    ecx
  3856.         add     ecx, 1
  3857.         inc     eax      ;add   eax, 1
  3858.         shr     ecx, 1
  3859.         shr     eax, 1
  3860.         sub     eax, ecx
  3861.         pop     ecx
  3862.         ret
  3863. ;-----------------------------------------------------------------------------
  3864. align 16
  3865. putimage_get4bpp:
  3866.         push    edx
  3867.         mov     edx, [esp+8]
  3868.         add     byte [edx], 80h
  3869.         jc      @f
  3870.         movzx   eax, byte [edx+1]
  3871.         mov     edx, [edx+4]
  3872.         and     eax, 0x0F
  3873.         mov     eax, [edx + eax*4]
  3874.         pop     edx
  3875.         ret     4
  3876. @@:
  3877.         movzx   eax, byte [esi]
  3878.         add     esi, 1
  3879.         mov     [edx+1], al
  3880.         shr     eax, 4
  3881.         mov     edx, [edx+4]
  3882.         mov     eax, [edx + eax*4]
  3883.         pop     edx
  3884.         ret     4
  3885. ;-----------------------------------------------------------------------------
  3886. align 4
  3887. putimage_init32bpp:
  3888.         shl     eax, 2
  3889.         ret
  3890. ;-----------------------------------------------------------------------------
  3891. align 16
  3892. putimage_get32bpp:
  3893.         lodsd
  3894.         ret     4
  3895. ;-----------------------------------------------------------------------------
  3896. align 4
  3897. putimage_init15bpp:
  3898. putimage_init16bpp:
  3899.         add     eax, eax
  3900.         ret
  3901. ;-----------------------------------------------------------------------------
  3902. align 16
  3903. putimage_get15bpp:
  3904. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  3905.         push    ecx edx
  3906.         movzx   eax, word [esi]
  3907.         add     esi, 2
  3908.         mov     ecx, eax
  3909.         mov     edx, eax
  3910.         and     eax, 0x1F
  3911.         and     ecx, 0x1F shl 5
  3912.         and     edx, 0x1F shl 10
  3913.         shl     eax, 3
  3914.         shl     ecx, 6
  3915.         shl     edx, 9
  3916.         or      eax, ecx
  3917.         or      eax, edx
  3918.         pop     edx ecx
  3919.         ret     4
  3920. ;-----------------------------------------------------------------------------
  3921. align 16
  3922. putimage_get16bpp:
  3923. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  3924.         push    ecx edx
  3925.         movzx   eax, word [esi]
  3926.         add     esi, 2
  3927.         mov     ecx, eax
  3928.         mov     edx, eax
  3929.         and     eax, 0x1F
  3930.         and     ecx, 0x3F shl 5
  3931.         and     edx, 0x1F shl 11
  3932.         shl     eax, 3
  3933.         shl     ecx, 5
  3934.         shl     edx, 8
  3935.         or      eax, ecx
  3936.         or      eax, edx
  3937.         pop     edx ecx
  3938.         ret     4
  3939. ;-----------------------------------------------------------------------------
  3940. ;align 4
  3941. ; eax x beginning
  3942. ; ebx y beginning
  3943. ; ecx x end
  3944.         ; edx y end
  3945. ; edi color
  3946. ;__sys_drawbar:
  3947. ;        mov     esi, [current_slot_idx]
  3948. ;        shl     esi, BSF sizeof.WDATA
  3949. ;        add     eax, [window_data+esi+WDATA.clientbox.left]
  3950. ;        add     ecx, [window_data+esi+WDATA.clientbox.left]
  3951. ;        add     ebx, [window_data+esi+WDATA.clientbox.top]
  3952. ;        add     edx, [window_data+esi+WDATA.clientbox.top]
  3953. ;--------------------------------------
  3954. ;align 4
  3955. ;.forced:
  3956. ;        call    vesa20_drawbar
  3957. ;        call    [draw_pointer]
  3958. ;        ret
  3959. ;-----------------------------------------------------------------------------
  3960. if used _rdtsc
  3961. _rdtsc:
  3962.         bt      [cpu_caps], CAPS_TSC
  3963.         jnc     .ret_rdtsc
  3964.         rdtsc
  3965.         ret
  3966.    .ret_rdtsc:
  3967.         mov     edx, 0xffffffff
  3968.         mov     eax, 0xffffffff
  3969.         ret
  3970. end if
  3971.  
  3972. sys_msg_board_str:
  3973.  
  3974.         pushad
  3975.    @@:
  3976.         cmp     [esi], byte 0
  3977.         je      @f
  3978.         mov     ebx, 1
  3979.         movzx   ecx, byte [esi]
  3980.         call    sys_msg_board
  3981.         inc     esi
  3982.         jmp     @b
  3983.    @@:
  3984.         popad
  3985.         ret
  3986.  
  3987. sys_msg_board_byte:
  3988. ; in: al = byte to display
  3989. ; out: nothing
  3990. ; destroys: nothing
  3991.         pushad
  3992.         mov     ecx, 2
  3993.         shl     eax, 24
  3994.         jmp     @f
  3995.  
  3996. sys_msg_board_word:
  3997. ; in: ax = word to display
  3998. ; out: nothing
  3999. ; destroys: nothing
  4000.         pushad
  4001.         mov     ecx, 4
  4002.         shl     eax, 16
  4003.         jmp     @f
  4004.  
  4005. sys_msg_board_dword:
  4006. ; in: eax = dword to display
  4007. ; out: nothing
  4008. ; destroys: nothing
  4009.         pushad
  4010.         mov     ecx, 8
  4011. @@:
  4012.         push    ecx
  4013.         rol     eax, 4
  4014.         push    eax
  4015.         and     al, 0xF
  4016.         cmp     al, 10
  4017.         sbb     al, 69h
  4018.         das
  4019.         mov     cl, al
  4020.         xor     ebx, ebx
  4021.         inc     ebx
  4022.         call    sys_msg_board
  4023.         pop     eax
  4024.         pop     ecx
  4025.         loop    @b
  4026.         popad
  4027.         ret
  4028.  
  4029. msg_board_data_size = 65536 ; Must be power of two
  4030.  
  4031. uglobal
  4032. msg_board_data  rb  msg_board_data_size
  4033. msg_board_count dd  ?
  4034. endg
  4035.  
  4036. iglobal
  4037. msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
  4038. endg
  4039.  
  4040. sys_msg_board:
  4041. ; ebx=1 -> write, cl = byte to write
  4042. ; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
  4043.         push    eax ebx
  4044.         mov     eax, ebx
  4045.         mov     ebx, ecx
  4046.         mov     ecx, [msg_board_count]
  4047.         cmp     eax, 1
  4048.         jne     .read
  4049.  
  4050. if defined debug_com_base
  4051.         push    dx ax
  4052. @@: ; wait for empty transmit register
  4053.         mov     dx, debug_com_base+5
  4054.         in      al, dx
  4055.         test    al, 1 shl 5
  4056.         jz      @r
  4057.         mov     dx, debug_com_base      ; Output the byte
  4058.         mov     al, bl
  4059.         out     dx, al
  4060.         pop     ax dx
  4061. end if
  4062.  
  4063.         mov     [msg_board_data+ecx], bl
  4064.         cmp     byte [debug_direct_print], 1
  4065.         jnz     .end
  4066.         pusha
  4067.         lea     edx, [msg_board_data+ecx]
  4068.         mov     ecx, 0x40FFFFFF
  4069.         mov     ebx, [msg_board_pos]
  4070.         mov     edi, 1
  4071.         mov     esi, edi ;1
  4072.         call    dtext
  4073.         popa
  4074.         add     word [msg_board_pos+2], 6
  4075.         cmp     word [msg_board_pos+2], 105*6
  4076.         jnc     @f
  4077.         cmp     bl, 10
  4078.         jnz     .end
  4079. @@:
  4080.         mov     word [msg_board_pos+2], 42*6
  4081.         add     word [msg_board_pos], 10
  4082.         mov     eax, [_display.height]
  4083.         sub     eax, 10
  4084.         cmp     ax, word [msg_board_pos]
  4085.         jnc     @f
  4086.         mov     word [msg_board_pos], 10
  4087. @@:
  4088.         pusha
  4089.         mov     eax, [msg_board_pos]
  4090.         movzx   ebx, ax
  4091.         shr     eax, 16
  4092.         mov     edx, 105*6
  4093.         xor     ecx, ecx
  4094.         mov     edi, 1
  4095.         mov     esi, 9
  4096. @@:
  4097.         call    hline
  4098.         inc     ebx
  4099.         dec     esi
  4100.         jnz     @b
  4101.         popa
  4102. .end:
  4103.         inc     ecx
  4104.         and     ecx, msg_board_data_size - 1
  4105.         mov     [msg_board_count], ecx
  4106. .ret:
  4107.         pop     ebx eax
  4108.         ret
  4109.  
  4110. .read:
  4111.         cmp     eax, 2
  4112.         jne     .ret
  4113.         add     esp, 8  ; returning data in ebx and eax, so no need to restore them
  4114.         test    ecx, ecx
  4115.         jnz     @f
  4116.         mov     [esp + SYSCALL_STACK.eax], ecx
  4117.         mov     [esp + SYSCALL_STACK.ebx], ecx
  4118.         ret
  4119. @@:
  4120.         mov     eax, msg_board_data+1
  4121.         mov     ebx, msg_board_data
  4122.         movzx   edx, byte [ebx]
  4123.         call    memmove
  4124.         dec     [msg_board_count]
  4125.         mov     [esp + SYSCALL_STACK.eax], edx
  4126.         mov     [esp + SYSCALL_STACK.ebx], 1
  4127.         ret
  4128.  
  4129. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4130. ;; 61 sys function.                                                ;;
  4131. ;; in eax=61,ebx in [1..3]                                         ;;
  4132. ;; out eax                                                         ;;
  4133. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4134. iglobal
  4135. align 4
  4136. f61call:
  4137.            dd sys_gs.1   ; resolution
  4138.            dd sys_gs.2   ; bits per pixel
  4139.            dd sys_gs.3   ; bytes per scanline
  4140. endg
  4141.  
  4142.  
  4143. align 4
  4144.  
  4145. sys_gs:                         ; direct screen access
  4146.         dec     ebx
  4147.         cmp     ebx, 2
  4148.         ja      .not_support
  4149.         jmp     dword [f61call + ebx*4]
  4150. .not_support:
  4151.         or      [esp + SYSCALL_STACK.eax], dword -1
  4152.         ret
  4153.  
  4154.  
  4155. .1:                             ; resolution
  4156.         mov     eax, [_display.width]
  4157.         shl     eax, 16
  4158.         mov     ax, word [_display.height]
  4159.         mov     [esp + SYSCALL_STACK.eax], eax
  4160.         ret
  4161. .2:                             ; bits per pixel
  4162.         mov     eax, [_display.bits_per_pixel]
  4163.         mov     [esp + SYSCALL_STACK.eax], eax
  4164.         ret
  4165. .3:                             ; bytes per scanline
  4166.         mov     eax, [_display.lfb_pitch]
  4167.         mov     [esp + SYSCALL_STACK.eax], eax
  4168.         ret
  4169.  
  4170. align 4
  4171. syscall_getscreensize:                  ; GetScreenSize
  4172.         mov     ax, word [_display.width]
  4173.         dec     ax
  4174.         shl     eax, 16
  4175.         mov     ax, word [_display.height]
  4176.         dec     ax
  4177.         mov     [esp + SYSCALL_STACK.eax], eax
  4178.         ret
  4179. ;-----------------------------------------------------------------------------
  4180. align 4
  4181. syscall_cdaudio:
  4182. ; ECX - position of CD/DVD-drive
  4183. ; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
  4184. ; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
  4185. ; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
  4186.         cmp     ecx, 11
  4187.         ja      .exit
  4188.  
  4189.         mov     eax, ecx
  4190.         shr     eax, 2
  4191.         lea     eax, [eax*5]
  4192.         mov     al, [eax + DRIVE_DATA + 1]
  4193.  
  4194.         push    ecx ebx
  4195.         mov     ebx, ecx
  4196.         and     ebx, 11b
  4197.         shl     ebx, 1
  4198.         mov     cl, 6
  4199.         sub     cl, bl
  4200.         shr     al, cl
  4201.         test    al, 2 ; it's not an ATAPI device
  4202.         pop     ebx ecx
  4203.  
  4204.         jz      .exit
  4205.  
  4206.         cmp     ebx, 4
  4207.         je      .eject
  4208.  
  4209.         cmp     ebx, 5
  4210.         je      .load
  4211. ;--------------------------------------
  4212. .exit:
  4213.         ret
  4214. ;--------------------------------------
  4215. .load:
  4216.         call    .reserve
  4217.         call    LoadMedium
  4218.         jmp     .free
  4219. ;--------------------------------------
  4220. .eject:
  4221.         call    .reserve
  4222.         call    clear_CD_cache
  4223.         call    allow_medium_removal
  4224.         call    EjectMedium
  4225.         jmp     .free
  4226. ;--------------------------------------
  4227. .reserve:
  4228.         call    reserve_cd
  4229.  
  4230.         mov     ebx, ecx
  4231.         inc     ebx
  4232.         mov     [cdpos], ebx
  4233.  
  4234.         mov     eax, ecx
  4235.         shr     eax, 1
  4236.         and     eax, 1
  4237.         inc     eax
  4238.         mov     [ChannelNumber], al
  4239.         mov     eax, ecx
  4240.         and     eax, 1
  4241.         mov     [DiskNumber], al
  4242.         call    reserve_cd_channel
  4243.         ret
  4244. ;--------------------------------------
  4245. .free:
  4246.         call    free_cd_channel
  4247.         and     [cd_status], 0
  4248.         ret
  4249. ;-----------------------------------------------------------------------------
  4250. align 4
  4251. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  4252.         cmp     ebx, [_display.width]
  4253.         jb      @f
  4254.         cmp     ecx, [_display.height]
  4255.         jb      @f
  4256.         xor     eax, eax
  4257.         jmp     .store
  4258. ;--------------------------------------
  4259. align 4
  4260. @@:
  4261.         mov     eax, [d_width_calc_area + ecx*4]
  4262.         add     eax, [_display.win_map]
  4263.         movzx   eax, byte[eax+ebx]        ; get value for current point
  4264. ;--------------------------------------
  4265. align 4
  4266. .store:
  4267.         mov     [esp + SYSCALL_STACK.eax], eax
  4268.         ret
  4269. ;-----------------------------------------------------------------------------
  4270. align 4
  4271. syscall_getpixel:                       ; GetPixel
  4272.         mov     ecx, [_display.width]
  4273.         xor     edx, edx
  4274.         mov     eax, ebx
  4275.         div     ecx
  4276.         mov     ebx, edx
  4277.         xchg    eax, ebx
  4278.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4279.         call    dword [GETPIXEL]; eax - x, ebx - y
  4280.         mov     [esp + SYSCALL_STACK.eax], ecx
  4281.         ret
  4282. ;-----------------------------------------------------------------------------
  4283. align 4
  4284. syscall_getarea:
  4285. ;eax = 36
  4286. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4287. ;ecx = [size x]*65536 + [size y]
  4288. ;edx = [start x]*65536 + [start y]
  4289.         pushad
  4290.         mov     edi, ebx
  4291.         mov     eax, edx
  4292.         shr     eax, 16
  4293.         mov     ebx, edx
  4294.         and     ebx, 0xffff
  4295.         dec     eax
  4296.         dec     ebx
  4297.      ; eax - x, ebx - y
  4298.         mov     edx, ecx
  4299.  
  4300.         shr     ecx, 16
  4301.         and     edx, 0xffff
  4302.         mov     esi, ecx
  4303.      ; ecx - size x, edx - size y
  4304.  
  4305.         mov     ebp, edx
  4306.         lea     ebp, [ebp*3]
  4307.         imul    ebp, esi
  4308.         stdcall is_region_userspace, edi, ebp
  4309.         jnz     .exit
  4310.  
  4311.         mov     ebp, edx
  4312.         dec     ebp
  4313.         lea     ebp, [ebp*3]
  4314.  
  4315.         imul    ebp, esi
  4316.  
  4317.         mov     esi, ecx
  4318.         dec     esi
  4319.         lea     esi, [esi*3]
  4320.  
  4321.         add     ebp, esi
  4322.         add     ebp, edi
  4323.  
  4324.         add     ebx, edx
  4325. ;--------------------------------------
  4326. align 4
  4327. .start_y:
  4328.         push    ecx edx
  4329. ;--------------------------------------
  4330. align 4
  4331. .start_x:
  4332.         push    eax ebx ecx
  4333.         add     eax, ecx
  4334.  
  4335.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4336.         call    dword [GETPIXEL]; eax - x, ebx - y
  4337.  
  4338.         mov     [ebp], cx
  4339.         shr     ecx, 16
  4340.         mov     [ebp+2], cl
  4341.  
  4342.         pop     ecx ebx eax
  4343.         sub     ebp, 3
  4344.         dec     ecx
  4345.         jnz     .start_x
  4346.         pop     edx ecx
  4347.         dec     ebx
  4348.         dec     edx
  4349.         jnz     .start_y
  4350.        
  4351. .exit:
  4352.         popad
  4353.         ret
  4354. ;-----------------------------------------------------------------------------
  4355.  
  4356. align 4
  4357. syscall_threads:                        ; CreateThreads
  4358. ;
  4359. ;   ecx=thread entry point
  4360. ;   edx=thread stack pointer
  4361. ;
  4362. ; on return : eax = pid
  4363.         xor     ebx, ebx
  4364.         call    new_sys_threads
  4365.  
  4366.         mov     [esp + SYSCALL_STACK.eax], eax
  4367.         ret
  4368.  
  4369. ;------------------------------------------------------------------------------
  4370. align 4
  4371. calculate_fast_getting_offset_for_WinMapAddress:
  4372. ; calculate data area for fast getting offset to _WinMapAddress
  4373.         xor     eax, eax
  4374.         mov     ecx, [_display.height]
  4375.         mov     edi, d_width_calc_area
  4376.         cld
  4377. @@:
  4378.         stosd
  4379.         add     eax, [_display.width]
  4380.         dec     ecx
  4381.         jnz     @r
  4382.         ret
  4383. ;------------------------------------------------------------------------------
  4384. align 4
  4385. calculate_fast_getting_offset_for_LFB:
  4386. ; calculate data area for fast getting offset to LFB
  4387.         xor     eax, eax
  4388.         mov     ecx, [_display.height]
  4389.         mov     edi, BPSLine_calc_area
  4390.         cld
  4391. @@:
  4392.         stosd
  4393.         add     eax, [_display.lfb_pitch]
  4394.         dec     ecx
  4395.         jnz     @r
  4396.         ret
  4397. ;------------------------------------------------------------------------------
  4398. align 4
  4399. set_screen:
  4400. ; in:
  4401. ; eax - new Screen_Max_X
  4402. ; ecx - new BytesPerScanLine
  4403. ; edx - new Screen_Max_Y
  4404.  
  4405.         pushfd
  4406.         cli
  4407.  
  4408.         mov     [_display.lfb_pitch], ecx
  4409.  
  4410.         mov     [screen_workarea.right], eax
  4411.         mov     [screen_workarea.bottom], edx
  4412.  
  4413.         push    ebx
  4414.         push    esi
  4415.         push    edi
  4416.  
  4417.         pushad
  4418.  
  4419.         cmp     [do_not_touch_winmap], 1
  4420.         je      @f
  4421.  
  4422.         stdcall kernel_free, [_display.win_map]
  4423.  
  4424.         mov     eax, [_display.width]
  4425.         mul     [_display.height]
  4426.         mov     [_display.win_map_size], eax
  4427.  
  4428.         stdcall kernel_alloc, eax
  4429.         mov     [_display.win_map], eax
  4430.         test    eax, eax
  4431.         jz      .epic_fail
  4432. ; store for f.18.24
  4433.         mov     eax, [_display.width]
  4434.         mov     [display_width_standard], eax
  4435.  
  4436.         mov     eax, [_display.height]
  4437.         mov     [display_height_standard], eax
  4438. @@:
  4439.         call    calculate_fast_getting_offset_for_WinMapAddress
  4440. ; for Qemu or non standart video cards
  4441. ; Unfortunately [BytesPerScanLine] does not always
  4442. ;                             equal to [_display.width] * [ScreenBPP] / 8
  4443.         call    calculate_fast_getting_offset_for_LFB
  4444.         popad
  4445.  
  4446.         call    repos_windows
  4447.         xor     eax, eax
  4448.         xor     ebx, ebx
  4449.         mov     ecx, [_display.width]
  4450.         mov     edx, [_display.height]
  4451.         dec     ecx
  4452.         dec     edx
  4453.         call    calculatescreen
  4454.         pop     edi
  4455.         pop     esi
  4456.         pop     ebx
  4457.  
  4458.         popfd
  4459.         ret
  4460.  
  4461. .epic_fail:
  4462.         hlt                     ; Houston, we've had a problem
  4463.  
  4464. ; --------------- APM ---------------------
  4465. uglobal
  4466. apm_entry       dp      0
  4467. apm_vf          dd      0
  4468. endg
  4469.  
  4470. align 4
  4471. sys_apm:
  4472.         xor     eax, eax
  4473.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4474.         jne     @f
  4475.         inc     eax
  4476.         or      dword [esp + 44], eax   ; error
  4477.         add     eax, 7
  4478.         mov     dword [esp + SYSCALL_STACK.eax], eax   ; 32-bit protected-mode interface not supported
  4479.         ret
  4480.  
  4481. @@:
  4482. ;       xchg    eax, ecx
  4483. ;       xchg    ebx, ecx
  4484.  
  4485.         cmp     dx, 3
  4486.         ja      @f
  4487.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4488.         mov     eax, [apm_vf]
  4489.         mov     [esp + SYSCALL_STACK.eax], eax
  4490.         shr     eax, 16
  4491.         mov     [esp + SYSCALL_STACK.ecx], eax
  4492.         ret
  4493.  
  4494. @@:
  4495.  
  4496.         mov     esi, [master_tab+(OS_BASE shr 20)]
  4497.         xchg    [master_tab], esi
  4498.         push    esi
  4499.         mov     edi, cr3
  4500.         mov     cr3, edi                ;flush TLB
  4501.  
  4502.         call    pword [apm_entry]       ;call APM BIOS
  4503.  
  4504.         xchg    eax, [esp]
  4505.         mov     [master_tab], eax
  4506.         mov     eax, cr3
  4507.         mov     cr3, eax
  4508.         pop     eax
  4509.  
  4510.         mov     [esp + SYSCALL_STACK.edi], edi
  4511.         mov     [esp + SYSCALL_STACK.esi], esi
  4512.         mov     [esp + SYSCALL_STACK.ebx], ebx
  4513.         mov     [esp + SYSCALL_STACK.edx], edx
  4514.         mov     [esp + SYSCALL_STACK.ecx], ecx
  4515.         mov     [esp + SYSCALL_STACK.eax], eax
  4516.         setc    al
  4517.         and     [esp + 44], byte 0xfe
  4518.         or      [esp + 44], al
  4519.         ret
  4520. ; -----------------------------------------
  4521.  
  4522. align 4
  4523. undefined_syscall:                      ; Undefined system call
  4524.         mov     [esp + SYSCALL_STACK.eax], dword -1
  4525.         ret
  4526.  
  4527. align 4
  4528. ; @brief Check if given memory region lays in lower 2gb (userspace memory) or not
  4529. ; @param base Base address of region
  4530. ; @param len Lenght of region
  4531. ; @return ZF = 1 if region in userspace memory,
  4532. ;         ZF = 0 otherwise
  4533. proc is_region_userspace stdcall, base:dword, len:dword
  4534.         push    eax
  4535.         mov     eax, [base]
  4536.  
  4537.         cmp     eax, OS_BASE-1
  4538.         ja      @f              ; zf
  4539.  
  4540.         add     eax, [len]
  4541.         jc      @f              ; zf
  4542.         cmp     eax, OS_BASE
  4543.         ja      @f              ; zf
  4544.  
  4545.         cmp     eax, eax        ; ZF
  4546. @@:
  4547.         pop     eax
  4548.         ret
  4549. endp
  4550.  
  4551. align 4
  4552. ; @brief Check whether given string lays in userspace memory, i.e. below OS_BASE
  4553. ; @param base Base address of string
  4554. ; @return ZF = 1 if string in userspace memory,
  4555. ;         zf = 0 otherwise
  4556. proc is_string_userspace stdcall, base:dword
  4557.         push    eax ecx edi
  4558.         xor     eax, eax
  4559.         mov     edi, [base]
  4560.  
  4561.         mov     ecx, OS_BASE-1
  4562.         sub     ecx, edi
  4563.         jb      .done           ; zf
  4564.         inc     ecx
  4565.         cmp     ecx, 0x10000    ; don't allow strings larger than 64k?
  4566.         jbe     @f
  4567.         mov     ecx, 0x10000
  4568. @@:
  4569.         repnz scasb
  4570. .done:
  4571.         pop     edi ecx eax
  4572.         ret
  4573. endp
  4574.  
  4575. if ~ lang eq sp
  4576. diff16 "end of .text segment",0,$
  4577. end if
  4578.  
  4579. include "data32.inc"
  4580.  
  4581. __REV__ = __REV
  4582.  
  4583. if ~ lang eq sp
  4584. diff16 "end of kernel code",0,$
  4585. end if
  4586.