Initial import

Based on m5threads-tls_2009_01_28, with additional support for OpenMP.
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..cf9b6b9
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,510 @@
+
+                  GNU LESSER GENERAL PUBLIC LICENSE
+                       Version 2.1, February 1999
+
+ Copyright (C) 1991, 1999 Free Software Foundation, Inc.
+     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the Lesser GPL.  It also counts
+ as the successor of the GNU Library Public License, version 2, hence
+ the version number 2.1.]
+
+                            Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+  This license, the Lesser General Public License, applies to some
+specially designated software packages--typically libraries--of the
+Free Software Foundation and other authors who decide to use it.  You
+can use it too, but we suggest you first think carefully about whether
+this license or the ordinary General Public License is the better
+strategy to use in any particular case, based on the explanations
+below.
+
+  When we speak of free software, we are referring to freedom of use,
+not price.  Our General Public Licenses are designed to make sure that
+you have the freedom to distribute copies of free software (and charge
+for this service if you wish); that you receive source code or can get
+it if you want it; that you can change the software and use pieces of
+it in new free programs; and that you are informed that you can do
+these things.
+
+  To protect your rights, we need to make restrictions that forbid
+distributors to deny you these rights or to ask you to surrender these
+rights.  These restrictions translate to certain responsibilities for
+you if you distribute copies of the library or if you modify it.
+
+  For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you.  You must make sure that they, too, receive or can get the source
+code.  If you link other code with the library, you must provide
+complete object files to the recipients, so that they can relink them
+with the library after making changes to the library and recompiling
+it.  And you must show them these terms so they know their rights.
+
+  We protect your rights with a two-step method: (1) we copyright the
+library, and (2) we offer you this license, which gives you legal
+permission to copy, distribute and/or modify the library.
+
+  To protect each distributor, we want to make it very clear that
+there is no warranty for the free library.  Also, if the library is
+modified by someone else and passed on, the recipients should know
+that what they have is not the original version, so that the original
+author's reputation will not be affected by problems that might be
+introduced by others.
+^L
+  Finally, software patents pose a constant threat to the existence of
+any free program.  We wish to make sure that a company cannot
+effectively restrict the users of a free program by obtaining a
+restrictive license from a patent holder.  Therefore, we insist that
+any patent license obtained for a version of the library must be
+consistent with the full freedom of use specified in this license.
+
+  Most GNU software, including some libraries, is covered by the
+ordinary GNU General Public License.  This license, the GNU Lesser
+General Public License, applies to certain designated libraries, and
+is quite different from the ordinary General Public License.  We use
+this license for certain libraries in order to permit linking those
+libraries into non-free programs.
+
+  When a program is linked with a library, whether statically or using
+a shared library, the combination of the two is legally speaking a
+combined work, a derivative of the original library.  The ordinary
+General Public License therefore permits such linking only if the
+entire combination fits its criteria of freedom.  The Lesser General
+Public License permits more lax criteria for linking other code with
+the library.
+
+  We call this license the "Lesser" General Public License because it
+does Less to protect the user's freedom than the ordinary General
+Public License.  It also provides other free software developers Less
+of an advantage over competing non-free programs.  These disadvantages
+are the reason we use the ordinary General Public License for many
+libraries.  However, the Lesser license provides advantages in certain
+special circumstances.
+
+  For example, on rare occasions, there may be a special need to
+encourage the widest possible use of a certain library, so that it
+becomes a de-facto standard.  To achieve this, non-free programs must
+be allowed to use the library.  A more frequent case is that a free
+library does the same job as widely used non-free libraries.  In this
+case, there is little to gain by limiting the free library to free
+software only, so we use the Lesser General Public License.
+
+  In other cases, permission to use a particular library in non-free
+programs enables a greater number of people to use a large body of
+free software.  For example, permission to use the GNU C Library in
+non-free programs enables many more people to use the whole GNU
+operating system, as well as its variant, the GNU/Linux operating
+system.
+
+  Although the Lesser General Public License is Less protective of the
+users' freedom, it does ensure that the user of a program that is
+linked with the Library has the freedom and the wherewithal to run
+that program using a modified version of the Library.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.  Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library".  The
+former contains code derived from the library, whereas the latter must
+be combined with the library in order to run.
+^L
+                  GNU LESSER GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License Agreement applies to any software library or other
+program which contains a notice placed by the copyright holder or
+other authorized party saying it may be distributed under the terms of
+this Lesser General Public License (also called "this License").
+Each licensee is addressed as "you".
+
+  A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+  The "Library", below, refers to any such software library or work
+which has been distributed under these terms.  A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language.  (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+  "Source code" for a work means the preferred form of the work for
+making modifications to it.  For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control
+compilation and installation of the library.
+
+  Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it).  Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+
+  1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+  You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+
+  2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) The modified work must itself be a software library.
+
+    b) You must cause the files modified to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    c) You must cause the whole of the work to be licensed at no
+    charge to all third parties under the terms of this License.
+
+    d) If a facility in the modified Library refers to a function or a
+    table of data to be supplied by an application program that uses
+    the facility, other than as an argument passed when the facility
+    is invoked, then you must make a good faith effort to ensure that,
+    in the event an application does not supply such function or
+    table, the facility still operates, and performs whatever part of
+    its purpose remains meaningful.
+
+    (For example, a function in a library to compute square roots has
+    a purpose that is entirely well-defined independent of the
+    application.  Therefore, Subsection 2d requires that any
+    application-supplied function or table used by this function must
+    be optional: if the application does not supply it, the square
+    root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library.  To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License.  (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.)  Do not make any other change in
+these notices.
+^L
+  Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+  This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+  4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+  If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library".  Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+  However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library".  The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+  When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library.  The
+threshold for this to be true is not precisely defined by law.
+
+  If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work.  (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+  Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+^L
+  6. As an exception to the Sections above, you may also combine or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+  You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License.  You must supply a copy of this License.  If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License.  Also, you must do one
+of these things:
+
+    a) Accompany the work with the complete corresponding
+    machine-readable source code for the Library including whatever
+    changes were used in the work (which must be distributed under
+    Sections 1 and 2 above); and, if the work is an executable linked
+    with the Library, with the complete machine-readable "work that
+    uses the Library", as object code and/or source code, so that the
+    user can modify the Library and then relink to produce a modified
+    executable containing the modified Library.  (It is understood
+    that the user who changes the contents of definitions files in the
+    Library will not necessarily be able to recompile the application
+    to use the modified definitions.)
+
+    b) Use a suitable shared library mechanism for linking with the
+    Library.  A suitable mechanism is one that (1) uses at run time a
+    copy of the library already present on the user's computer system,
+    rather than copying library functions into the executable, and (2)
+    will operate properly with a modified version of the library, if
+    the user installs one, as long as the modified version is
+    interface-compatible with the version that the work was made with.
+
+    c) Accompany the work with a written offer, valid for at least
+    three years, to give the same user the materials specified in
+    Subsection 6a, above, for a charge no more than the cost of
+    performing this distribution.
+
+    d) If distribution of the work is made by offering access to copy
+    from a designated place, offer equivalent access to copy the above
+    specified materials from the same place.
+
+    e) Verify that the user has already received a copy of these
+    materials or that you have already sent this user a copy.
+
+  For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it.  However, as a special exception,
+the materials to be distributed need not include anything that is
+normally distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+  It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system.  Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+^L
+  7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+    a) Accompany the combined library with a copy of the same work
+    based on the Library, uncombined with any other library
+    facilities.  This must be distributed under the terms of the
+    Sections above.
+
+    b) Give prominent notice with the combined library of the fact
+    that part of it is a work based on the Library, and explaining
+    where to find the accompanying uncombined form of the same work.
+
+  8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License.  Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License.  However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+  9. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Library or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+  10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties with
+this License.
+^L
+  11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply, and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License
+may add an explicit geographical distribution limitation excluding those
+countries, so that distribution is permitted only in or among
+countries not thus excluded.  In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+  13. The Free Software Foundation may publish revised and/or new
+versions of the Lesser General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation.  If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+^L
+  14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission.  For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this.  Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+                            NO WARRANTY
+
+  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+                     END OF TERMS AND CONDITIONS
+^L
+           How to Apply These Terms to Your New Libraries
+
+  If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change.  You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms
+of the ordinary General Public License).
+
+  To apply these terms, attach the following notices to the library.
+It is safest to attach them to the start of each source file to most
+effectively convey the exclusion of warranty; and each file should
+have at least the "copyright" line and a pointer to where the full
+notice is found.
+
+
+    <one line to give the library's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or
+your school, if any, to sign a "copyright disclaimer" for the library,
+if necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the
+  library `Frob' (a library for tweaking knobs) written by James
+  Random Hacker.
+
+  <signature of Ty Coon>, 1 April 1990
+  Ty Coon, President of Vice
+
+That's all there is to it!
+
+
diff --git a/README b/README
new file mode 100644
index 0000000..4600ee7
--- /dev/null
+++ b/README
@@ -0,0 +1,104 @@
+m5threads -- A pthread library for the M5 simulator
+===================================================
+Daniel Sanchez, Stanford University
+
+
+Changelog
+---------
+27-Jan-09
+- Added support for TLS in SPARC and x86-64 in static binaries. Alpha no longer works due to having unimplemented TLS support.
+- Fixed a race condition in rwlocks and condition variables.
+- Added support for detached threads.
+- Added thread-specific data (TSD) functions: key_create/delete/getspecific/setspecific.
+- Integrated with NPTL/LinuxThreads-based glibc (libc aliases, specific functions, intialization routines, and libc-specific TSD). libc calls are now MT-safe and the library runs the full test suite correctly. Tested on SPARC/glibc2.3.6/Linux 2.6.11 (LinuxThreads) in M5, and x86-64/glibc2.6/Linux 2.6.26 (NPTL) in an 8-core machine.
+- Added support for OpenMP programs (see test_opm.cpp) -- for now, works in x86 (real machine and M5), but not in SPARC.
+- Licensed under GPLv2.
+- Extended this README.
+
+23-Jan-2009 
+ - Added support for SPARC in pthread_exit
+ - Substituted tree barriers by counter barriers. Now barriers work regardless of which threads take them.
+
+21-Jan-2009
+ - Initial version
+
+License
+-------
+
+This software is licensed under the GPLv2. See the LICENSE file for a full copy of the license.
+
+This software contains portions of code from the Linux kernel and glibc 2.3.6. Both are redistributed under the terms of the GPL.
+
+Description
+-----------
+
+This library enables M5 to simulate multithreaded apps in system call emulation mode. It is intended as a replacement of NPTL/LinuxThreads implementations of libpthread. Instead of using a large portion of the Linux system calls, this library does as much as possible in user-level code. It requires just two system calls: clone, to spawn a new process, and exit, to finish a thread. As a result, it is easy to support in a syscall-emulation simulator. However, this is not a full implementation of pthreads, and the library lacks a thread scheduler. In M5, you will not be able to schedule more threads than thread contexts. 
+
+This library works in M5, but in real systems too. Both x86-64 and SPARC systems running Linux should execute programs correctly. In real systems, you will be able to allocate more threads than CPUs, but performance will degrade in this case since there is no thread scheduler (and thread switching occurs at the granularity of the OS scheduler).
+
+Only a subset of the pthread specification is supported. This includes:
+- Creation and join of joinable and detached threads; pthread_exit
+- Regular mutexes (NOT recursive or other rare modes)
+- Regular read-write locks
+- Barriers
+- Condition variables
+- Keys (key_create/delete, get/setspecific)
+- Miscellaneous functions:
+In particular, the following thinks are not supported:
+- pthread_cancel and related functions
+- pthread_kill
+- Anything else that has to do with signals
+- pthread_cleanup_XXX, pthread_unwind
+If your program uses a non-implemented pthread function, it will fail an assertion.
+
+This library should compile with GNU toolchains implementing LinuxThreads (Linux <=2.4 or 2.6) or NPTL (2.6 only) pthreads. If you compile it with an NPTL glibc, you may get futex() system calls if you try to do concurrent calls to multithreaded-safe glibc functions (e.g. printf). These are unimplemented in M5. To avoid them, enclose these calls in a global lock. Additionally, NPTL apps tend to use more system calls, so it is recommended to use M5 with a glibc compiled with LinuxThreads. Performance should be practically identical with both versions, as we are substituting the threading library.
+
+This library includes support for thread-local storage (TLS), but only for the SPARC and x86-64 ABIs (which are nearly identical). Alpha is no longer supported. Supporting Alpha would require implementing its TLS ABI.
+
+Compiling & using
+-----------------
+
+Applications compiled with this library should be built statically, and should link against the built pthread.o object file. Again, see the Makefile in the tests/ directory for the exact commands used.
+
+By default, the tests/Makefile builds all the tests using your system's g++. You can build sparc binaries by building a cross-compiler.
+
+TODOs
+-----
+- Ticket/MCS locks
+- Tree barriers
+- Add a scheduler, turning the library to an M:N model
+
+Implementation details
+======================
+
+What follows is recommended reading if you want to understand how the library works in more detail and extend it.
+
+This library implements mutexes as TTS spinlocks (taken from the Linux source code tree), and barriers as counter barriers. Compatibility with NPTL and LinuxThreads data structures is maintained by using a variety of macros, defined in pthread_defs.h
+
+All the memory regions needed to spawn a thread are contained in a single memory segment, the thread block, which has the following format:
+
+---------------------- <- lower addresses
+ Thread Control Block
+----------------------
+      TLS data
+---------------------- <- TLS pointer
+ (Unused) "real" TCB
+----------------------
+    [empty space]
+----------------------
+        Stack
+---------------------- <- Initial stack pointer (grows to lower addresses)
+     Stack Guard
+---------------------- <- upper addresses
+
+The thread control block contains the information relative to the current thread (status, flags, etc). The thread ID (returned by pthread_self()) is simply a pointer to the TCB. This enables distributed thread create/join, and there are no global structures for tracking thread data.
+
+The "real" TCB is the TCB defined by LinuxThreads or NPTL. We don't use it directly and initialize its contents to 0, but reserve some space because some variables (most notably errno) are in this area.
+
+The thread block conforms to the TLS ABI for x86-64 and SPARC architectures, which follows variant II as described in http://people.redhat.com/drepper/tls.pdf If you wish to extend this to other architectures, e.g. Alpha, be sure to read sections 1-3 of that document. Also, most of the code for the TLS part of the library is taken from glibc2.3.6 (search for the __libc_setup_tls function).
+
+In pthread_create, the parent mmaps the thread block, populates the TCB, and spawns a new child. The child sets up TLS before starting execution. In joinable threads, it is the responsibility of the parent to munmap the thread block. If the thread is detached, it will munmap its own thread block when exiting. The thread block is allocated with mmap because 1) it is a sizable chunk of memory and 2) this way, the child can delete its own stack (since munmap is a system call, not a function call, a stack is not required on return).
+
+The library includes function aliases, extra definitions, libc-specific keys and initialization code to work correctly with glibc. When linking with this library, glibc function calls *should* be MT-safe. However, how libc and libpthread interact has changed over time, and this may not work correctly with glibc versions >2.6. Modifying the code to support other versions of glibc should be straightworward, if you have some idea for what you are doing. I recommend using nm (to see what symbols are defined) and objdump (to dissasemble) on whatever version of libc.a you'll be using, and see if there is any mismatch with the "glibc glue" code in the library (mostly, at the end of pthread.c).
+
+
diff --git a/pthread.c b/pthread.c
new file mode 100644
index 0000000..972e47a
--- /dev/null
+++ b/pthread.c
@@ -0,0 +1,1129 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+
+    Author: Daniel Sanchez
+*/
+
+#include <unistd.h>
+#include <assert.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <signal.h>
+#include <sys/errno.h>
+#include <sched.h>
+#include <sys/mman.h>
+#include <string.h>
+#include <malloc.h>
+#include <sys/syscall.h>
+
+//Spinlock assembly
+#if defined(__x86) || defined(__x86_64)
+  #include "spinlock_x86.h"
+#elif defined(__alpha)
+  #include "spinlock_alpha.h"
+#elif defined(__sparc)
+  #include "spinlock_sparc.h"
+#else
+  #error "spinlock routines not available for your arch!\n"
+#endif
+
+#include "pthread_defs.h"
+#include "tls_defs.h"
+
+#define restrict 
+
+//64KB stack, change to your taste...
+#define CHILD_STACK_BITS 16
+#define CHILD_STACK_SIZE (1 << CHILD_STACK_BITS)
+
+//Debug macro
+#ifdef __DEBUG
+  #define DEBUG(args...) printf(args)
+#else
+  #define DEBUG(args...) 
+#endif
+
+//Size and alignment requirements of "real" (NPTL/LinuxThreads) thread control block
+#define TCB_SIZE 512
+#define TCB_ALIGN sizeof(double)
+//TODO: Figure out real (NPTL/LinuxThreads) TCB space. 512 bytes should be enough.
+
+//Thread control structure
+typedef struct {
+  pthread_t tid;
+  unsigned int is_detached; //0 if joinable, 1 if detached
+  volatile int child_finished;
+  void* result; //written by child on exit
+  void *(*start_routine)(void*);
+  void* arg;
+  //thread block limits
+  void* tls_start_addr;
+  void* stack_start_addr;
+} pthread_tcb_t;
+
+
+//Information about the thread block (TLS, sizes)
+static struct {
+  size_t tls_memsz;
+  size_t tls_filesz;
+  void*  tls_initimage;
+  size_t tls_align;
+  size_t total_size;
+  size_t stack_guard_size;
+} thread_block_info;
+
+
+/* Thread-local data */
+
+//Pointer to our TCB (NULL for main thread)
+__thread pthread_tcb_t* __tcb;
+
+// Used for TSD (getspecific, setspecific, etc.)
+__thread void** pthread_specifics = NULL; //dynamically allocated, since this is rarely used
+__thread uint32_t pthread_specifics_size = 0;
+
+
+/* Initialization, create/exit/join functions */
+
+// Search ELF segments, pull out TLS block info, campute thread block sizes
+static void populate_thread_block_info() {
+  ElfW(Phdr) *phdr;
+
+  //If there is no TLS segment...
+  thread_block_info.tls_memsz = 0;
+  thread_block_info.tls_filesz = 0;
+  thread_block_info.tls_initimage = NULL;
+  thread_block_info.tls_align = 0;
+
+  /* Look through the TLS segment if there is any.  */
+  if (_dl_phdr != NULL) {
+    for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr) {
+      if (phdr->p_type == PT_TLS) {
+          /* Gather the values we need.  */
+          thread_block_info.tls_memsz = phdr->p_memsz;
+          thread_block_info.tls_filesz = phdr->p_filesz;
+          thread_block_info.tls_initimage = (void *) phdr->p_vaddr;
+          thread_block_info.tls_align = phdr->p_align;
+          break;
+      }
+    }
+  }
+
+  //Set a stack guard size
+  //In SPARC/M5, this is needed to avoid out-of-range accesses on register saves...
+  //See src/arch/sparc/process.hh -- sets stackBias to 2047
+  thread_block_info.stack_guard_size = 2048;
+
+  //Total thread block size -- this is what we'll request to mmap
+  size_t sz = sizeof(pthread_tcb_t) + thread_block_info.tls_memsz + TCB_SIZE + thread_block_info.stack_guard_size + CHILD_STACK_SIZE;
+  //Note that TCB_SIZE is the "real" TCB size, not ours, which we leave zeroed (but some variables, notably errno, are somewhere inside there)
+
+  //Align to multiple of CHILD_STACK_SIZE
+  sz += CHILD_STACK_SIZE - 1;  
+  thread_block_info.total_size = (sz>>CHILD_STACK_BITS)<<CHILD_STACK_BITS;
+
+}
+
+
+//Set up TLS block in current thread
+static void setup_thread_tls(void* th_block_addr) {
+  /* Compute the (real) TCB offset */
+  size_t tcb_offset = roundup(thread_block_info.tls_memsz, TCB_ALIGN);
+  /* Align the TLS block.  */
+  void* tlsblock = (void *) (((uintptr_t) th_block_addr + thread_block_info.tls_align - 1)
+                       & ~(thread_block_info.tls_align - 1));
+  /* Initialize the TLS block.  */
+  char* tls_start_ptr = ((char *) tlsblock + tcb_offset
+                           - roundup (thread_block_info.tls_memsz, thread_block_info.tls_align ?: 1));
+
+  //DEBUG("Init TLS: Copying %d bytes from 0x%llx to 0x%llx\n", filesz, (uint64_t) initimage, (uint64_t) tls_start_ptr);
+  memcpy (tls_start_ptr, thread_block_info.tls_initimage, thread_block_info.tls_filesz);
+
+  //Rest of tls vars are already cleared (mmap returns zeroed memory)
+
+  //Note: We don't care about DTV pointers for x86/SPARC -- they're never used in static mode
+  /* Initialize the thread pointer.  */
+  TLS_INIT_TP ((char *) tlsblock + tcb_offset, 0);
+}
+
+//Some NPTL definitions
+int __libc_multiple_threads; //set to one on initialization
+int __nptl_nthreads = 32; //TODO: we don't really know...
+
+//Called at initialization. Sets up TLS for the main thread and populates thread_block_info, used in subsequent calls
+//Works with LinuxThreads and NPTL
+void __pthread_initialize_minimal() {
+  __libc_multiple_threads = 1; //tell libc we're multithreaded (NPTL-specific)
+  populate_thread_block_info();
+  void* ptr = mmap(0, thread_block_info.total_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+  setup_thread_tls(ptr);
+}
+
+
+//Used by pthread_create to spawn child
+static int __pthread_trampoline(void* thr_ctrl) {
+  //Set TLS up
+  pthread_tcb_t* tcb = (pthread_tcb_t*) thr_ctrl; 
+  setup_thread_tls(tcb->tls_start_addr);
+  __tcb = tcb;
+  DEBUG("Child in trampoline, TID=%llx\n", tcb->tid);
+
+  void* result = tcb->start_routine(tcb->arg);
+  pthread_exit(result);
+  assert(0); //should never be reached
+}
+
+int pthread_create (pthread_t* thread,
+                    const pthread_attr_t* attr,
+                    void *(*start_routine)(void*), 
+                    void* arg) {
+  DEBUG("pthread_create: start\n");
+
+  //Allocate the child thread block (TCB+TLS+stack area)
+  //We use mmap so that the child can munmap it at exit without using a stack (it's a system call)
+  void* thread_block;
+  size_t thread_block_size = thread_block_info.total_size;
+  thread_block = mmap(0, thread_block_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+  DEBUG("pthread_create: mmapped child thread block 0x%llx -- 0x%llx\n", thread_block, ((char*)thread_block) + CHILD_STACK_SIZE) ;
+ 
+  //Populate the thread control block
+  pthread_tcb_t* tcb = (pthread_tcb_t*) thread_block;
+  tcb->tid = (pthread_t) thread_block; //thread ID is tcb address itself
+  tcb->is_detached = 0; //joinable
+  tcb->child_finished = 0;
+  tcb->start_routine = start_routine;
+  tcb->arg = arg;
+  tcb->tls_start_addr = (void*)(((char*)thread_block) + sizeof(pthread_tcb_t)); //right after tcb
+  tcb->stack_start_addr = (void*) (((char*) thread_block) + thread_block_size - thread_block_info.stack_guard_size); //end of thread_block
+  
+  *thread=(pthread_t) thread_block;
+
+  //Call clone()
+  DEBUG("pthread_create: prior to clone()\n");
+  clone(__pthread_trampoline, tcb->stack_start_addr, CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD, tcb);
+  DEBUG("pthread_create: after clone()\n");
+  return 0;
+}
+
+pthread_t pthread_self() {
+    if (__tcb == NULL) return 0; //main thread
+    return __tcb->tid;
+}
+
+int pthread_join (pthread_t thread, void** status) {
+    DEBUG("pthread_join: started\n");
+    pthread_tcb_t* child_tcb = (pthread_tcb_t*) thread;
+    assert(child_tcb->tid == thread); // checks that this is really a tcb
+    assert(!child_tcb->is_detached); // thread should be joinable
+    volatile int child_done = 0;
+    while (child_done == 0) { // spin until child done
+        child_done = child_tcb->child_finished;
+    }
+    DEBUG("pthread_join: child joined\n");
+    //Get result
+    if (status) *status = child_tcb->result;
+
+    //Deallocate child block
+    //munmap(child_tcb, thread_block_info.total_size);   
+
+    return 0;
+
+}
+
+
+void pthread_exit (void* status) {
+    // TODO: The good way to solve this is to have the child, not its parent, free
+    // its own stack (and TLS segment). This enables detached threads. But to do this
+    // you need an extra stack. A way to do this is to have a global, lock-protected 
+    // manager stack, or have the M5 exit system call do it... Anyhow, I'm deferring
+    // this problem until we have TLS.
+
+    //From point (XXX)  on, the thread **does not exist**,
+    //as its parent may have already freed the stack. 
+    //So we must call sys_exit without using the stack => asm
+
+    // NOTE: You may be tempted to call exit(0) or _exit(0) here, but there call exit_group,
+    // killing the whole process and not just the current thread
+
+    //If the keys array was allocated, free it
+    if (pthread_specifics != NULL) free(pthread_specifics);
+
+    //Main thread
+    if (__tcb == NULL) _exit(0);
+
+    DEBUG("Child TID=0x%llx in pthread_exit...\n", pthread_self() );
+    __tcb->result = status;
+    //TODO mem barrier here...
+    __tcb->child_finished = 1;
+    //XXX
+    syscall(__NR_exit,0);
+    assert(0); //should never be reached
+
+/*#if defined(__x86) or defined(__x86_64)
+    __asm__ __volatile__  (
+         "\nmov  $0x3c,%%eax\n\t" \
+         "syscall\n\t" 
+         ::: "eax");
+#elif defined(__alpha)
+    __asm__ __volatile__  (
+         "\nldi  $0,1\n\t" \
+         "callsys\n\t");
+#elif defined(__sparc)
+    // Since this part of the code is provisional, don't bother with asm for now
+    syscall(__NR_exit,0);
+#else
+    #error "No pthread_exit asm for your arch, sorry!\n"
+#endif
+
+    assert(0);*/
+}
+
+
+// mutex functions
+
+int pthread_mutex_init (pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) {
+    mutex->PTHREAD_MUTEX_T_COUNT = 0;
+    return 0;
+}
+
+int pthread_mutex_lock (pthread_mutex_t* lock) {
+    spin_lock((int*)&lock->PTHREAD_MUTEX_T_COUNT);
+    return 0;
+}
+
+int pthread_mutex_unlock (pthread_mutex_t* lock) {
+    spin_unlock((int*)&lock->PTHREAD_MUTEX_T_COUNT);
+    return 0;
+}
+
+int pthread_mutex_destroy (pthread_mutex_t* mutex) {
+    return 0;
+}
+
+int pthread_mutex_trylock (pthread_mutex_t* mutex) {
+    int acquired = trylock((int*)&mutex->PTHREAD_MUTEX_T_COUNT);
+    if (acquired == 1) {
+        return 0;
+    }
+    return EBUSY;
+}
+
+// rwlock functions
+
+int pthread_rwlock_init (pthread_rwlock_t* lock, const pthread_rwlockattr_t* attr) {
+    PTHREAD_RWLOCK_T_LOCK(lock) = 0; // used only with spin_lock, so we know to initilize to zero
+    PTHREAD_RWLOCK_T_READERS(lock) = 0;
+    PTHREAD_RWLOCK_T_WRITER(lock) = -1; // -1 means no one owns the write lock
+
+    return 0;
+}
+
+int pthread_rwlock_destroy (pthread_rwlock_t* lock) {
+    return 0;
+}
+
+int pthread_rwlock_rdlock (pthread_rwlock_t* lock) {
+    do {
+        // this is to reduce the contention and a possible live-lock to lock->access_lock
+        while (1) {
+            pthread_t writer = PTHREAD_RWLOCK_T_WRITER(lock);
+            if (writer == -1) {
+                break;
+            }
+        }
+
+        spin_lock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
+        if ((pthread_t)PTHREAD_RWLOCK_T_WRITER(lock) == -1) {
+            PTHREAD_RWLOCK_T_READERS(lock)++;
+            spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
+            return 0;
+        }
+        spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
+    } while (1);
+    return 0;
+}
+
+int pthread_rwlock_wrlock (pthread_rwlock_t* lock) {
+    do {
+        while (1) {
+            pthread_t writer = PTHREAD_RWLOCK_T_WRITER(lock);
+            if (writer == -1) {
+                break;
+            }
+            int num_readers = PTHREAD_RWLOCK_T_READERS(lock);
+            if (num_readers == 0) {
+                break;
+            }
+        }
+
+        spin_lock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
+        if ((pthread_t)PTHREAD_RWLOCK_T_WRITER(lock) == -1 && PTHREAD_RWLOCK_T_READERS(lock) == 0) {
+            PTHREAD_RWLOCK_T_WRITER(lock) = pthread_self();
+            spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
+            return 0;
+        }
+        spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
+    } while (1);
+    return 0;
+}
+
+int pthread_rwlock_unlock (pthread_rwlock_t* lock) {
+    spin_lock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
+    if (pthread_self() == PTHREAD_RWLOCK_T_WRITER(lock)) {
+        // the write lock will be released
+        PTHREAD_RWLOCK_T_WRITER(lock) = -1;
+    } else {
+        // one of the read locks will be released
+        PTHREAD_RWLOCK_T_READERS(lock) = PTHREAD_RWLOCK_T_READERS(lock) - 1;
+    }
+    spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
+    return 0;
+}
+
+
+// key functions
+#ifndef PTHREAD_KEYS_MAX
+#define PTHREAD_KEYS_MAX 1024
+#endif
+
+typedef struct {
+  int in_use;
+  void (*destr)(void*);
+} pthread_key_struct;
+
+static pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX];
+static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+int pthread_key_create (pthread_key_t* key, void (*destructor)(void*)) {
+  int i;
+
+  pthread_mutex_lock(&pthread_keys_mutex);
+  for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
+    if (! pthread_keys[i].in_use) {
+      /* Mark key in use */
+      pthread_keys[i].in_use = 1;
+      pthread_keys[i].destr = destructor;
+      pthread_mutex_unlock(&pthread_keys_mutex);
+      *key = i;
+      return 0;
+    }
+  }
+  pthread_mutex_unlock(&pthread_keys_mutex);
+  return EAGAIN;
+}
+
+int pthread_key_delete (pthread_key_t key)
+{
+  pthread_mutex_lock(&pthread_keys_mutex);
+  if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) {
+    pthread_mutex_unlock(&pthread_keys_mutex);
+    return EINVAL;
+  }
+  pthread_keys[key].in_use = 0;
+  pthread_keys[key].destr = NULL;
+
+  /* NOTE: The LinuxThreads implementation actually zeroes deleted keys on
+     spawned threads. I don't care, the spec says that if you are  access a
+     key after if has been deleted, you're on your own. */
+
+  pthread_mutex_unlock(&pthread_keys_mutex);
+  return 0;
+}
+
+int pthread_setspecific (pthread_key_t key, const void* value) {
+  int m_size;
+  if (key < 0 || key >= PTHREAD_KEYS_MAX) return EINVAL; 
+  if (key >= pthread_specifics_size) {
+    m_size = (key+1)*sizeof(void*);
+    if (pthread_specifics_size == 0) {
+       pthread_specifics = (void**) malloc(m_size);
+       DEBUG("pthread_setspecific: malloc of size %d bytes, got 0x%llx\n", m_size, pthread_specifics);
+    } else {
+       pthread_specifics = (void**) realloc(pthread_specifics, m_size);
+       DEBUG("pthread_setspecific: realloc of size %d bytes, got 0x%llx\n", m_size, pthread_specifics);
+    }
+    pthread_specifics_size = key+1;
+  }
+  pthread_specifics[key] = (void*) value;
+  return 0;
+}
+
+void* pthread_getspecific (pthread_key_t key) {
+  if (key < 0 || key >= pthread_specifics_size) return NULL;
+  DEBUG("pthread_getspecific: key=%d pthread_specifics_size=%d\n", key, pthread_specifics_size);
+  return pthread_specifics[key]; 
+}
+
+// condition variable functions
+
+int pthread_cond_init (pthread_cond_t* cond, const pthread_condattr_t* attr) {
+    PTHREAD_COND_T_FLAG(cond) = 0;
+    PTHREAD_COND_T_THREAD_COUNT(cond) = 0;
+    PTHREAD_COND_T_COUNT_LOCK(cond) = 0;
+    return 0;    
+}
+
+int pthread_cond_destroy (pthread_cond_t* cond) {
+    return 0;
+}
+
+int pthread_cond_broadcast (pthread_cond_t* cond) {
+    PTHREAD_COND_T_FLAG(cond) = 1;
+    return 0;
+}
+
+int pthread_cond_wait (pthread_cond_t* cond, pthread_mutex_t* lock) {
+    volatile int* thread_count  = &(PTHREAD_COND_T_THREAD_COUNT(cond));
+    volatile int* flag = &(PTHREAD_COND_T_FLAG(cond));
+    volatile int* count_lock    = &(PTHREAD_COND_T_COUNT_LOCK(cond));
+
+    // dsm: ++/-- have higher precedence than *, so *thread_count++
+    // increments *the pointer*, then dereferences it (!)
+    (*thread_count)++;
+
+    pthread_mutex_unlock(lock);
+    while (1) {
+        volatile int f = *flag;
+        if (f == 1) {
+            break;
+        }
+    }
+
+    spin_lock(count_lock);
+
+    (*thread_count)--;
+
+    if (*thread_count == 0) {
+        *flag = 0;
+    }
+    spin_unlock(count_lock);
+    pthread_mutex_lock(lock);
+    return 0;
+}
+
+int pthread_cond_signal (pthread_cond_t* cond) {
+    //Could also signal only one thread, but this is compliant too
+    //TODO: Just wake one thread up
+    return pthread_cond_broadcast(cond);
+}
+
+
+//barrier functions
+
+//These funny tree barriers will only work with consecutive TIDs starting from 0, e.g. a barrier initialized for 8 thread will need to be taken by TIDs 0-7
+//TODO: Adapt to work with arbitrary TIDs
+/*int pthread_barrier_init (pthread_barrier_t *restrict barrier,
+                          const pthread_barrierattr_t *restrict attr, unsigned count)
+{
+    assert(barrier != NULL);
+    //assert(0 < count && count <= MAX_NUM_CPUS);
+
+    PTHREAD_BARRIER_T_NUM_THREADS(barrier) = count;
+
+    // add one to avoid false sharing
+    tree_barrier_t* ptr
+        = ((tree_barrier_t*)malloc((count + 1) * sizeof(tree_barrier_t))) + 1;
+    for (unsigned i = 0; i < count; ++i) {
+      ptr[i].value = 0;
+    }
+
+    PTHREAD_BARRIER_T_BARRIER_PTR(barrier) = ptr;
+
+    return 0;
+}
+
+int pthread_barrier_destroy (pthread_barrier_t *barrier)
+{
+    free(PTHREAD_BARRIER_T_BARRIER_PTR(barrier) - 1);
+    return 0;
+}
+
+int pthread_barrier_wait (pthread_barrier_t* barrier)
+{
+    int const num_threads = PTHREAD_BARRIER_T_NUM_THREADS(barrier);
+    int const self = pthread_self(); 
+    tree_barrier_t * const barrier_ptr = PTHREAD_BARRIER_T_BARRIER_PTR(barrier);
+
+    int const goal = 1 - barrier_ptr[self].value;
+
+    int round_mask = 3;
+    while ((self & round_mask) == 0 && round_mask < (num_threads << 2)) {
+      int const spacing = (round_mask + 1) >> 2;
+      for (int i = 1; i <= 3 && self + i*spacing < num_threads; ++i) {
+        while (barrier_ptr[self + i*spacing].value != goal) {
+          // spin
+        }
+      }
+      round_mask = (round_mask << 2) + 3;
+    }
+
+    barrier_ptr[self].value = goal;
+    while (barrier_ptr[0].value != goal) {
+      // spin
+    }
+
+    return 0;
+}*/
+
+int pthread_barrier_init (pthread_barrier_t *restrict barrier,
+                          const pthread_barrierattr_t *restrict attr, unsigned count)
+{
+    assert(barrier != NULL);
+
+    PTHREAD_BARRIER_T_NUM_THREADS(barrier) =  count;
+    PTHREAD_BARRIER_T_SPINLOCK(barrier) = 0;
+    PTHREAD_BARRIER_T_COUNTER(barrier) = 0;
+    PTHREAD_BARRIER_T_DIRECTION(barrier) = 0; //up
+
+    return 0;
+}
+
+int pthread_barrier_destroy (pthread_barrier_t *barrier)
+{
+    //Nothing to do
+    return 0;
+}
+
+int pthread_barrier_wait (pthread_barrier_t* barrier)
+{
+    int const initial_direction = PTHREAD_BARRIER_T_DIRECTION(barrier); //0 == up, 1 == down
+
+    if (initial_direction == 0) {
+       spin_lock(&(PTHREAD_BARRIER_T_SPINLOCK(barrier)));
+       PTHREAD_BARRIER_T_COUNTER(barrier)++; 
+       if (PTHREAD_BARRIER_T_COUNTER(barrier) == PTHREAD_BARRIER_T_NUM_THREADS(barrier)) {
+           //reverse direction, now down
+           PTHREAD_BARRIER_T_DIRECTION(barrier) = 1;
+       }
+       spin_unlock(&(PTHREAD_BARRIER_T_SPINLOCK(barrier)));
+    } else {
+       spin_lock(&(PTHREAD_BARRIER_T_SPINLOCK(barrier)));
+       PTHREAD_BARRIER_T_COUNTER(barrier)--;
+       if (PTHREAD_BARRIER_T_COUNTER(barrier) == 0) {
+          //reverse direction, now up
+          PTHREAD_BARRIER_T_DIRECTION(barrier) = 0;
+       }
+       spin_unlock(&(PTHREAD_BARRIER_T_SPINLOCK(barrier)));
+   }
+
+   volatile int direction = PTHREAD_BARRIER_T_DIRECTION(barrier);
+   while (initial_direction == direction) {
+      //spin
+      direction = PTHREAD_BARRIER_T_DIRECTION(barrier);
+   }
+
+   return 0;
+}
+
+//misc functions
+
+static pthread_mutex_t __once_mutex = PTHREAD_MUTEX_INITIALIZER;
+int pthread_once (pthread_once_t* once,
+                  void (*init)(void))
+{
+  //fast path
+  if (*once != PTHREAD_ONCE_INIT) return 0;
+  pthread_mutex_lock(&__once_mutex);
+  if (*once != PTHREAD_ONCE_INIT) {
+    pthread_mutex_unlock(&__once_mutex);
+    return 0;
+  }
+  *once = PTHREAD_ONCE_INIT+1;
+  init();
+  pthread_mutex_unlock(&__once_mutex);
+  return 0;
+}
+
+#ifndef __USE_EXTERN_INLINES
+int pthread_equal (pthread_t t1, pthread_t t2)
+{
+    return t1 == t2; //that was hard :-)
+}
+#endif
+
+// Functions that we want defined, but we don't use them
+// All other functions are not defined so that they will cause a compile time
+// error and we can decide if we need to do something with them
+
+// functions really don't need to do anything
+
+int pthread_yield() {
+    // nothing else to yield to
+    return 0;
+}
+
+int pthread_attr_init (pthread_attr_t* attr) {
+    return 0;
+}
+
+int pthread_attr_setscope (pthread_attr_t* attr, int scope) {
+    return 0;
+}
+
+int pthread_rwlockattr_init (pthread_rwlockattr_t* attr) {
+    return 0;
+}
+
+int pthread_attr_setstacksize (pthread_attr_t* attr, size_t stacksize) {
+    return 0;
+}
+
+int pthread_attr_setschedpolicy (pthread_attr_t* attr, int policy) {
+    return 0;
+}
+
+// some functions that we don't really support
+
+int pthread_setconcurrency (int new_level) {
+    return 0;
+}
+
+int pthread_setcancelstate (int p0, int* p1)
+{
+    //NPTL uses this
+    return 0;
+}
+
+//and some affinity functions (used by libgomp, openmp)
+int pthread_getaffinity_np(pthread_t thread, size_t size, cpu_set_t *set) {
+  return 0;
+}
+
+int pthread_setaffinity_np(pthread_t thread, size_t size, cpu_set_t *set) {
+  return 0;
+}
+
+int pthread_attr_setaffinity_np(pthread_attr_t attr, size_t cpusetsize, const cpu_set_t *cpuset) {
+  return 0;
+}
+
+int pthread_attr_getaffinity_np(pthread_attr_t attr, size_t cpusetsize, cpu_set_t *cpuset) {
+  return 0;
+}
+
+
+// ... including any dealing with thread-level signal handling
+// (maybe we should throw an error message instead?)
+
+int pthread_sigmask (int how, const sigset_t* set, sigset_t* oset) {
+    return 0;
+}
+
+int pthread_kill (pthread_t thread, int sig)  {
+    assert(0);
+}
+
+// unimplemented pthread functions
+
+int pthread_atfork (void (*f0)(void),
+                    void (*f1)(void),
+                    void (*f2)(void))
+{
+    assert(0);
+}
+
+int pthread_attr_destroy (pthread_attr_t* attr)
+{
+    assert(0);
+}
+
+int pthread_attr_getdetachstate (const pthread_attr_t* attr,
+                                 int* b)
+{
+    assert(0);
+}
+
+int pthread_attr_getguardsize (const pthread_attr_t* restrict a,
+                               size_t *restrict b)
+{
+    assert(0);
+}
+
+int pthread_attr_getinheritsched (const pthread_attr_t *restrict a,
+                                  int *restrict b)
+{
+    assert(0);
+}
+
+int pthread_attr_getschedparam (const pthread_attr_t *restrict a,
+                                struct sched_param *restrict b)
+{
+    assert(0);
+}
+
+int pthread_attr_getschedpolicy (const pthread_attr_t *restrict a,
+                                 int *restrict b)
+{
+    assert(0);
+}
+
+int pthread_attr_getscope (const pthread_attr_t *restrict a,
+                           int *restrict b)
+{
+    assert(0);
+}
+
+int pthread_attr_getstack (const pthread_attr_t *restrict a,
+                           void* *restrict b,
+                           size_t *restrict c)
+{
+    assert(0);
+}
+
+int pthread_attr_getstackaddr (const pthread_attr_t *restrict a,
+                               void* *restrict b)
+{
+    assert(0);
+}
+
+int pthread_attr_getstacksize (const pthread_attr_t *restrict a,
+                               size_t *restrict b)
+{
+    assert(0);
+}
+
+int pthread_attr_setdetachstate (pthread_attr_t* a,
+                                 int b)
+{
+   return 0; //FIXME
+}
+int pthread_attr_setguardsize (pthread_attr_t* a,
+                               size_t b)
+{
+    assert(0);
+}
+
+int pthread_attr_setinheritsched (pthread_attr_t* a,
+                                  int b)
+{
+    assert(0);
+}
+
+int pthread_attr_setschedparam (pthread_attr_t *restrict a,
+                                const struct sched_param *restrict b)
+{
+    assert(0);
+}
+
+int pthread_attr_setstack (pthread_attr_t* a,
+                           void* b,
+                           size_t c)
+{
+    assert(0);
+}
+
+int pthread_attr_setstackaddr (pthread_attr_t* a,
+                               void* b)
+{
+    assert(0);
+}
+
+int pthread_cancel (pthread_t a)
+{
+    assert(0);
+}
+
+void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer,
+                            void (*__routine) (void *),
+                            void *__arg) 
+{
+    assert(0);
+}
+
+void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer,
+                           int __execute) 
+{
+    assert(0);
+}
+
+int pthread_cond_timedwait (pthread_cond_t *restrict a,
+                            pthread_mutex_t *restrict b,
+                            const struct timespec *restrict c)
+{
+    assert(0);
+}
+
+int pthread_condattr_destroy (pthread_condattr_t* a)
+{
+    assert(0);
+}
+
+int pthread_condattr_getpshared (const pthread_condattr_t *restrict a,
+                                 int *restrict b)
+{
+    assert(0);
+}
+
+int pthread_condattr_init (pthread_condattr_t* a)
+{
+    assert(0);
+}
+
+int pthread_condattr_setpshared (pthread_condattr_t* a,
+                                 int b)
+{
+    assert(0);
+}
+
+int pthread_detach (pthread_t a)
+{
+    assert(0);
+}
+
+
+int pthread_getconcurrency ()
+{
+    assert(0);
+}
+
+int pthread_getschedparam(pthread_t a,
+                          int *restrict b,
+                          struct sched_param *restrict c)
+{
+    assert(0);
+}
+
+int pthread_mutex_getprioceiling (const pthread_mutex_t *restrict a,
+                                  int *restrict b)
+{
+    assert(0);
+}
+
+int pthread_mutex_setprioceiling (pthread_mutex_t *restrict a,
+                                  int b,
+                                  int *restrict c)
+{
+    assert(0);
+}
+
+int pthread_mutex_timedlock (pthread_mutex_t* a,
+                             const struct timespec* b)
+{
+    assert(0);
+}
+
+int pthread_mutexattr_destroy (pthread_mutexattr_t* a)
+{
+    //assert(0);
+    //used by libc
+    return 0;
+}
+
+int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t *restrict a,
+                                      int *restrict b)
+{
+    assert(0);
+}
+
+int pthread_mutexattr_getprotocol (const pthread_mutexattr_t *restrict a,
+                                   int *restrict b)
+{
+    assert(0);
+}
+
+int pthread_mutexattr_getpshared (const pthread_mutexattr_t *restrict a,
+                                  int *restrict b)
+{
+    assert(0);
+}
+
+int pthread_mutexattr_gettype (const pthread_mutexattr_t *restrict a,
+                               int *restrict b)
+{
+    assert(0);
+}
+
+int pthread_mutexattr_init (pthread_mutexattr_t* a)
+{
+    //assert(0);
+    //used by libc
+    return 0;
+}
+
+int pthread_mutexattr_setprioceiling (pthread_mutexattr_t* a,
+                                      int b)
+{
+    assert(0);
+}
+
+int pthread_mutexattr_setprotocol (pthread_mutexattr_t* a,
+                                   int b)
+{
+    assert(0);
+}
+
+int pthread_mutexattr_setpshared (pthread_mutexattr_t* a,
+                                  int b)
+{
+    assert(0);
+}
+
+int pthread_mutexattr_settype (pthread_mutexattr_t* a,
+                               int b)
+{
+    //assert(0);
+    //used by libc
+    //yeah, and the freaking libc just needs a recursive lock.... screw it
+    //if (b == PTHREAD_MUTEX_RECURSIVE_NP) assert(0);
+    return 0;
+}
+
+int pthread_rwlock_timedrdlock (pthread_rwlock_t *restrict a,
+                                const struct timespec *restrict b)
+{
+    assert(0);
+}
+
+int pthread_rwlock_timedwrlock (pthread_rwlock_t *restrict a,
+                                const struct timespec *restrict b)
+{
+    assert(0);
+}
+
+int pthread_rwlock_tryrdlock (pthread_rwlock_t* a)
+{
+    assert(0);
+}
+
+int pthread_rwlock_trywrlock (pthread_rwlock_t* a)
+{
+    assert(0);
+}
+
+int pthread_rwlockattr_destroy (pthread_rwlockattr_t* a)
+{
+    assert(0);
+}
+
+int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *restrict a,
+                                   int *restrict b)
+{
+    assert(0);
+}
+
+int pthread_rwlockattr_setpshared(pthread_rwlockattr_t* a,
+                                  int b)
+{
+    assert(0);
+}
+
+int pthread_setcanceltype (int a,
+                           int* b)
+{
+    assert(0);
+}
+
+int pthread_setschedparam (pthread_t a,
+                           int b,
+                           const struct sched_param* c)
+{
+    assert(0);
+}
+
+int pthread_setschedprio (pthread_t a,
+                          int b)
+{
+    assert(0);
+}
+
+void pthread_testcancel ()
+{
+    assert(0);
+}
+
+
+/* Stuff to properly glue with glibc */
+
+// glibc keys
+
+//For NPTL, or LinuxThreads with TLS defined and used
+__thread void* __libc_tsd_MALLOC;
+__thread void* __libc_tsd_DL_ERROR;
+__thread void* __libc_tsd_RPC_VARS;
+//__thread void* __libc_tsd_LOCALE; seems to be defined in my libc already, but your glibc might not dfine it...
+//Defined in libgomp (OpenMP)
+//__thread void* __libc_tsd_CTYPE_B;
+//__thread void* __libc_tsd_CTYPE_TOLOWER;
+//__thread void* __libc_tsd_CTYPE_TOUPPER;
+
+//If glibc was not compiled with __thread, it uses __pthread_internal_tsd_get/set/address for its internal keys
+//These are from linuxthreads-0.7.1/specific.c
+
+//FIXME: When enabled, SPARC/M5 crashes (for some weird reason, libc calls a tsd_get on an uninitialized key at initialization, and uses its result). Are we supposed to initialize these values??
+//libc can live without these, so it's not critical
+#if 0
+enum __libc_tsd_key_t { _LIBC_TSD_KEY_MALLOC = 0,
+                        _LIBC_TSD_KEY_DL_ERROR,
+                        _LIBC_TSD_KEY_RPC_VARS,
+                        _LIBC_TSD_KEY_LOCALE,
+                        _LIBC_TSD_KEY_CTYPE_B,
+                        _LIBC_TSD_KEY_CTYPE_TOLOWER,
+                        _LIBC_TSD_KEY_CTYPE_TOUPPER,
+                        _LIBC_TSD_KEY_N };
+__thread void* p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
+
+int
+__pthread_internal_tsd_set (int key, const void * pointer)
+{
+  p_libc_specific[key] = (void*) pointer;
+  return 0;
+}
+
+void *
+__pthread_internal_tsd_get (int key)
+{
+  return  p_libc_specific[key];
+}
+
+void ** __attribute__ ((__const__))
+__pthread_internal_tsd_address (int key)
+{
+  return &p_libc_specific[key];
+}
+#endif //0
+
+
+//Aliases for glibc
+int __pthread_mutex_init (pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)  __attribute__ ((weak, alias ("pthread_mutex_init")));
+int __pthread_mutex_lock (pthread_mutex_t* lock) __attribute__ ((weak, alias ("pthread_mutex_lock")));
+int __pthread_mutex_trylock (pthread_mutex_t* lock) __attribute__ ((weak, alias ("pthread_mutex_trylock")));
+int __pthread_mutex_unlock (pthread_mutex_t* lock) __attribute__ ((weak, alias ("pthread_mutex_unlock")));
+
+int __pthread_mutexattr_destroy (pthread_mutexattr_t* a) __attribute__ ((weak, alias ("pthread_mutexattr_destroy")));
+int __pthread_mutexattr_init (pthread_mutexattr_t* a) __attribute__ ((weak, alias ("pthread_mutexattr_init")));
+int __pthread_mutexattr_settype (pthread_mutexattr_t* a, int b) __attribute__ ((weak, alias ("pthread_mutexattr_settype")));
+
+int __pthread_rwlock_init (pthread_rwlock_t* lock, const pthread_rwlockattr_t* attr) __attribute__ ((weak, alias ("pthread_rwlock_init")));  
+int __pthread_rwlock_rdlock (pthread_rwlock_t* lock) __attribute__ ((weak, alias ("pthread_rwlock_rdlock")));
+int __pthread_rwlock_wrlock (pthread_rwlock_t* lock) __attribute__ ((weak, alias ("pthread_rwlock_wrlock")));
+int __pthread_rwlock_unlock (pthread_rwlock_t* lock) __attribute__ ((weak, alias ("pthread_rwlock_unlock")));
+int __pthread_rwlock_destroy (pthread_rwlock_t* lock) __attribute__ ((weak, alias ("pthread_rwlock_destroy")));
+/*
+int   __pthread_key_create(pthread_key_t *, void (*)(void *)) __attribute__ ((weak, alias ("pthread_key_create")));
+int   __pthread_key_delete(pthread_key_t) __attribute__ ((weak, alias ("pthread_key_delete")));
+void* __pthread_getspecific(pthread_key_t) __attribute__ ((weak, alias ("pthread_getspecific")));
+int   __pthread_setspecific(pthread_key_t, const void *) __attribute__ ((weak, alias ("pthread_setspecific")));
+*/
+int __pthread_once (pthread_once_t* once, void (*init)(void))  __attribute__ ((weak, alias ("pthread_once")));
+
+
+//No effect, NPTL-specific, may cause leaks? (TODO: Check!)
+void __nptl_deallocate_tsd() {}
+
+
diff --git a/pthread_defs.h b/pthread_defs.h
new file mode 100644
index 0000000..6bfea7c
--- /dev/null
+++ b/pthread_defs.h
@@ -0,0 +1,142 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+*/
+
+
+
+#ifndef __PTHREAD_DEFS_H__
+#define __PTHREAD_DEFS_H__
+
+
+/*typedef struct {
+    volatile int value;
+    long _padding[15]; // to prevent false sharing
+} tree_barrier_t;*/
+
+// old LinuxThreads needs different magic than newer NPTL implementation
+// definitions for LinuxThreads
+#ifdef __linux__
+
+//XOPEN2K and UNIX98 defines to avoid for rwlocks/barriers when compiling with gcc...
+//see <bits/pthreadtypes.h>
+#if !defined(__USE_UNIX98) && !defined(__USE_XOPEN2K) && !defined(__SIZEOF_PTHREAD_MUTEX_T)
+/* Read-write locks.  */
+typedef struct _pthread_rwlock_t
+{
+  struct _pthread_fastlock __rw_lock; /* Lock to guarantee mutual exclusion */
+  int __rw_readers;                   /* Number of readers */
+  _pthread_descr __rw_writer;         /* Identity of writer, or NULL if none */
+  _pthread_descr __rw_read_waiting;   /* Threads waiting for reading */
+  _pthread_descr __rw_write_waiting;  /* Threads waiting for writing */
+  int __rw_kind;                      /* Reader/Writer preference selection */
+  int __rw_pshared;                   /* Shared between processes or not */
+} pthread_rwlock_t;
+
+
+/* Attribute for read-write locks.  */
+typedef struct
+{
+  int __lockkind;
+  int __pshared;
+} pthread_rwlockattr_t;
+#endif
+#if !defined(__USE_XOPEN2K) && !defined(__SIZEOF_PTHREAD_MUTEX_T)
+/* POSIX spinlock data type.  */
+typedef volatile int pthread_spinlock_t;
+
+/* POSIX barrier. */
+typedef struct {
+  struct _pthread_fastlock __ba_lock; /* Lock to guarantee mutual exclusion */
+  int __ba_required;                  /* Threads needed for completion */
+  int __ba_present;                   /* Threads waiting */
+  _pthread_descr __ba_waiting;        /* Queue of waiting threads */
+} pthread_barrier_t;
+
+/* barrier attribute */
+typedef struct {
+  int __pshared;
+} pthread_barrierattr_t;
+
+#endif
+
+
+#ifndef  __SIZEOF_PTHREAD_MUTEX_T
+#define PTHREAD_MUTEX_T_COUNT __m_count
+
+#define PTHREAD_COND_T_FLAG(cond) (*(volatile int*)(&(cond->__c_lock.__status)))
+#define PTHREAD_COND_T_THREAD_COUNT(cond) (*(volatile int*)(&(cond-> __c_waiting)))
+#define PTHREAD_COND_T_COUNT_LOCK(cond) (*(volatile int*)(&(cond->__c_lock.__spinlock)))
+
+#define PTHREAD_RWLOCK_T_LOCK(rwlock)  (*(volatile int*)(&rwlock->__rw_lock))
+#define PTHREAD_RWLOCK_T_READERS(rwlock)  (*(volatile int*)(&rwlock->__rw_readers))
+#define PTHREAD_RWLOCK_T_WRITER(rwlock)  (*(volatile pthread_t*)(&rwlock->__rw_kind))
+
+//For tree barriers
+//#define PTHREAD_BARRIER_T_NUM_THREADS(barrier)  (*(int*)(&barrier->__ba_lock.__spinlock))
+//#define PTHREAD_BARRIER_T_BARRIER_PTR(barrier) (*(tree_barrier_t**)(&barrier->__ba_required))
+
+#define PTHREAD_BARRIER_T_SPINLOCK(barrier)  (*(volatile int*)(&barrier->__ba_lock.__spinlock))
+#define PTHREAD_BARRIER_T_NUM_THREADS(barrier) (*((volatile int*)(&barrier->__ba_required)))
+#define PTHREAD_BARRIER_T_COUNTER(barrier) (*((volatile int*)(&barrier->__ba_present)))
+#define PTHREAD_BARRIER_T_DIRECTION(barrier) (*((volatile int*)(&barrier->__ba_waiting)))
+
+// definitions for NPTL implementation
+#else /* __SIZEOF_PTHREAD_MUTEX_T defined */
+#define PTHREAD_MUTEX_T_COUNT __data.__count
+
+#define PTHREAD_RWLOCK_T_LOCK(rwlock)  (*(volatile int*)(&rwlock->__data.__lock))
+#define PTHREAD_RWLOCK_T_READERS(rwlock)  (*(volatile int*)(&rwlock->__data.__nr_readers))
+#define PTHREAD_RWLOCK_T_WRITER(rwlock)  (*(volatile int*)(&rwlock->__data.__writer))
+
+#if defined(__GNUC__) && __GNUC__ >= 4
+#define PTHREAD_COND_T_FLAG(cond) (*(volatile int*)(&(cond->__data.__lock)))
+#define PTHREAD_COND_T_THREAD_COUNT(cond) (*(volatile int*)(&(cond-> __data.__futex)))
+#define PTHREAD_COND_T_COUNT_LOCK(cond) (*(volatile int*)(&(cond->__data.__nwaiters)))
+
+//For tree barriers
+//#define PTHREAD_BARRIER_T_NUM_THREADS(barrier)  (*((int*)(barrier->__size+(0*sizeof(int)))))
+//#define PTHREAD_BARRIER_T_BARRIER_PTR(barrier) (*(tree_barrier_t**)(barrier->__size+(1*sizeof(int))))
+
+#define PTHREAD_BARRIER_T_SPINLOCK(barrier) (*((volatile int*)(barrier->__size+(0*sizeof(int)))))
+#define PTHREAD_BARRIER_T_NUM_THREADS(barrier) (*((volatile int*)(barrier->__size+(1*sizeof(int)))))
+#define PTHREAD_BARRIER_T_COUNTER(barrier) (*((volatile int*)(barrier->__size+(2*sizeof(int)))))
+#define PTHREAD_BARRIER_T_DIRECTION(barrier) (*((volatile int*)(barrier->__size+(3*sizeof(int)))))
+
+//Tree barrier-related
+#if 0
+#ifndef __SIZEOF_PTHREAD_BARRIER_T
+#error __SIZEOF_PTHREAD_BARRIER_T not defined
+#endif
+#if ((4/*fields*/*4/*sizeof(int32)*/) > __SIZEOF_PTHREAD_BARRIER_T)
+#error barrier size __SIZEOF_PTHREAD_BARRIER_T not large enough for our implementation
+#endif
+#endif
+
+#else // gnuc >= 4
+//gnuc < 4
+#error "This library requires gcc 4.0+ (3.x should work, but you'll need to change pthread_defs.h)"
+#endif // gnuc >= 4
+
+#endif // LinuxThreads / NPTL
+
+// non-linux definitions... fill this in?
+#else // !__linux__
+  #error "Non-Linux pthread definitions not available"
+#endif //!__linux__
+
+#endif //  __PTHREAD_DEFS_H__
diff --git a/spinlock_alpha.h b/spinlock_alpha.h
new file mode 100644
index 0000000..e2f8ee2
--- /dev/null
+++ b/spinlock_alpha.h
@@ -0,0 +1,75 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+*/
+
+
+
+#ifndef __SPINLOCK_ALPHA_H__
+#define __SPINLOCK_ALPHA_H__
+
+// routines adapted from /usr/src/linux/include/asm-alpha/spinlock.h
+
+static __inline__ void spin_lock (volatile int* lock) {
+        long tmp;
+        __asm__ __volatile__(
+         "1:     ldl_l   %0,%1\n"
+         "       bne     %0,2f\n"
+         "       lda     %0,1\n"
+         "       stl_c   %0,%1\n"
+         "       beq     %0,2f\n"
+         "       mb\n"
+         ".subsection 2\n"
+         "2:     ldl     %0,%1\n"
+         "       bne     %0,2b\n"
+         "       br      1b\n"
+         ".previous"
+         : "=&r" (tmp), "=m" (*lock)
+         : "m"(*lock) : "memory");
+}
+
+static __inline__ void spin_unlock (volatile int* lock) {
+   __asm__ __volatile__ ("mb\n");
+   *lock = 0;
+}
+
+static __inline__ int trylock (volatile int* lock) {
+
+
+	long regx;
+	int success;
+
+	__asm__ __volatile__(
+	"1:	ldl_l	%1,%0\n"
+	"	lda	%2,0\n"
+	"	bne	%1,2f\n"
+	"	lda	%2,1\n"
+	"	stl_c	%2,%0\n"
+	"	beq	%2,6f\n"
+	"2:	mb\n"
+	".subsection 2\n"
+	"6:	br	1b\n"
+	".previous"
+	: "=m" (*lock), "=&r" (regx), "=&r" (success)
+	: "m" (*lock) : "memory");
+
+	return success;
+
+
+}
+
+#endif  // __SPINLOCK_H__
diff --git a/spinlock_sparc.h b/spinlock_sparc.h
new file mode 100644
index 0000000..901fff7
--- /dev/null
+++ b/spinlock_sparc.h
@@ -0,0 +1,73 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+*/
+
+
+#ifndef __SPINLOCK_SPARC_H__
+#define __SPINLOCK_SPARC_H__
+
+// routines from /usr/src/linux/include/asm-sparc/spinlock_64.h
+// Note: these work even with RMO, but a few barriers could be eliminated for TSO
+
+static __inline__ void spin_lock(volatile int* lock)
+{
+	unsigned long tmp;
+
+	__asm__ __volatile__(
+"1:	ldstub		[%1], %0\n"
+"	membar		#StoreLoad | #StoreStore\n"
+"	brnz,pn		%0, 2f\n"
+"	 nop\n"
+"	.subsection	2\n"
+"2:	ldub		[%1], %0\n"
+"	membar		#LoadLoad\n"
+"	brnz,pt		%0, 2b\n"
+"	 nop\n"
+"	ba,a,pt		%%xcc, 1b\n"
+"	.previous"
+	: "=&r" (tmp)
+	: "r" (lock)
+	: "memory");
+}
+
+static __inline__ int trylock(volatile int* lock)
+{
+	unsigned long result;
+
+	__asm__ __volatile__(
+"	ldstub		[%1], %0\n"
+"	membar		#StoreLoad | #StoreStore"
+	: "=r" (result)
+	: "r" (lock)
+	: "memory");
+
+	return (result == 0);
+}
+
+static __inline__ void spin_unlock(volatile int* lock)
+{
+	__asm__ __volatile__(
+"	membar		#StoreStore | #LoadStore\n"
+"	stb		%%g0, [%0]"
+	: // No outputs 
+	: "r" (lock)
+	: "memory");
+}
+
+
+#endif  // __SPINLOCK_SPARC_H__
diff --git a/spinlock_x86.h b/spinlock_x86.h
new file mode 100644
index 0000000..0da99d8
--- /dev/null
+++ b/spinlock_x86.h
@@ -0,0 +1,59 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+*/
+
+
+#ifndef __SPINLOCK_X86_H__
+#define __SPINLOCK_X86_H__
+
+// routines from /usr/src/linux/include/asm-x86/spinlock.h
+
+static __inline__ void spin_lock (volatile int* lock) {
+    char oldval;
+    __asm__ __volatile__
+        (
+         "\n1:\t" \
+         "cmpb $0,%1\n\t" \
+         "jne 1b\n\t" \
+         "xchgb %b0, %1\n\t" \
+         "cmpb $0,%0\n" \
+         "jne 1b\n\t"
+         :"=q"(oldval), "=m"(*lock)
+         : "0"(1)
+         : "memory");
+}
+
+static __inline__ void spin_unlock (volatile int* lock) {
+	__asm__ __volatile__
+        ("movb $0,%0" \
+         :"=m" (*lock) : : "memory");
+}
+
+static __inline__ int trylock (volatile int* lock) {
+    char oldval;
+    __asm__ __volatile__
+        (
+         "xchgb %b0,%1"
+         :"=q" (oldval),
+          "=m" (*lock)
+         :"0" (1) 
+         : "memory");
+    return oldval == 0;
+}
+
+#endif  // __SPINLOCK_X86_H__
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100755
index 0000000..ddd295f
--- /dev/null
+++ b/tests/Makefile
@@ -0,0 +1,52 @@
+
+
+# ==== Variables ==============================================================
+
+# 64-bit compiles
+#Uncomment to use sparc/alpha cross-compilers
+CC := sparc64-unknown-linux-gnu-gcc
+CPP := sparc64-unknown-linux-gnu-g++
+#CC := alpha-unknown-linux-gnu-gcc
+#CPP := alpha-unknown-linux-gnu-g++
+
+#CC := gcc
+#CPP := g++
+
+#CFLAGS := -ggdb3 -O3 -D__DEBUG
+CFLAGS := -g -O3
+
+CPPFLAGS := $(CFLAGS)
+
+TEST_OBJS := test_stackgrow.o test_pthreadbasic.o test_pthread.o test_atomic.o test_barrier.o test_lock.o test_malloc.o test_sieve.o  test___thread.o test_omp.o
+
+TEST_PROGS := $(TEST_OBJS:.o=)
+
+
+# ==== Rules ==================================================================
+
+.PHONY: default clean
+
+default: $(TEST_PROGS) 
+
+clean:
+	$(RM)  $(TEST_OBJS) $(TEST_PROGS) $(TEST_OBJS:.o=_p) ../pthread.o
+
+$(TEST_PROGS): $(TEST_OBJS) ../pthread.o
+	$(CPP)  -static -o $@  $@.o ../pthread.o
+	$(CPP)  -static -o $@_p  $@.o -lpthread
+
+%.o: %.cpp Makefile
+	$(CPP) $(CPPFLAGS)  -c -o $@ $*.cpp
+
+#Special rules for OpenMP programs
+test_omp: test_omp.o
+	$(CPP)  -static -o $@  $@.o -lgomp ../pthread.o -lgomp
+	$(CPP)  -static -o $@_p  $@.o -lgomp -lpthread
+
+test_omp.o: test_omp.cpp ../pthread.o
+	$(CPP) $(CPPFLAGS) -fopenmp -c -o $@ $*.cpp
+
+../pthread.o: ../pthread.c ../pthread_defs.h ../tls_defs.h Makefile
+	$(CC) $(CFLAGS) -c ../pthread.c -o ../pthread.o
+
+
diff --git a/tests/test___thread.cpp b/tests/test___thread.cpp
new file mode 100644
index 0000000..d597155
--- /dev/null
+++ b/tests/test___thread.cpp
@@ -0,0 +1,117 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+*/
+
+
+
+#include <assert.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <sys/time.h>
+
+// without volatile, simulator test works even if __thread support is broken
+__thread volatile int local = 7;
+
+volatile long long int jjjs = 0;
+
+static const int count = 1024;
+
+void* run (void* arg)
+{
+    long long int id = (long long int)arg;
+    int i;
+    printf("&local[%d]=%p\n", id, &local);
+    local += id;
+    for (i = 0; i < count; i++) {
+        local++;
+    }
+
+    //Some calculations to delay last read
+    long long int jjj = 0;
+    for (i = 0; i < 10000; i++) {
+      jjj = 2*jjj +4 -i/5 + local;
+    }
+    jjjs = jjj;
+ 
+    //assert(local == count +id);
+    return (void*)local;
+}
+
+int main (int argc, char** argv)
+{
+    if (argc != 2) { 
+        printf("usage: %s <thread_count>\n", argv[0]);
+        exit(1);
+    }
+    int thread_count = atoi(argv[1]);
+
+    printf("Starting %d threads...\n", thread_count);
+
+    //struct timeval startTime;
+    //int startResult = gettimeofday(&startTime, NULL);
+    //assert(startResult == 0);
+    int i;
+    pthread_t* threads = (pthread_t*)calloc(thread_count, sizeof(pthread_t));
+    assert(threads != NULL);
+    for (i = 1 ; i < thread_count; i++) {
+        int createResult = pthread_create(&threads[i], 
+                                          NULL,
+                                          run,
+                                          (void*)i);
+        assert(createResult == 0);
+    }
+
+    long long int local = (long long int)run((void*)0);
+    printf("local[0] = %d\n", local);
+
+    for (i = 1 ; i < thread_count; i++) {
+        int joinResult = pthread_join(threads[i], 
+                                      (void**)&local);
+        assert(joinResult == 0);
+        printf("local[%d] = %d\n", i, local);
+    }
+    
+    /*struct timeval endTime;
+    int endResult = gettimeofday(&endTime, NULL);
+    assert(endResult == 0);
+    
+    long startMillis = (((long)startTime.tv_sec)*1000) + (((long)startTime.tv_usec)/1000);
+    long endMillis   = (((long)endTime.tv_sec)*1000)   + (((long)endTime.tv_usec)/1000);
+    */
+    /*printf("End Time (s)    = %d\n", (int)endTime.tv_sec);
+    printf("Start Time (s)  = %d\n", (int)startTime.tv_sec);
+    printf("Time (s)        = %d\n", (int)(endTime.tv_sec-startTime.tv_sec));
+    printf("\n");
+
+    printf("End Time (us)   = %d\n", (int)endTime.tv_usec);
+    printf("Start Time (us) = %d\n", (int)startTime.tv_usec);
+    printf("Time (us)       = %d\n", (int)(endTime.tv_usec-startTime.tv_usec));
+    printf("\n");
+
+    printf("End Time (ms)   = %d\n", (int)endMillis);
+    printf("Start Time (ms) = %d\n", (int)startMillis);
+    printf("Time (ms)       = %d\n", (int)(endMillis-startMillis));
+    printf("\n");*/
+
+    /*double difference=(double)(endTime.tv_sec-startTime.tv_sec)+(double)(endTime.tv_usec-startTime.tv_usec)*1e-6;
+    printf("Time (s) = %f\n", difference);
+    printf("\n");*/
+    return 0;
+}
diff --git a/tests/test_atomic.cpp b/tests/test_atomic.cpp
new file mode 100644
index 0000000..5ead4cb
--- /dev/null
+++ b/tests/test_atomic.cpp
@@ -0,0 +1,132 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+
+    Author: Daniel Sanchez
+*/
+
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+/**
+ * test_atomic:
+ * This benchmark is intended to stress-test atomic operations by using heavily 
+ * contended locks. 
+ */
+
+static pthread_mutex_t lock;
+static pthread_barrier_t barrier;
+
+int* intArray;
+int next;
+
+int iteration;
+
+void* chain(void* arglist)
+{
+  int iteration;
+  for(iteration = 1; iteration <= 10; iteration++) {
+    long long int id = (long long int) arglist;
+    pthread_barrier_wait(&barrier);
+
+    pthread_mutex_lock(&lock);
+
+    int current = next;
+    printf("[Iteration %d, Thread %d] Got lock\n", iteration, id);
+    intArray[current]++;
+
+    //Uncomment this snip for longer-running critical section
+    /*int i;
+    for (i = 0; i < 5000; i++) {
+      next = (i + next)/2;
+      Sim_Print0(""); //so that gcc does not optimize this out
+    }*/
+
+    next = id;
+
+    printf("[Iteration %d, Thread %d] Critical section done, previously next=%d, now next=%d\n", iteration, id, current, next);
+    pthread_mutex_unlock(&lock);
+
+    pthread_barrier_wait(&barrier);
+  }
+
+    return NULL;
+}
+
+
+int main(int argc, const char** const argv) {
+    if (argc != 2) {
+       printf("Usage: ./test_atomic <nthreads>\n");
+       exit(1);
+    }
+    int nthreads = atoi(argv[1]);
+
+    if (nthreads < 2) {
+        printf("\nthis test requires at least 2 cpus\n");
+        exit(0);
+    }
+
+    pthread_t pth[nthreads];
+    pthread_attr_t attr;
+
+    pthread_attr_init(&attr);
+    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
+
+
+    pthread_mutex_init(&lock, NULL);
+    printf("Init done\n");
+
+    int j;
+
+    //for(iteration = 1; iteration <= 10; iteration++) {
+      pthread_barrier_init(&barrier, NULL, nthreads);
+      intArray = (int*) calloc(nthreads, sizeof(int));
+      next = 0;
+      for (j = 1; j < nthreads; j++) {
+        pthread_create(&pth[j], &attr, chain, (void*) j);
+      }
+   
+   
+    for(iteration = 1; iteration <= 10; iteration++) {
+      pthread_barrier_wait(&barrier);
+      /*for (j = 1; j < Sim_GetNumCpus(); j++) {
+        pthread_join(pth[j], NULL);
+      }*/
+      pthread_barrier_wait(&barrier);
+
+      intArray[next]++;
+      int failed = 0;
+      for (j = 0; j < nthreads; j++) {
+        if (intArray[j] != 1) {
+          printf("FAILED: Position %d had %d instead of 1\n", j, intArray[j]);
+          failed = 1;
+        }
+      }
+      if (failed) exit(1);
+      //pthread_barrier_destroy(&barrier);
+      //free(intArray);
+      for (j = 0; j < nthreads; j++) intArray[j] = 0;
+      next = 0;
+      //intArray = (int*) calloc(Sim_GetNumCpus(), sizeof(int));
+
+      printf("Iteration %d completed\n", iteration);
+    }
+
+    printf("PASSED :-)\n");
+}
diff --git a/tests/test_barrier.cpp b/tests/test_barrier.cpp
new file mode 100644
index 0000000..68683e5
--- /dev/null
+++ b/tests/test_barrier.cpp
@@ -0,0 +1,62 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+*/
+
+
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+
+pthread_barrier_t barrier;
+//int A[MAX_NUM_CPUS][64];
+
+void* run (void* arg) {
+    long long int my_id = (long long int) arg;
+    //A[my_id][0]++;
+    printf("%i BEFORE\n", my_id);
+    pthread_barrier_wait(&barrier);
+    printf("%i AFTER\n", my_id);
+    //A[my_id][0]++;
+    return NULL;
+}
+int main (int argc, const char** const argv)  {
+    int i;
+    
+    if (argc != 2) {
+       printf("Usage: ./test_atomic <nthreads>\n");
+       exit(1);
+    }
+
+    int nthreads = atoi(argv[1]);
+
+    pthread_t* pth = (pthread_t*) calloc(nthreads, sizeof(pthread_t));
+
+    pthread_barrier_init(&barrier, NULL, nthreads);
+
+    for (i=1; i < nthreads; i++)  {
+      pthread_create(&pth[i], NULL, run, (void*) i);
+    }
+    run((void*)0);
+    for (i=1; i < nthreads; i++)  {
+      pthread_join(pth[i], NULL);
+    }
+
+    pthread_barrier_destroy(&barrier);
+    free(pth);
+}
diff --git a/tests/test_lock.cpp b/tests/test_lock.cpp
new file mode 100644
index 0000000..f8670f6
--- /dev/null
+++ b/tests/test_lock.cpp
@@ -0,0 +1,128 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+*/
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+static pthread_rwlock_t lock;
+static pthread_mutex_t trylock = PTHREAD_MUTEX_INITIALIZER;
+
+void* run1(void* arglist)
+{
+    pthread_t id = pthread_self();
+    printf("[run1] TID=%d\n", id);
+
+    printf("[run1] started\n");
+
+    pthread_rwlock_rdlock(&lock);
+    printf("[run1] a read lock is obtained\n");
+
+    pthread_rwlock_unlock(&lock);
+    printf("[run1] a read lock is released\n");
+
+    return NULL;
+}
+
+
+void* run2(void* arglist)
+{
+    printf("[run2]started\n");
+
+    int res = pthread_mutex_trylock(&trylock);
+    printf("[run2] try lock result %d\n", res);
+
+    if (res == 0) {
+        pthread_mutex_unlock(&trylock);
+    }
+    return NULL;
+}
+
+int main(int argc, const char** const argv) {
+
+    pthread_t pth;
+    pthread_attr_t attr;
+    int arg;
+
+    pthread_attr_init(&attr);
+    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
+
+    pthread_rwlock_init(&lock, NULL);
+    printf("[main]a rwlock is initialized\n");
+
+    // test 1 : read lock 
+    printf("\n1. read lock test\n");
+
+    pthread_rwlock_rdlock(&lock);
+    printf("[main]a read lock is obtained\n");
+
+    pthread_create(&pth, &attr, run1, &arg);
+    printf("[main]thread created with run1\n");
+
+    pthread_join(pth, NULL);
+    printf("[main]thread joined\n");
+
+    pthread_rwlock_unlock(&lock);
+    printf("[main]a read lock is released\n");
+
+    // test 2 : write lock 
+    printf("\n2. write lock test\n");
+
+    pthread_rwlock_wrlock(&lock);
+    printf("[main]a write lock is obtained\n");
+
+    pthread_create(&pth, &attr, run1, &arg);
+    printf("[main]thread created with run1\n");
+
+    int i;
+    for (i = 0; i < 10; i++) {
+        printf("[main]idling %d\n", i);
+    }
+
+    pthread_rwlock_unlock(&lock);
+    printf("[main]a write lock is released\n");
+
+    pthread_rwlock_destroy(&lock);
+
+    pthread_join(pth, NULL);
+    printf("[main]thread joined\n");
+
+    // test 3 : try lock 
+    printf("\n3. try lock test\n");
+
+    // 3.1 trylock will be tried to an occupied lock
+    pthread_mutex_lock(&trylock);
+    printf("[main]a lock is obtained\n");
+
+    pthread_create(&pth, &attr, run2, &arg);
+    printf("[main]thread created with run2\n");
+
+    pthread_join(pth, NULL);
+    printf("[main]thread joined\n");
+
+    pthread_mutex_unlock(&trylock);
+    printf("[main]a lock is released\n");
+
+    // 3.2 trylock will be tried to a free lock
+    pthread_create(&pth, &attr, run2, &arg);
+    printf("[main]thread created with run2\n");
+
+    pthread_join(pth, NULL);
+    printf("[main]thread joined\n");
+}
diff --git a/tests/test_malloc.cpp b/tests/test_malloc.cpp
new file mode 100644
index 0000000..5d6ca88
--- /dev/null
+++ b/tests/test_malloc.cpp
@@ -0,0 +1,101 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+
+    Author: Daniel Sanchez
+*/
+
+
+#include <pthread.h>
+#include <stdlib.h>
+
+#include <stdio.h>
+
+/**
+ * test_malloc:
+ * This benchmark tests malloc/free by allocating memory concurrently 
+ */
+
+static pthread_barrier_t barrier;
+
+
+void*** ptr_matrix;
+
+int iteration;
+
+int nthreads;
+
+typedef unsigned long int uint32;
+
+void* alloc(void* arglist)
+{
+    long long int id = (long long int) arglist;
+
+    pthread_barrier_wait(&barrier);
+    
+    int bytes = iteration*(id +1);
+    void* ptr = malloc(bytes);
+    ptr_matrix[iteration][id] = ptr;
+    printf("[ALLOC %d, Thread %d] Allocated %d bytes, from %x to %x\n", iteration, id, bytes, (uint32)ptr, (uint32)(((char*)ptr) + bytes - 1));
+
+    pthread_barrier_wait(&barrier);
+    int target = (id + iteration) % nthreads;
+    free(ptr_matrix[iteration][target]);
+    printf("[ALLOC %d, Thread %d] Freed %d's allocation, %x\n", iteration, id, target, (uint32)ptr_matrix[iteration][target]);
+    //free(ptr_matrix[iteration][target]);
+    return NULL;
+}
+
+
+int main(int argc, const char** const argv) {
+
+    if (argc != 2) {
+       printf("Usage: ./test_malloc <nthreads>\n");
+       exit(1);
+    }
+
+    nthreads = atoi(argv[1]);
+
+    pthread_t* pth = (pthread_t*) calloc(nthreads, sizeof(pthread_t));
+
+    pthread_attr_t attr;
+
+    pthread_attr_init(&attr);
+    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
+
+
+    printf("Init done\n");
+
+    int j;
+    ptr_matrix = (void***) calloc(20, sizeof(void**));
+    for(iteration = 1; iteration <= 20; iteration++) {
+      pthread_barrier_init(&barrier, NULL, nthreads);
+      ptr_matrix[iteration] = (void**) calloc(nthreads, sizeof(void*));
+      for (j = 1; j < nthreads; j++) {
+        pthread_create(&pth[j], &attr, alloc, (void*) j);
+      }
+      alloc((void *)0);
+      for (j = 1; j < nthreads; j++) {
+        pthread_join(pth[j], NULL);
+      }
+
+      pthread_barrier_destroy(&barrier);
+
+      printf("Iteration %d completed\n", iteration);
+    }
+    printf("PASSED\n");
+}
diff --git a/tests/test_omp.cpp b/tests/test_omp.cpp
new file mode 100644
index 0000000..b554f44
--- /dev/null
+++ b/tests/test_omp.cpp
@@ -0,0 +1,96 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+
+    Author: Daniel Sanchez
+*/
+
+//Good old matrix multiply using openmp
+
+#include <assert.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <omp.h>
+
+int64_t* A;
+int64_t* B;
+int64_t* C;
+
+int main(int argc, const char** argv) {
+  
+  if (argc != 3) {
+    printf("Usage: ./test_omp <nthreads> <size>\n");
+    exit(1);
+  }
+
+  int nthreads = atoi(argv[1]);
+
+  if (nthreads < 1) {
+    printf("nthreads must be 1 or more\n");
+    exit(1);
+  }
+
+  int size = atoi(argv[2]);
+ 
+  if (size < 1) {
+    printf("size must be 1 or more\n");
+    exit(1);
+  }
+
+  printf("Setting OMP threads to %d\n", nthreads);
+  omp_set_num_threads(nthreads);
+
+  A = (int64_t*) calloc(size*size, sizeof(int64_t));
+  B = (int64_t*) calloc(size*size, sizeof(int64_t));
+  C = (int64_t*) calloc(size*size, sizeof(int64_t));
+
+  printf("Starting with row/col size=%d\n",size);
+
+  for(int x = 0; x < size; x++) {
+    for (int y = 0; y < size; y++) {
+      A[x*size + y] = x*y;
+    }
+  }
+  printf("A initialized\n");
+
+  for(int x = 0; x < size; x++) {
+    for (int y = 0; y < size; y++) {
+      B[x*size + y] = x*y - y;
+    }
+  }
+  printf("B initialized\n");
+
+  printf("Computing A*B with %d threads\n", nthreads);
+
+  #pragma omp parallel for
+  for(int x = 0; x < size; x++) {
+    for (int y = 0; y < size; y++) {
+      int64_t tot;
+      for (int m = 0; m < size; m++) {
+        tot += A[x*size + m]*B[m*size + y];
+      }
+      C[x*size + y] = tot;
+    }
+  }
+
+  printf("Done\n");
+
+  return 0;  
+}
+
+
diff --git a/tests/test_pthread.cpp b/tests/test_pthread.cpp
new file mode 100644
index 0000000..1b3635f
--- /dev/null
+++ b/tests/test_pthread.cpp
@@ -0,0 +1,199 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+*/
+
+
+
+#include <assert.h>
+#include <pthread.h>
+#include <stdlib.h>
+
+#include <stdio.h>
+
+void* run1(void* arglist)
+{
+    int* args = (int*)arglist;
+    printf("[run1] argument passed %d %d\n", args[0], args[1]);
+
+    // yield() does nothing
+    pthread_yield();
+
+    pthread_t run1th = pthread_self();
+    printf("[run1] thread id : %d \n", (int)run1th);
+
+    pthread_exit(0);
+    assert(false);
+    return NULL;
+}
+
+int gl_counter = 0;
+
+void funcWithCriticalSection()
+{
+    static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+    int result = pthread_mutex_lock(&lock);
+    assert(result == 0);
+    gl_counter++;
+    printf("%d\n",gl_counter);
+    result = pthread_mutex_unlock(&lock);
+    assert(result == 0);
+}
+
+void* run2(void* arglist)
+{
+    int  i;
+    for (i = 0; i <5; i++) {
+        funcWithCriticalSection();
+    }
+    return NULL;
+}
+
+pthread_key_t key;
+
+void* run3(void* arglist)
+{
+    int j;
+    int i = pthread_self();
+    int result = pthread_setspecific(key, (void*)&i);
+    assert(result == 0);
+    void* value = pthread_getspecific(key);
+    assert(value != NULL);
+
+    for (j=5; j >= 0 ; j--) {
+        printf("");
+    }
+    
+    if (*((int*)value) == (int) pthread_self()) {
+        printf("[run3]thread-private value matches 2 : %d \n", *((int*)value));
+    }
+    else {
+        printf("[run3]thread-private value doesn't match 2 : %d \n", *((int*)value));
+    }
+    return NULL;
+}
+
+pthread_cond_t cond_sync = PTHREAD_COND_INITIALIZER;
+pthread_mutex_t cond_lock = PTHREAD_MUTEX_INITIALIZER;
+
+void* run4(void* arglist)
+{
+    int j;
+    for (j=5; j >= 0 ; j--) {
+        printf("[run4] goofing around for a moment %d \n", j);
+    }
+
+    int result = pthread_mutex_lock(&cond_lock);
+    assert(result == 0);
+
+    printf("[run4] about to call broadcast\n"); {
+        result = pthread_cond_broadcast(&cond_sync);
+        assert(result == 0);
+    }
+
+    result = pthread_mutex_unlock(&cond_lock);
+    assert(result == 0);
+    return NULL;
+}
+
+int main(int argc, const char** const argv) {
+
+    // test 1 : creation & join
+    printf("\n1. thread creation and join test\n");
+
+    pthread_t pth;
+    pthread_attr_t attr;
+    int arg[2];
+
+    arg[0] = 2; 
+    arg[1] = 4; 
+
+    int result = pthread_attr_init(&attr);
+    assert(result == 0);
+    result = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
+    assert(result == 0);
+
+    result = pthread_create(&pth, &attr, run1, &arg);
+    assert(result == 0);
+    printf("[main]thread(%d) created with run1\n", (int)pth);
+
+    result = pthread_join(pth, NULL);
+    assert(result == 0);
+    printf("[main]thread(%d) joined\n", (int)pth);
+    
+
+    // test 2 : mutex   
+    printf("\n2. mutex test\n");
+    result = pthread_create(&pth, &attr, run2, &arg);
+    assert(result == 0);
+    printf("[main]thread(%d) created with run2\n", (int)pth);
+    
+    int i;
+    for (i = 0; i <5; i++) {
+        funcWithCriticalSection();
+    }
+
+    result = pthread_join(pth, NULL);
+    assert(result == 0);
+
+
+    // test 3 : key 
+    printf("\n3. thread-private storage test\n");
+    result = pthread_key_create(&key, NULL);
+    assert(result == 0);
+
+    result = pthread_create(&pth, &attr, run3, &arg);
+    assert(result == 0);
+
+    printf("[main]thread(%d) created with run3\n", (int)pth);
+
+    i = 1;
+    result = pthread_setspecific(key, (void*)&i);
+    assert(result == 0);
+    void* value = pthread_getspecific(key);
+    assert(value != NULL);
+
+    if (*((int*)value) == 1) {
+        printf("[main]thread-private value matches 1 : %d \n", *((int*)value));
+    }
+    else {
+        printf("[main]thread-private value doesn't match 1 : %d \n", *((int*)value));
+    }
+
+    result = pthread_join(pth, NULL);
+    assert(result == 0);
+
+
+    // test 4 : wait / notifyall
+    printf("\n4. wait-notifyall test\n");
+    result = pthread_create(&pth, &attr, run4, &arg);
+    assert(result == 0);
+    printf("[main]thread(%d) created with run4\n", (int)pth);
+
+    result = pthread_mutex_lock(&cond_lock);
+    assert(result == 0);
+
+    printf("[main]going into wait()\n");
+    result = pthread_cond_wait(&cond_sync, &cond_lock);
+    assert(result == 0);
+
+    result = pthread_mutex_unlock(&cond_lock);
+    assert(result == 0);
+
+    result = pthread_join(pth, NULL);
+    assert(result == 0);
+}
diff --git a/tests/test_pthreadbasic.cpp b/tests/test_pthreadbasic.cpp
new file mode 100644
index 0000000..85af155
--- /dev/null
+++ b/tests/test_pthreadbasic.cpp
@@ -0,0 +1,64 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+
+    Author: Daniel Sanchez
+*/
+
+
+#include <assert.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+
+void* run (void* arg) {
+    printf("Hello from a child thread! (thread ID: %d).\n", (int)pthread_self());
+    return NULL;
+}
+
+int main(int argc, const char** argv) {
+    pthread_t pth;
+    pthread_attr_t attr;
+
+    printf("Main thread initialized. TID=%d\n", pthread_self());
+    int result = pthread_attr_init(&attr);
+    assert(result == 0);
+    printf("Main thread called pthread_attr_init\n");
+
+    result = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
+    assert(result == 0);
+
+    printf("Main thread called pthread_attr_setscope\n");
+
+    printf("Main thread creating 1st thread...\n");
+    result = pthread_create(&pth, &attr, run, NULL);
+
+    pthread_t pth2;
+    printf("Main thread creating 2nd thread...\n");
+    result = pthread_create(&pth2, &attr, run, NULL);
+
+    printf("Main thread calling join w/ 1st thread (id=%llx)... (self=%llx)\n", pth, pthread_self());
+    pthread_join(pth, NULL);
+    printf("Main thread calling join w/ 2nd thread (id=%llx)... (self=%llx)\n", pth2, pthread_self());
+    pthread_join(pth2, NULL);
+    printf("Main thread has self=%d\n", pthread_self());
+
+    printf("Main thread done.\n");
+}
+
+
diff --git a/tests/test_sieve.cpp b/tests/test_sieve.cpp
new file mode 100644
index 0000000..e1dc367
--- /dev/null
+++ b/tests/test_sieve.cpp
@@ -0,0 +1,201 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+*/
+
+
+/*
+   g++ -O3 -o ./sieve -lm -lpthread sieve.cpp && time sieve 1 && time sieve 2
+*/
+
+#include <assert.h>
+#include <math.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <pthread.h>
+
+
+static int  max_limit;
+static int  sqrt_max_limit;
+static int* not_prime;
+
+static const int print = 1;
+
+static const int start = 2;
+static int sqrt_limit;
+static int limit;
+
+static void* run (void*);
+#ifdef SIMULATOR 
+void mainX(int argc, const char **argv, const char **envp)
+#else
+int main (int argc, char** argv)
+#endif
+{
+#ifdef SIMULATOR
+    if (argc != 1) { 
+        printf("usage: %s\n", argv[0]);
+        exit(1);
+    }
+    int thread_count = Sim_GetNumCpus();
+#else
+    if (argc != 2) { 
+        printf("usage: %s <thread_count>\n", argv[0]);
+        exit(1);
+    }
+    int thread_count = atoi(argv[1]);
+#endif
+
+#ifdef SIMULATOR
+    max_limit      =  10000;
+#else
+    max_limit      =  10000;//0000;
+#endif
+    sqrt_max_limit = (int)ceil(sqrt(max_limit));
+
+    not_prime      = (int*)calloc(max_limit, sizeof(int));
+    assert(not_prime != NULL);
+
+    sqrt_limit = (int)ceil(sqrt(sqrt_max_limit));
+    limit      =                sqrt_max_limit;
+    if (1) {
+        not_prime[0] = 1;
+        not_prime[1] = 1;
+        run(NULL);
+    }
+
+    sqrt_limit = (int)ceil(sqrt(max_limit));
+    limit      =                max_limit;
+
+    printf("sqrt_max_limit %d\n", sqrt_max_limit);
+    printf("max_limit      %d\n", max_limit);
+#ifdef SIMULATOR
+    printf("Starting threads...\n");
+#else
+    printf("Starting %d threads...\n", thread_count);
+#endif
+
+#ifndef SIMULATOR
+    struct timeval startTime;
+    int startResult = gettimeofday(&startTime, NULL);
+    assert(startResult == 0);
+#endif
+
+
+    pthread_t* threads = (pthread_t*)calloc(thread_count, sizeof(pthread_t));
+    assert(threads != NULL);
+    for (int i = 1 ; i < thread_count; i++) {
+        int createResult = pthread_create(&threads[i], 
+                                          NULL,
+                                          run,
+                                          NULL);
+        assert(createResult == 0);
+    }
+
+    run(NULL);
+
+    for (int i = 1 ; i < thread_count; i++) {
+        int joinResult = pthread_join(threads[i], 
+                                      NULL);
+        assert(joinResult == 0);
+    }
+    
+#ifndef SIMULATOR
+    struct timeval endTime;
+    int endResult = gettimeofday(&endTime, NULL);
+    assert(endResult == 0);
+    
+    long startMillis = (((long)startTime.tv_sec)*1000) + (((long)startTime.tv_usec)/1000);
+    long endMillis   = (((long)endTime.tv_sec)*1000)   + (((long)endTime.tv_usec)/1000);
+
+    printf("%d\n", (int)endTime.tv_sec);
+    printf("%d\n", (int)startTime.tv_sec);
+    printf("%d\n", (int)(endTime.tv_sec-startTime.tv_sec));
+    printf("\n");
+
+    printf("%d\n", (int)endTime.tv_usec);
+    printf("%d\n", (int)startTime.tv_usec);
+    printf("%d\n", (int)(endTime.tv_usec-startTime.tv_usec));
+    printf("\n");
+
+    printf("%d\n", (int)endMillis);
+    printf("%d\n", (int)startMillis);
+    printf("%d\n", (int)(endMillis-startMillis));
+    printf("\n");
+
+    double difference=(double)(endTime.tv_sec-startTime.tv_sec)+(double)(endTime.tv_usec-startTime.tv_usec)*1e-6;
+    printf("%f\n", difference);
+    printf("\n");
+#endif
+
+    if (print) {
+        printf("Primes less than 100:\n");
+        for (int i = 0; i < 100; i++) {
+            if (!not_prime[i]) {
+                printf("%d\n",i);
+            }
+        }
+    }
+
+#ifndef SIMULATOR
+    return 0;
+#endif
+}
+
+void* run (void* arg)
+{
+    if (0 /*Sim_GetMode() == MODE_TM*/) {
+
+#ifdef WITH_TM
+        for (int my_prime = start; my_prime < sqrt_limit; ++my_prime) {
+            if (!not_prime[my_prime]) {
+                TM_BeginClosed(); {
+                    not_prime[my_prime] = true;
+                }
+                TM_EndClosed();
+                for (int multiple = my_prime*2; multiple < limit; multiple += my_prime) {
+                    not_prime[multiple] = true;
+                }
+                TM_BeginClosed(); {
+                    not_prime[my_prime] = false;
+                }
+                TM_EndClosed();
+            }
+        }
+#else
+        printf("Somehow mode is MODE_TM but WITH_TM was not defined.");
+        exit(1);
+#endif
+    }
+    else {
+        for (int my_prime = start; my_prime < sqrt_limit; ++my_prime) {
+            if (!not_prime[my_prime]) {
+                // Sim_Print1("Found prime: %d\n", my_prime);
+                not_prime[my_prime] = 1;
+                for (int multiple = my_prime*2; multiple < limit; multiple += my_prime) {
+                    not_prime[multiple] = 1;
+                }
+                not_prime[my_prime] = 0;
+            } else {
+                // Sim_Print1("not prime: %d\n", my_prime);
+            }
+        }
+    }
+    return NULL;
+}
diff --git a/tests/test_stackgrow.cpp b/tests/test_stackgrow.cpp
new file mode 100644
index 0000000..5386a32
--- /dev/null
+++ b/tests/test_stackgrow.cpp
@@ -0,0 +1,46 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+
+    Author: Daniel Sanchez
+*/
+
+
+
+/* Just test which direction the stack grows in this arch/ABI
+   Kind of a big deal when deciding which end of the stack to
+   pass as a pointer to clone :-)
+*/
+
+#include <stdio.h>
+
+
+void func (int* f1) {
+  int f2;
+  printf("Addr frame 1 = %llx, Addr frame 2 = %llx\n", f1, &f2);
+  if (&f2 > f1) {
+    printf("Stack grows up (and this threading library needs to be fixed for your arch...)\n");
+  } else {
+    printf("Stack grows down\n");
+  }
+}
+
+int main (int argc, char**argv) {
+  int f1;
+  func(&f1);
+}
+
diff --git a/tls_defs.h b/tls_defs.h
new file mode 100644
index 0000000..0b51a06
--- /dev/null
+++ b/tls_defs.h
@@ -0,0 +1,172 @@
+/*
+    m5threads, a pthread library for the M5 simulator
+    Copyright (C) 2009, Stanford University
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+*/
+
+#ifndef __TLS_DEFS_H__
+#define __TLS_DEFS_H__
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+
+//These are mostly taken verbatim from glibc 2.3.6
+
+//32 for ELF32 binaries, 64 for ELF64
+//TODO: Macro it
+#define __ELF_NATIVE_CLASS 64
+
+/* Standard ELF types.  */
+
+#include <stdint.h>
+
+/* Type for a 16-bit quantity.  */
+typedef uint16_t Elf32_Half;
+typedef uint16_t Elf64_Half;
+
+/* Types for signed and unsigned 32-bit quantities.  */
+typedef uint32_t Elf32_Word;
+typedef int32_t  Elf32_Sword;
+typedef uint32_t Elf64_Word;
+typedef int32_t  Elf64_Sword;
+
+/* Types for signed and unsigned 64-bit quantities.  */
+typedef uint64_t Elf32_Xword;
+typedef int64_t  Elf32_Sxword;
+typedef uint64_t Elf64_Xword;
+typedef int64_t  Elf64_Sxword;
+
+/* Type of addresses.  */
+typedef uint32_t Elf32_Addr;
+typedef uint64_t Elf64_Addr;
+
+/* Type of file offsets.  */
+typedef uint32_t Elf32_Off;
+typedef uint64_t Elf64_Off;
+
+/* Type for section indices, which are 16-bit quantities.  */
+typedef uint16_t Elf32_Section;
+typedef uint16_t Elf64_Section;
+
+/* Type for version symbol information.  */
+typedef Elf32_Half Elf32_Versym;
+typedef Elf64_Half Elf64_Versym;
+
+
+typedef struct
+{
+  Elf32_Word    p_type;                 /* Segment type */
+  Elf32_Off     p_offset;               /* Segment file offset */
+  Elf32_Addr    p_vaddr;                /* Segment virtual address */
+  Elf32_Addr    p_paddr;                /* Segment physical address */
+  Elf32_Word    p_filesz;               /* Segment size in file */
+  Elf32_Word    p_memsz;                /* Segment size in memory */
+  Elf32_Word    p_flags;                /* Segment flags */
+  Elf32_Word    p_align;                /* Segment alignment */
+} Elf32_Phdr;
+
+typedef struct
+{
+  Elf64_Word    p_type;                 /* Segment type */
+  Elf64_Word    p_flags;                /* Segment flags */
+  Elf64_Off     p_offset;               /* Segment file offset */
+  Elf64_Addr    p_vaddr;                /* Segment virtual address */
+  Elf64_Addr    p_paddr;                /* Segment physical address */
+  Elf64_Xword   p_filesz;               /* Segment size in file */
+  Elf64_Xword   p_memsz;                /* Segment size in memory */
+  Elf64_Xword   p_align;                /* Segment alignment */
+} Elf64_Phdr;
+
+
+#define ElfW(type) _ElfW (Elf, __ELF_NATIVE_CLASS, type)
+#define _ElfW(e,w,t)       _ElfW_1 (e, w, _##t)
+#define _ElfW_1(e,w,t)     e##w##t
+
+
+#define PT_TLS              7               /* Thread-local storage segment */
+
+
+# define roundup(x, y)  ((((x) + ((y) - 1)) / (y)) * (y))
+
+extern ElfW(Phdr) *_dl_phdr;
+extern size_t _dl_phnum;
+
+//Architecture-specific definitions
+
+#if defined(__x86_64) || defined(__amd64)
+
+/* Type for the dtv.  */
+typedef union dtv
+{
+  size_t counter;
+  void *pointer;
+} dtv_t;
+
+typedef struct
+{
+  void *tcb;            /* Pointer to the TCB.  Not necessary the
+                           thread descriptor used by libpthread.  */
+  dtv_t *dtv;
+  void *self;           /* Pointer to the thread descriptor.  */
+  int multiple_threads;
+} tcbhead_t;
+
+#include <asm/prctl.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+
+/* Macros to load from and store into segment registers.  */
+# define TLS_GET_FS() \
+  { int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; }
+# define TLS_SET_FS(val) \
+  __asm ("movl %0, %%fs" :: "q" (val))
+
+# define TLS_INIT_TP(thrdescr, secondcall) \
+  { void *_thrdescr = (thrdescr);                                            \
+     tcbhead_t *_head = (tcbhead_t *) _thrdescr;                              \
+     int _result;                                                             \
+                                                                              \
+     _head->tcb = _thrdescr;                                                  \
+     /* For now the thread descriptor is at the same address.  */             \
+     _head->self = _thrdescr;                                                 \
+                                                                              \
+     /* It is a simple syscall to set the %fs value for the thread.  */       \
+     asm volatile ("syscall"                                                  \
+                   : "=a" (_result)                                           \
+                   : "0" ((unsigned long int) __NR_arch_prctl),               \
+                     "D" ((unsigned long int) ARCH_SET_FS),                   \
+                     "S" (_thrdescr)                                          \
+                   : "memory", "cc", "r11", "cx");                            \
+                                                                              \
+    _result ? "cannot set %fs base address for thread-local storage" : 0;     \
+  }
+
+#elif defined (__sparc)
+
+register struct pthread *__thread_self __asm__("%g7");
+
+/* Code to initially initialize the thread pointer.  */
+# define TLS_INIT_TP(descr, secondcall) \
+  (__thread_self = (__typeof (__thread_self)) (descr), NULL)
+
+#else
+  #error "No TLS defs for your architecture"
+#endif
+
+#endif /*__TLS_DEFS_H__*/
+