KernelNewbies
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Kernel Hacking

  • Frontpage

  • Kernel Hacking

  • Kernel Documentation

  • Kernel Glossary

  • FAQ

  • Found a bug?

  • Kernel Changelog

  • Upstream Merge Guide

Projects

  • KernelJanitors

  • KernelMentors

  • KernelProjects

Community

  • Why a community?

  • Regional Kernelnewbies

  • Personal Pages

  • Upcoming Events

References

  • Mailing Lists

  • Related Sites

  • Programming Links

Wiki

  • Recent Changes

  • Site Editors

  • Side Bar

  • Tips for Editors

  • Hosted by WikiWall

Navigation

  • RecentChanges
  • FindPage
  • HelpContents

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

Revision 1 as of 2004-12-20 03:01:01
KernelNewbies:
  • CopyUser

Intention of this page

This page was created by [mailto:per.persson@gnosjo.pp.se Per Persson] for describing the functions and macros copy_{to,from}_user() and __copy_{to,from}_user. Several people have asked about them at kernelnewbies@nl.linux.org.

Hopefully, I will not just start creating this page, but also complete it (if it can be completed :) ).

In the meantime, take a look at http://www.kernelnewbies.org/documents/copy_user/

  • /Per

Introduction

During the time I've been on the KernelNewbies mailing list, I've seen questions about the "copy_user" functions several times, and also tried to explain some of them. Although I'm just a kernel newbie, the response to my explanations has been very positive. John Levon even has published my explanation of __copy_user at KernelNewbies: http://www.kernelnewbies.org/documents/copy_user/.

Even if you're not especially interested in the "copy_user" functions/macros themselves, their implementations contain some techniques that can be useful in other situations:

  • macros and inline functions
  • inline assembly (gcc style)
  • sections
  • removal of unneeded branches


Most of the code considered in this text can be found in the file [http://lxr.linux.no/source/include/asm-i386/uaccess.h?v=2.4.16 include/asm-i386/uaccess.h].

There are four functions/macros that I will cover here:

  1. copy_to_user

  2. copy_from_user

  3. __copy_to_user

  4. __copy_from_user

Direction

Accesschecking?

Kernel-->User

User-->Kernel

Yes

copy_to_user

copy_from_user

No

__copy_to_user

__copy_from_user

Perhaps the __copy_{to,from}_user macros would better be called copy_{to,from}_user_nocheck because they don't do the access-checking that copy_{to,from}_user do.

Constant or Generic?

At the top level, the four "functions" are implemented as macros:

#define copy_to_user(to,from,n)                         \
        (__builtin_constant_p(n) ?                      \
         __constant_copy_to_user((to),(from),(n)) :     \
         __generic_copy_to_user((to),(from),(n)))

This code tells the compiler to check if n is a constant (known at compile time), and if so the optimised (inline) function __constant_copy_to_user should be used, in other cases the function __generic_copy_to_user should be used.

  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01