Fix for OpenCL crash with GK110B based cards

Discussion in 'Graphics' started by MacAndor, Jan 22, 2014.

  1. MacAndor

    MacAndor

    Joined:
    Feb 5, 2011
    Messages:
    174
    Mobo:
    Gigabyte GA-Z77M-D3H
    CPU:
    Intel Core i7-3770
    Graphics:
    Gigabyte GV-N780OC-3GD (rev. 2.0)
    Mac:
    MacBook Pro
    Mobile Phone:
    Android, iOS
    Jan 22, 2014 #1
    MacAndor

    MacAndor

    Joined:
    Feb 5, 2011
    Messages:
    174
    Mobo:
    Gigabyte GA-Z77M-D3H
    CPU:
    Intel Core i7-3770
    Graphics:
    Gigabyte GV-N780OC-3GD (rev. 2.0)
    Mac:
    MacBook Pro
    Mobile Phone:
    Android, iOS
    So...

    You're probably here because you have a GK110B based GPU which crashes with all OpenCL apps. I managed to stop the crashes and here's a guide for y'all.

    THIS WILL STOP THE CRASHES, DOES NOT ADD THE PROPER SUPPORT!

    AFTER UPDATING THE OS, RE-CREATE YOUR OPENCL BACKUP!

    First of all:
    • USE AT OWN RISK, I AM NOT RESPONSIBLE FOR DAMAGE, BLABLABLA (you know the deal...)
    • TESTED ON 10.9.0, 10.9.1, 10.9.2, 10.9.3, 10.9.4, 10.9.5
    • DON'T FORGET TO BACKUP THE FRAMEWORK BEFORE MODIFYING

    What I did:
    • Disabled GPU OpenCL support (FOR ALL GPUs).
    • CPU OpenCL is still supported (NOT IGPU)
    • CUDA is still supported

    What you need:
    • Xcode (for codesigning)
    • A hex editor (I'll be using Hex Fiend)

    Creating a backup:
    There are multiple ways of doing this, but this is what I usually do.

    • Open Terminal
    Code (Text):
    sudo -s
    <enter password>
    cd /System/Library/Frameworks/OpenCL.framework/Versions/A/
    cp OpenCL OpenCL.bkp
    Now you will have the original version as "OpenCL.bkp" next to the modded version
    Caution: after updating the OS re-create the backup because it will be the backup of the older version, and you don’t really want to revert to that.

    The Guide:
    1. Create a Certificate for codesigning (if you don't have one already)
      1. Open Keychain Access
      2. Go to Keychain Access > Certificate Assistant > Create a Certificate...
        • Give it a name like "OpenCLFWKCS" (I will use this name later)
        • Identity Type: Self Signed Root
        • Certificate Type: Code Signing
        • Create
    2. Copy the file somewhere where you can edit it (for example to your desktop with Terminal)
      Code (Text):
      cp /System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL ~/Desktop/OpenCL
    3. Open the editable copy of your OpenCL file in a hex editor
      • For 10.9.0 and 10.9.1
        WARNING: THERE SHOULD ONLY BE ONE MATCH
        Code (Text):
        64-bit
        Replace: E83DE10100F6C3057413
           With: E83DE10100F6C3007413
        32-bit
        Replace: E81EF901008B4508A8057418
           With: E81EF901008B4508A8007418
      • For 10.9.2
        WARNING: THERE SHOULD ONLY BE ONE MATCH
        Code (Text):
        64-bit
        Replace: E831E30100F6C3057413
           With: E831E30100F6C3007413
        32-bit
        Replace: 8B4508A80574188D8638010000
           With: 8B4508A80074188D8638010000
      • For 10.9.3 and 10.9.4
        WARNING: THERE SHOULD ONLY BE ONE MATCH
        Code (Text):
        64-bit
        Replace: E8DFE30100F6C3057413
           With: E8DFE30100F6C3007413
        32-bit
        Replace: 8B4508A80574188D8638010000
           With: 8B4508A80074188D8638010000
      • For 10.9.5
        WARNING: THERE SHOULD ONLY BE ONE MATCH
        Code (Text):
        64-bit
        Replace: E8CBE30100F6C3057413
           With: E8CBE30100F6C3007413
        32-bit
        Replace: E892FB01008B4508A8057418
           With: E892FB01008B4508A8007418
    4. Copy back the edited version of the OpenCL file to the framework (with Terminal)
      Code (Text):
      sudo cp ~/Desktop/OpenCL /System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL
    5. Resign the OpenCL framework
      Code (Text):
      sudo codesign -fs "OpenCLFWKCS" [u]/System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL[/u]
    6. Restart

    How to revert:
    This applies when the backup method was the one mentioned above.

    • Boot chameleon with -s
    Code (Text):
    /sbin/mount -uw /
    cd /System/Library/Frameworks/OpenCL.framework/Versions/A/
    cp OpenCL.bkp OpenCL
    Why I won't share the binary:
    Because I codesigned it :) sorry

    Caution:
    • Do not share the modified executable
    • Do not share the created certificate

    Tested with:
    Preview (jpg, png), Xcode (assistant view), LuxMark, Safari, Premiere Pro CC (trial) and others

    Fast walkthrough on what I modified:
    When an app is using the OpenCL library nothing special happens until the library gets the first call. When this happens, OpenCL initialises itself.
    This is in the /System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL file at offset <0x21734> (in 10.9.0 and 10.9.1). (If you wan’t to tag along, then you probably need a disassembler)
    This initialisation block contains three pthread_once calls for three different functions.
    • The first probably does some basic initialisation
    • The second initialises CPU support
    • The third initialises GPU support - this is what we don’t want
    Lucky for us, this is in an "if" block, which is something like this:
    Code (Text):
    if (x & 5) { pthread_once(...); }
    For non-programmers x & 5 is a bitwise AND operation (http://en.wikipedia.org/wiki/Bitwise_operation#AND)
    If you bitwise AND any value with zero, the result will be zero.​

    If we don’t want it to call pthread_once, we only need to change the condition to this:
    Code (Text):
    if (x & 0) { pthread_once(...); }
    There is no way it will call it now :)
    If you look at the patch, this is exactly what I did, the only real difference in the search hex and the replace hex is a 5->0 change.

    Updates:
    - Added 32 & 64-bit patches for 10.9.5
    - Added 32 & 64-bit patches for 10.9.4
    - Added 32 & 64-bit patches for 10.9.3, thanks to @MyAss
    - Added 32 & 64-bit patches for 10.9.2, guide is more verbose
    - Added 32-bit patch for 10.9.0 and 10.9.1
     
  2. genzai

    genzai

    Joined:
    Aug 19, 2010
    Messages:
    476
    Mobo:
    GA-Z77X-UP5 TH
    CPU:
    3770K
    Graphics:
    HD4000 & GTX 770
    Mac:
    MacBook Air, MacBook Pro, Mac mini, Mac Pro
    Mobile Phone:
    iOS
    Jan 23, 2014 #2
    genzai

    genzai

    Joined:
    Aug 19, 2010
    Messages:
    476
    Mobo:
    GA-Z77X-UP5 TH
    CPU:
    3770K
    Graphics:
    HD4000 & GTX 770
    Mac:
    MacBook Air, MacBook Pro, Mac mini, Mac Pro
    Mobile Phone:
    iOS
    Thank you. Very clear. very straightforward. I am interested how you found that bit of binary mod goodness :)

    One question... Why caution against sharing the modified (unsigned) executable? then people could just sign it themselves and avoid the hex editing if they wanted.

    Thanks,
    g\
     
  3. MacAndor

    MacAndor

    Joined:
    Feb 5, 2011
    Messages:
    174
    Mobo:
    Gigabyte GA-Z77M-D3H
    CPU:
    Intel Core i7-3770
    Graphics:
    Gigabyte GV-N780OC-3GD (rev. 2.0)
    Mac:
    MacBook Pro
    Mobile Phone:
    Android, iOS
    Jan 23, 2014 #3
    MacAndor

    MacAndor

    Joined:
    Feb 5, 2011
    Messages:
    174
    Mobo:
    Gigabyte GA-Z77M-D3H
    CPU:
    Intel Core i7-3770
    Graphics:
    Gigabyte GV-N780OC-3GD (rev. 2.0)
    Mac:
    MacBook Pro
    Mobile Phone:
    Android, iOS
    It took me a few days to check how the system handles OpenCL. This took a considerable amount of disassembling (drivers, graphics libraries) and reading all that. Thankfully I have experience with arm, x86 and x86-64 assembly code, so I could understand it fairly easily. After understanding the workings of it, it was only a question of where the modification would be the best.

    I already made a walkthrough but I didn't publish it because I'm not sure if that complies with tonymac forum rules, so I sent them a question whether it's ok or not.

    On the caution part, the OpenCL framework is not part of Apple's open source packages. Thus distributing the executable is probably not legal. There's a fair chance that no one in the world would give a sh*t, but in my opinion, better be safe then sorry.
     
  4. tonymacx86

    tonymacx86 Administrator Staff Member

    Joined:
    Nov 18, 2009
    Messages:
    7,841
    Mobo:
    GA-Z170X-UD5-TH
    CPU:
    Core i5-6600K
    Graphics:
    HD 530 / GT 740
    Mac:
    MacBook Pro, Mac mini
    Classic Mac:
    Apple, PowerBook
    Mobile Phone:
    iOS
    Jan 23, 2014 #4
    tonymacx86

    tonymacx86 Administrator Staff Member

    Joined:
    Nov 18, 2009
    Messages:
    7,841
    Mobo:
    GA-Z170X-UD5-TH
    CPU:
    Core i5-6600K
    Graphics:
    HD 530 / GT 740
    Mac:
    MacBook Pro, Mac mini
    Classic Mac:
    Apple, PowerBook
    Mobile Phone:
    iOS
    Good work! Sure- go ahead and post the experimental files and guide for people to test. Unfortunately I dont have a 780 or 780 Ti to test, but I'll be following along!

    :mrgreen:
     
  5. MacAndor

    MacAndor

    Joined:
    Feb 5, 2011
    Messages:
    174
    Mobo:
    Gigabyte GA-Z77M-D3H
    CPU:
    Intel Core i7-3770
    Graphics:
    Gigabyte GV-N780OC-3GD (rev. 2.0)
    Mac:
    MacBook Pro
    Mobile Phone:
    Android, iOS
    Jan 23, 2014 #5
    MacAndor

    MacAndor

    Joined:
    Feb 5, 2011
    Messages:
    174
    Mobo:
    Gigabyte GA-Z77M-D3H
    CPU:
    Intel Core i7-3770
    Graphics:
    Gigabyte GV-N780OC-3GD (rev. 2.0)
    Mac:
    MacBook Pro
    Mobile Phone:
    Android, iOS
    Thanks! :) Updated the first post with small walkthrough.
     
  6. BaskoRazglas

    BaskoRazglas

    Joined:
    Sep 26, 2011
    Messages:
    17
    Mobo:
    Mavericks 10.9.1
    CPU:
    Intel Core i5-2500K
    Graphics:
    nVidia GTX 780Ti
    Mac:
    MacBook Air
    Mobile Phone:
    iOS
    Jan 23, 2014 #6
    BaskoRazglas

    BaskoRazglas

    Joined:
    Sep 26, 2011
    Messages:
    17
    Mobo:
    Mavericks 10.9.1
    CPU:
    Intel Core i5-2500K
    Graphics:
    nVidia GTX 780Ti
    Mac:
    MacBook Air
    Mobile Phone:
    iOS
    Awesome work, thanks a lot!

    Just a quick question, before I attempt this. Once NVIDIA, hopefully, sees the light, and implements OpenCL support for GK100b, what steps will I need to take make it fully GPU-accelerated again?

    And... can you please provide some info for us completely coding-illeterates. What does "backing up the framework" mean? I know that "if you don't know that, you probably shouldn't attempt this" but I'm without my OSX for a month now so I'm ready for an "all in" approach :D
     
  7. MacAndor

    MacAndor

    Joined:
    Feb 5, 2011
    Messages:
    174
    Mobo:
    Gigabyte GA-Z77M-D3H
    CPU:
    Intel Core i7-3770
    Graphics:
    Gigabyte GV-N780OC-3GD (rev. 2.0)
    Mac:
    MacBook Pro
    Mobile Phone:
    Android, iOS
    Jan 23, 2014 #7
    MacAndor

    MacAndor

    Joined:
    Feb 5, 2011
    Messages:
    174
    Mobo:
    Gigabyte GA-Z77M-D3H
    CPU:
    Intel Core i7-3770
    Graphics:
    Gigabyte GV-N780OC-3GD (rev. 2.0)
    Mac:
    MacBook Pro
    Mobile Phone:
    Android, iOS
    Updated first post.
    Making it fully GPU-accelerated again is the same as the "How to revert" part, but you don't need to boot chameleon with -s, you can simply open Terminal, type sudo -s and do the cd and cp commands (which are in the description).
     
  8. SquishyTia

    SquishyTia

    Joined:
    Jun 11, 2013
    Messages:
    57
    Mobo:
    Gigabyte GA-Z87X-UD5H
    CPU:
    Core i7-4770K
    Graphics:
    EVGA 03G-P4-3784-KR GeForce GTX 780
    Mac:
    Mac Pro
    Classic Mac:
    Power Mac, Quadra
    Mobile Phone:
    Android
    Jan 23, 2014 #8
    SquishyTia

    SquishyTia

    Joined:
    Jun 11, 2013
    Messages:
    57
    Mobo:
    Gigabyte GA-Z87X-UD5H
    CPU:
    Core i7-4770K
    Graphics:
    EVGA 03G-P4-3784-KR GeForce GTX 780
    Mac:
    Mac Pro
    Classic Mac:
    Power Mac, Quadra
    Mobile Phone:
    Android
    Thank you, MacAndor, for taking the time to find this workaround for us. I know it isn't "ideal", but it gets the job done and lets us have a relatively stable system as a result. Beggars can't be choosers. :)
     
  9. MacAndor

    MacAndor

    Joined:
    Feb 5, 2011
    Messages:
    174
    Mobo:
    Gigabyte GA-Z77M-D3H
    CPU:
    Intel Core i7-3770
    Graphics:
    Gigabyte GV-N780OC-3GD (rev. 2.0)
    Mac:
    MacBook Pro
    Mobile Phone:
    Android, iOS
    Jan 23, 2014 #9
    MacAndor

    MacAndor

    Joined:
    Feb 5, 2011
    Messages:
    174
    Mobo:
    Gigabyte GA-Z77M-D3H
    CPU:
    Intel Core i7-3770
    Graphics:
    Gigabyte GV-N780OC-3GD (rev. 2.0)
    Mac:
    MacBook Pro
    Mobile Phone:
    Android, iOS
    I'm happy this doesn't only solve my problem :)
     
  10. Crazylegsmurphy

    Crazylegsmurphy

    Joined:
    Aug 18, 2013
    Messages:
    154
    Jan 23, 2014 #10
    Crazylegsmurphy

    Crazylegsmurphy

    Joined:
    Aug 18, 2013
    Messages:
    154
    Awesome job!

    I do have a few questions before I move forward with this, if you don't mind. While I enjoy making my computer work, I also like learning as I go.

    1. What exactly is this doing, and why does this work?

    2. What, if any other issues could we see by doing this (will it cause other programs to stop working, ie. Adobe products)?

    3. Do we have to have developer status, or can anyone make this work?

    4. What are the risks (hardware/software) in doing this?

    Thanks for your time figuring this out, and for providing the info!
     

Share This Page