This is an archive of the discontinued LLVM Phabricator instance.

Add implementations of __readfs{byte,word,dword,qword} to Intrin.h
ClosedPublic

Authored by hans on Jan 23 2014, 9:17 AM.

Details

Summary

Please take a look. I always worry about getting the mov operands backwards :)

Diff Detail

Event Timeline

rnk accepted this revision.Jan 23 2014, 10:07 AM

LGTM

lib/Headers/Intrin.h
777

I would use offset instead of addr.

779

Can you add explicit size suffixes so we get better error checking here?

I think you can use movb, movw, movl, and movq.

779

I spent a while fooling around with multi-alternative constraints trying to get clang to produce 'movb %fs:0, %reg' for __readfsbyte(0), but I gave up. Our inline asm stuff isn't sophisticated enough to handle that.

hans added inline comments.Jan 23 2014, 10:37 AM
lib/Headers/Intrin.h
777

Done.

779

That's a good idea. Added the suffixes.

hans updated this revision to Unknown Object (????).Jan 23 2014, 10:38 AM

Addressed rnk's comments.

Chandler: can you provide a pointer to the address space technique you mentioned?

rnk added a comment.Jan 23 2014, 11:03 AM

This is the address space extension for accessing TLS:
http://clang.llvm.org/docs/LanguageExtensions.html#x86-x86-64-language-extensions

That's a lot cleaner. Let's do that.

hans updated this revision to Unknown Object (????).Jan 23 2014, 11:20 AM

Uploading patch that uses the address_space attribute instead of inline asm.

Please take another look.

rnk added inline comments.Jan 23 2014, 12:09 PM
lib/Headers/Intrin.h
779

We may want to move to macro-stamping these out if we add incfs*, addfs*, and __*gs*.

779

__offset isn't pointer sized in x64 mode, so we'll get -Wsystem-header warnings here. The API is specified to use unsigned long, since I believe you can't address more than 2 GB off of these segments. I'm not sure what integer type we can use to cast, though...

hans updated this revision to Unknown Object (????).Jan 23 2014, 2:01 PM

Thanks for the comments! Here's a new version with all the bells and whistles:

  • volatile ptr, as per David's request
  • using a macro
  • casting __offset to size_t to make sure it's pointer-sized
  • prefix address_space with double underscores to avoid name conflicts.
cdavis5x added inline comments.Jan 23 2014, 3:49 PM
lib/Headers/Intrin.h
781–796

In MSVC, these intrinsics actually aren't available on x64. (They're not needed anyway on Windows/x64, because it uses GS for the TEB instead of FS as Windows/i386 does.) For this reason (unless you have some pressing need for FS-relative access on a non-Windows x86-64 platform), you might consider putting them inside an #ifndef __x86_64__ block.

hans updated this revision to Unknown Object (????).Jan 23 2014, 4:29 PM

Addressing Charles's comment.

rnk added a comment.Jan 23 2014, 4:31 PM

lgtm, ship it. :)

hans closed this revision.Jan 23 2014, 4:58 PM

Closed by commit rL199958 (authored by @hans).