커널 소스 파악 질문있습니다.

jbs0123의 이미지

context switching이 어떻게 동작하는지 궁금하여 분석을 해보려다...내공이 부족하여 질문을 올리게 되었습니다.
일단
movq %rsp,TASK_threadsp(%rdi)
movq TASK_threadsp(%rsi), %rsp
이런 코드가 있는데 TASK_threadsp가 어떤 동작을 하는지 모르겠습니다. 찾아 들어가면 결국 마지막에
asm volatile("\n->" #sym "%0" #val :: "i" (val)) 나옵니다. 이건 또 어떤 문법인지..제가 C언어를 못하는건지 아니면 어셈인지.. 파악을 못하겠습니다. 도움좀 부탁드립니다.

hansung51의 이미지

어셈블리입니다.
asm 명령어는 c에서 어셈으로 코딩 할 때 사용하는 것이고,
mov 명령어들은 값을 복사하는 어셈명령어입니다.
더 자세한 내용은 저도 고수님들을 기다려야겠네요 ^^;;

AustinKim의 이미지

컨택스트 스위칭을 할 때 레지스터 세트를 프로세스 만의 공간에 저장하거나 로딩합니다.
해당 코드는 관련 동작인데, TASK_threadsp 는 태스크 디스크립터 구조체 기준으로 thread.sp 주소 오프셋을 의미합니다.

 
https://code.woboq.org/linux/linux/include/generated/asm-offsets.h.html
#define TASK_threadsp 12120 /* offsetof(struct task_struct, thread.sp) */

관련 코드를 조금 더 보겠습니다.
다음은 struct task_struct 구조체인데 struct thread_struct 타입인 thread 필드가 있습니다.

 
https://elixir.bootlin.com/linux/v4.14.60/source/include/linux/sched.h 
struct task_struct {
	/* -1 unrunnable, 0 runnable, >0 stopped: */
	volatile long			state;
...
	void				*stack;
	atomic_t			usage;
...
	/* CPU-specific state of this task: */
	struct thread_struct		thread;  //<<---

struct thread_struct 구조체를 보면 sp 필드가 있습니다.

 
https://elixir.bootlin.com/linux/v4.14.60/source/arch/x86/include/asm/processor.h 
struct thread_struct {
	/* Cached TLS descriptors: */
	struct desc_struct	tls_array[GDT_ENTRY_TLS_ENTRIES];
#ifdef CONFIG_X86_32
	unsigned long		sp0;
#endif
	unsigned long		sp; //<<--


이해하기 쉽게 코드로 표현하면 다음 수식이 되겠군요.
 
struct task_struct.thread.sp - struct task_struct 

(개인블로그)
http://rousalome.egloos.com

jbs0123의 이미지

답변 감사합니다. 안 그래도 커널 접하고 우연히 Austin Kim님 블로그를 알게되어서 4장 프로세스부터 공부하고 있었습니다. 여기서 답변을 해주실줄이야... 감사합니다.

AustinKim의 이미지

도움이 됐다고 하시니 뿌듯하군요. 제 블로그 자주 오셔서 유용한 정보 얻어 가시길 빕니다.

(개인블로그)
http://rousalome.egloos.com

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.