Subversion Repositories Kolibri OS

Rev

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