Subversion Repositories Kolibri OS

Rev

Rev 427 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. init_fpu:
  9.            clts
  10.            fninit
  11.  
  12.            bt [cpu_caps], CAPS_SSE
  13.            jnc .no_SSE
  14.  
  15.            mov ebx, cr4
  16.            mov ecx, cr0
  17.            or ebx, CR4_OSFXSR+CR4_OSXMMEXPT
  18.            mov cr4, ebx
  19.  
  20.            and ecx, not (CR0_MP+CR0_EM)
  21.            or ecx, CR0_NE
  22.            mov cr0, ecx
  23.  
  24.            mov dword [esp-4], SSE_INIT
  25.            ldmxcsr [esp-4]
  26.  
  27.            xorps xmm0, xmm0
  28.            xorps xmm1, xmm1
  29.            xorps xmm2, xmm2
  30.            xorps xmm3, xmm3
  31.            xorps xmm4, xmm4
  32.            xorps xmm5, xmm5
  33.            xorps xmm6, xmm6
  34.            xorps xmm7, xmm7
  35.            fxsave [fpu_data]  ;[eax]
  36.            ret
  37. .no_SSE:
  38.            mov ecx, cr0
  39.            and ecx, not CR0_EM
  40.            or ecx, CR0_MP+CR0_NE
  41.            mov cr0, ecx
  42.            fnsave [fpu_data]
  43.            ret
  44.  
  45. ; param
  46. ;  eax= 512 bytes memory area
  47.  
  48. align 4
  49. fpu_save:
  50.            push ecx
  51.            push esi
  52.            push edi
  53.  
  54.            pushfd
  55.            cli
  56.  
  57.            clts
  58.            mov edi, eax
  59.  
  60.            mov ecx, [fpu_owner]
  61.            mov esi, [CURRENT_TASK]
  62.            cmp ecx, esi
  63.            jne .save
  64.  
  65.            call save_context
  66.            jmp .exit
  67. .save:
  68.            mov [fpu_owner], esi
  69.  
  70.            shl ecx, 8
  71.            mov eax, [ecx+SLOT_BASE+APPDATA.fpu_state]
  72.  
  73.            call save_context
  74.  
  75.            shl esi, 8
  76.            mov esi, [esi+SLOT_BASE+APPDATA.fpu_state]
  77.            mov ecx, 512/4
  78.            cld
  79.            rep movsd
  80.            fninit
  81. .exit:
  82.            popfd
  83.            pop edi
  84.            pop esi
  85.            pop ecx
  86.            ret
  87.  
  88. align 4
  89. save_context:
  90.            bt [cpu_caps], CAPS_SSE
  91.            jnc .no_SSE
  92.  
  93.            fxsave [eax]
  94.            ret
  95. .no_SSE:
  96.            fnsave [eax]
  97.            ret
  98.  
  99. align 4
  100. fpu_restore:
  101.            push ecx
  102.            push esi
  103.  
  104.            mov esi, eax
  105.  
  106.            pushfd
  107.            cli
  108.  
  109.            mov ecx, [fpu_owner]
  110.            mov eax, [CURRENT_TASK]
  111.            cmp ecx, eax
  112.            jne .copy
  113.  
  114.            clts
  115.            bt [cpu_caps], CAPS_SSE
  116.            jnc .no_SSE
  117.  
  118.            fxrstor [esi]
  119.            popfd
  120.            pop esi
  121.            pop ecx
  122.            ret
  123. .no_SSE:
  124.            fnclex               ;fix possible problems
  125.            frstor [esi]
  126.            popfd
  127.            pop esi
  128.            pop ecx
  129.            ret
  130. .copy:
  131.            shl eax, 8
  132.            mov edi, [eax+SLOT_BASE+APPDATA.fpu_state]
  133.            mov ecx, 512/4
  134.            cld
  135.            rep movsd
  136.            popfd
  137.            pop esi
  138.            pop ecx
  139.            ret
  140.  
  141. align 4
  142. e7:                        ;#NM exception handler
  143.         save_ring3_context
  144.         clts
  145.         mov ax, app_data  ;
  146.         mov ds, ax
  147.         mov es, ax
  148.  
  149.         mov ebx, [fpu_owner]
  150.         cmp ebx, [CURRENT_TASK]
  151.         je .exit
  152.  
  153.         shl ebx, 8
  154.         mov eax, [ebx+SLOT_BASE+APPDATA.fpu_state]
  155.         bt [cpu_caps], CAPS_SSE
  156.         jnc .no_SSE
  157.  
  158.         fxsave [eax]
  159.         mov ebx, [CURRENT_TASK]
  160.         mov [fpu_owner], ebx
  161.         shl ebx, 8
  162.         mov eax, [ebx+SLOT_BASE+APPDATA.fpu_state]
  163.         fxrstor [eax]
  164. .exit:
  165.         restore_ring3_context
  166.         iret
  167.  
  168. .no_SSE:
  169.         fnsave [eax]
  170.         mov ebx, [CURRENT_TASK]
  171.         mov [fpu_owner], ebx
  172.         shl ebx, 8
  173.         mov eax, [ebx+SLOT_BASE+APPDATA.fpu_state]
  174.         frstor [eax]
  175.         restore_ring3_context
  176.         iret
  177.  
  178. iglobal
  179.   fpu_owner dd 1
  180.  endg
  181.  
  182. reg_eip       equ ebp+4
  183. reg_cs        equ ebp+8
  184. reg_eflags    equ ebp+12
  185. reg_esp       equ ebp+16
  186. reg_ss        equ ebp+20
  187.  
  188. align 4
  189. except_16:           ;fpu native exceptions handler
  190.            push ebp
  191.            mov ebp, esp
  192.  
  193.            push eax
  194.            push ebx
  195.            push ecx
  196.            push edx
  197.  
  198.            mov ebx, [CURRENT_TASK]
  199.            shl ebx, 8
  200.  
  201.            mov eax, [ebx+SLOT_BASE+APPDATA.fpu_handler]
  202.            test eax, eax
  203.            jz .default
  204.  
  205.            mov ecx, [reg_eip]
  206.            mov edx, [reg_esp]
  207.            sub edx, 4
  208.            mov [edx], ecx
  209.            mov [reg_esp], edx
  210.            mov dword [reg_eip], eax
  211.  
  212.            pop edx
  213.            pop ecx
  214.            pop ebx
  215.            pop eax
  216.  
  217.            leave
  218.            iretd
  219.  
  220. .default:
  221.            pop edx
  222.            pop ecx
  223.            pop ebx
  224.            pop eax
  225.            leave
  226.  
  227.            save_ring3_context     ;debugger support
  228.  
  229.            mov bl, 16
  230.            jmp exc_c
  231.  
  232. align 4
  233. except_19:          ;sse exceptions handler
  234.            push ebp
  235.            mov ebp, esp
  236.  
  237.            push eax
  238.            push ebx
  239.            push ecx
  240.            push edx
  241.  
  242.            mov ebx, [current_slot]
  243.  
  244.            mov eax, [ebx+APPDATA.sse_handler]
  245.            test eax, eax
  246.            jz .default
  247.  
  248.            mov ecx, [reg_eip]
  249.            mov edx, [reg_esp]
  250.            sub edx, 4
  251.            mov [edx], ecx
  252.            mov [reg_esp], edx
  253.            mov dword [reg_eip], eax
  254.  
  255.            pop edx
  256.            pop ecx
  257.            pop ebx
  258.            pop eax
  259.  
  260.            leave
  261.            iretd
  262.  
  263. .default:
  264.            pop edx
  265.            pop ecx
  266.            pop ebx
  267.            pop eax
  268.            leave
  269.  
  270.            save_ring3_context     ;debugger support
  271.  
  272.            mov bl, 19
  273.            jmp exc_c
  274.  
  275. restore reg_eip
  276. restore reg_cs
  277. restore reg_eflags
  278. restore reg_esp
  279. restore reg_ss
  280.  
  281.  
  282.