Subversion Repositories Kolibri OS

Rev

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

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