Developing a Calling Convention
Posted: Tue Oct 20, 2015 12:29 pm
Hi all,
I am in the process of transitioning out of my boot loader and into my Kernel, and am looking to make some decisions related to how I want to structure the code going forward. There are a number of different calling conventions for calling both ASM code and interfacing C with ASM, and I am looking to make a decision on one early in my development cycle, for consistency reasons.
I am stuck between wanting to implement the cdecl standard (as it's just that, standard and generally ubiquitous) and one of the other, more recently implemented x64 variants (System V X86_64 being the most attractive), as I would like to start in 64-bit long mode. Each calling convention seems to have its own distinct advantages and disadvantages as well, and I have some concerns.
1) Is Cdecl the "correct" way to go? What happens with arrays, structs, or varargs?
2) Cdecl uses stack based parameter passsing, which, I presume, is slower than passing parameters directly in registers. Other calling conventions pass parameters directly in registers first, followed by others put on the stack; yet this method seems more complicated to implement and maintain, is that presumption correct?
3) In System V X86_64, there is a note mentioning the "Red Zone" in http://wiki.osdev.org/Calling_Conventions#System_V_ABI. I am slightly confused as to the purpose of this space, and the same confusion arises in other calling conventions that have similar constructs (the Microsoft x64 calling convention with its "Shadow Space"). Can someone explain the purpose of that reserved space?
4) Why in System V X86_64 does the stack need to be 16-Byte aligned? This is 128-bits (correct)? Double the word size?
It seems I am leaning in the way of following the cdecl standard, unless there are compelling reasons I should use another calling convention.
I am in the process of transitioning out of my boot loader and into my Kernel, and am looking to make some decisions related to how I want to structure the code going forward. There are a number of different calling conventions for calling both ASM code and interfacing C with ASM, and I am looking to make a decision on one early in my development cycle, for consistency reasons.
I am stuck between wanting to implement the cdecl standard (as it's just that, standard and generally ubiquitous) and one of the other, more recently implemented x64 variants (System V X86_64 being the most attractive), as I would like to start in 64-bit long mode. Each calling convention seems to have its own distinct advantages and disadvantages as well, and I have some concerns.
1) Is Cdecl the "correct" way to go? What happens with arrays, structs, or varargs?
2) Cdecl uses stack based parameter passsing, which, I presume, is slower than passing parameters directly in registers. Other calling conventions pass parameters directly in registers first, followed by others put on the stack; yet this method seems more complicated to implement and maintain, is that presumption correct?
3) In System V X86_64, there is a note mentioning the "Red Zone" in http://wiki.osdev.org/Calling_Conventions#System_V_ABI. I am slightly confused as to the purpose of this space, and the same confusion arises in other calling conventions that have similar constructs (the Microsoft x64 calling convention with its "Shadow Space"). Can someone explain the purpose of that reserved space?
4) Why in System V X86_64 does the stack need to be 16-Byte aligned? This is 128-bits (correct)? Double the word size?
It seems I am leaning in the way of following the cdecl standard, unless there are compelling reasons I should use another calling convention.